I'm searching for data structure to store list o开发者_如何学Gof unique indices (integers). The most important features for me are:
- fast checking if value exists in set of values - like in hashtable - small size in memory and after serialization - like array It should of course support adding, removing elements, but performance of this actions aren't significiant.Is there any structure in framework that works in this way? Or I should create it?
Example of use: I have class for user and in this class few (~20) lists of various data. (accesses, privilages, documents etc). I need to store user data in cache for fast access during postbacks - querying DB each time is very slow. Integers are indices in db,
I think you're looking for HashSet<T> http://msdn.microsoft.com/en-us/library/bb359438.aspx
It's implemented so that it provides O(1) lookups (or so the documentation says, however I suspect its really an amortized O(1) since its implemented with Hashtables...) and supports many set operations.
I am not sure of its serialized form, but I will investigate it further. If you really want it to serialize to an array you could always do
var mySet = new HashSet<T>(new []{ 1, 2, 3, 3, 4, 5, 4, 5 });
Serialize(mySet.ToArray());
and then to deserialize just create a HashSet from the serialized array.
精彩评论