2012-09-12 4 views
5

Sto cercando un dizionario immutabile C# corretto, con metodi di aggiornamento rapidi (che creano una copia parziale del dizionario con lievi modifiche). Ne ho implementato uno io stesso, usando le cerniere per aggiornare un albero rosso-nero, ma non è particolarmente veloce.Esiste un dizionario immutabile open source per C#, con metodi rapidi "With/Without"?

Con "dizionario immutabile" non intendo solo readonly o const. Voglio qualcosa che abbia ragionevolmente veloce "Con" e "Senza", o metodi equivalenti, che restituiscano una cosa con leggere modifiche senza modificare l'originale.

Un esempio da un'altra lingua è map in Scala

risposta

1

V'è una certa implementation of the immutable dictionary sulla base di sola lettura binaria albero AVL.

/** 
* To modify, use the InsertIntoNew and RemoveFromNew methods 
* which return a new instance with minimal changes (about Log C), 
* so this is an efficient way to make changes without having 
* to copy the entire data structure. 
*/ 

Si prega di dare un'occhiata al metodo InsertIntoNew():

/** Return a new tree with the key-value pair inserted 
* If the key is already present, it replaces the value 
* This operation is O(Log N) where N is the number of keys 
*/ 
public ImmutableDictionary<K,V> InsertIntoNew(K key, V val) 
{ ... } 

Il RemoveFromNew() metodo:

/** Try to remove the key, and return the resulting Dict 
* if the key is not found, old_node is Empty, else old_node is the Dict 
* with matching Key 
*/ 
public ImmutableDictionary<K,V> RemoveFromNew(K key, out ImmutableDictionary<K,V> old_node) 
{ ... } 

Inoltre, v'è un'altra implementazione: Immutable AVL Tree in C#. Ha la stessa O (log N) tempi di ricerca e inserimento.