![]() (There's similar problems with floating-point numbers, but trying to represent floating-point numbers in sets is a bad idea in the first place.) You would probably want to hash the values, but if you're concerned about hash collisions you should save the string in the set and do a strncmp() to compare the stored string with the probing string. Given char a = "Hello, World!" char b = "Hello, World!", a set of pointers would find a and b to be different. You could enter it as (void *) 5, and for practical purposes this is likely to work with small integers, but if your integers can get into large enough sizes to compete with pointers this has a very small probability of failing. You can't have a set containing the value 5 you have to assign 5 to a variable, which means it won't match a random 5. The downside of this is that you can't enter rvalues independently. This means you need a hash map or binary tree containing pointers, and this will work for all data objects. The way to get genericity in C is by void *, so you're going to be using pointers anyway, and pointers to different objects are unique. You can also use open addressing as an alternative to maintaining and managing buckets. the hash-set contains/ insert/ remove functionality.an equality function for the type being used to test whether two hashables are equal or not.the hash function for the type being hashed.With large amounts of memory at your disposal, you can size your array of buckets generously and, in combination with a good hash method, drastically reduce the probability of collision, achieving virtually constant-time performance. one or more of the hashables contained in the hash-set could be (a pointer to) another hash-set, or even to the hash-set itself (i.e.a hashable's hash value is used to index into the array of buckets (hash-value-indexed array). ![]() ![]() a bucket can be implemented as a dynamic array or linked list of hashables.all hashables in a bucket have the same hash value.You can design a hash-set (a special case of hash-tables) of pointers to hashable PODs, with chaining, internally represented as a fixed-size array of buckets of hashables, where: Since you mentioned value-indexed arrays, let's try the hash-based approach which builds naturally on top of the value-indexed array technique.īeware of the advantages and disadvantages of hash-based vs. hash-based approach (unordered traversal).tree-based approach (ordered traversal).There are multiple ways of implementing set (and map) functionality, for example:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |