3bd5e4a76d765510bc916a0e2462a2e351af0433
[satune.git] / src / Collections / structs.c
1 #include "structs.h"
2 #include "mymemory.h"
3 #include "orderpair.h"
4 #include "tableentry.h"
5
6 VectorImpl(Table, Table *, 4);
7 VectorImpl(Set, Set *, 4);
8 VectorImpl(Boolean, Boolean *, 4);
9 VectorImpl(Function, Function *, 4);
10 VectorImpl(Predicate, Predicate *, 4);
11 VectorImpl(Element, Element *, 4);
12 VectorImpl(Order, Order *, 4);
13 VectorImpl(TableEntry, TableEntry *, 4);
14 VectorImpl(ASTNode, ASTNode *, 4);
15 VectorImpl(Int, uint64_t, 4);
16
17 inline unsigned int Ptr_hash_function(void * hash) {
18         return (unsigned int)((uint64_t)hash >> 4);
19 }
20
21 inline bool Ptr_equals(void * key1, void * key2) {
22         return key1 == key2;
23 }
24
25 static inline unsigned int order_pair_hash_Function(OrderPair* This){
26         return (uint) (This->first << 2) ^ This->second;
27 }
28
29 static inline unsigned int order_pair_equals(OrderPair* key1, OrderPair* key2){
30         return key1->first== key2->first && key1->second == key2->second;
31 }
32
33 static inline unsigned int table_entry_hash_Function(TableEntry* This){
34         //http://isthe.com/chongo/tech/comp/fnv/
35         unsigned int h = 2166136261;
36         const unsigned int FNV_PRIME = 16777619;
37         for(uint i=0; i<This->inputSize; i++){
38                 h ^= This->inputs[i];
39                 h *= FNV_PRIME;
40         }
41         return h;
42 }
43
44 static inline bool table_entry_equals(TableEntry* key1, TableEntry* key2){
45         if(key1->inputSize != key2->inputSize)
46                 return false;
47         for(uint i=0; i<key1->inputSize; i++)
48                 if(key1->inputs[i]!=key2->inputs[i])
49                         return false;
50         return true;
51 }
52
53 HashTableImpl(OrderPair, OrderPair *, OrderPair *, order_pair_hash_Function, order_pair_equals, ourfree);
54 HashSetImpl(TableEntry, TableEntry*, table_entry_hash_Function, table_entry_equals);
55
56