What is a simple C library for a set of whole sets?

I need to change the program to C, and I need to include a set of unsigned integer sets. That is, I have millions of sets of integers (each of these whole sets contains from 3 to 100 integers), and I need to save them in some structure by calling it in a directory that can tell me in a logarithmic time whether this an integer set already exists in the directory. The only operations that need to be defined in the directory are search and insert.

It would be easy in languages ​​with built-in support for useful data structures, but I'm a C foreigner and looking back at Google (surprisingly) did not answer my question satisfactorily. This project looks right:

http://uthash.sourceforge.net/

but I will need to create my own hash key generator.

This is a standard, simple problem, so I hope there is a standard and simple solution.

+6
c set integer
source share
4 answers

It depends on what you are going to do with the data. But maybe tsearch does what you want. You can also create a sorted array for each set and view the values ​​with bsearch, although performance may suffer during insertion.

EDIT: if you are looking for a (external) library, you will find a comparison of some C and C ++ hash table implementations here . The author of the article wrote a general header implementation called khash . Thus, you compiled binary do not have any additional dependencies.

+3
source share

EDIT: sorry, I started answering it as in C ++, not in C. Yes, then you have to find your hash function and copy it yourself .. since you already know the average size of the set is not that difficult, just pick a good one hash function! But you will need to code the whole set for one number if you want to check if the directory exists.

You can try using iterative hashing of single dialing numbers:

int hashcode = initvalue for (int i = 0; i < 0; ++i) hashcode = calc_code(hashcode, number_set[i], i); 

so that the hash function depends on its previous value, current number and current index.

What about STL kits?

 #include <set> int nums[6] = {1,6,34,2,67,41}; set<int> numbers; for( int i = 0; i < 6; ++i ) numbers.insert(nums[i]); for( set<int>::const_iterator iter = numbers.begin(); iter != numbers.end(); ++iter ) cout << *iter << ' '; 

Using this data structure, you can easily store all your sets, but you also need a way to check if the set is included in the catalog. It is not clear: do you want to know if there is a set that contains all ONLY elements in the catalog?

You can do this manually by checking all the elements, but since you have millions, you should find a way to hash the dial elements in a unique number and use a set map.

0
source share

If I understand you correctly, you want to present a set of sets of numbers that, in my opinion, are not trivial.

The first point is the representation of a set of integers. The easiest way is to use a variable-sized array:

 typedef struct { int size; int elems[1]; } intset; 

than you can create a new set (with a fixed number of elements) with

 intset *newset(int size) { intset *set; set = malloc(sizeof(intset) + sizeof(int)*(size-1)); if (set) set->size = size; return set; } 

and save the elements with set->elems[0]=i1; ... set->elems[0]=i1; ...

Another option is to use bitmaps, but the implementation will depend on the nature of the integers to store (for example, are they within a fixed range? Do they usually appear in groups in a set?).

Once you have a set of integers, you will need a comparison function (to determine if two sets have the same elements). If you have selected an array to represent the set and you have saved this array, just check to see if the two sets are identical; if it is a bitmap, it will depend on how you implemented it.

Now, for a set of sets, you can select a (sorted) vector that you may need to change from time to time when inserting elements or a hash table. In the latter case, you need to write a hash function for your sets of integers (possibly using existing functions!).

As I said, this does not seem trivial to me, I am not surprised that Google did not help.

It is not very difficult, but you only need to make some decisions before proceeding.

0
source share

Enter a simple hash table yourself. This will make you a better programmer when you know how to implement it yourself.

http://en.wikipedia.org/wiki/Hash_table

-one
source share

All Articles