adding new hash functions ...
[satune.git] / src / Collections / structs.c
1 #include "structs.h"
2 #include "mymemory.h"
3 #include "orderpair.h"
4 #include "tableentry.h"
5 #include "ordernode.h"
6 #include "orderedge.h"
7
8 VectorImpl(Table, Table *, 4);
9 VectorImpl(Set, Set *, 4);
10 VectorImpl(Boolean, Boolean *, 4);
11 VectorImpl(Function, Function *, 4);
12 VectorImpl(Predicate, Predicate *, 4);
13 VectorImpl(Element, Element *, 4);
14 VectorImpl(Order, Order *, 4);
15 VectorImpl(TableEntry, TableEntry *, 4);
16 VectorImpl(ASTNode, ASTNode *, 4);
17 VectorImpl(Int, uint64_t, 4);
18
19 inline unsigned int Ptr_hash_function(void * hash) {
20         return (unsigned int)((int64)hash >> 4);
21 }
22
23 inline bool Ptr_equals(void * key1, void * key2) {
24         return key1 == key2;
25 }
26
27 static inline unsigned int order_pair_hash_Function(OrderPair* This){
28         return (uint) (This->first << 2) ^ This->second;
29 }
30
31 static inline unsigned int order_pair_equals(OrderPair* key1, OrderPair* key2){
32         return key1->first== key2->first && key1->second == key2->second;
33 }
34
35 static inline unsigned int table_entry_hash_Function(TableEntry* This){
36         //http://isthe.com/chongo/tech/comp/fnv/
37         unsigned int h = 2166136261;
38         const unsigned int FNV_PRIME = 16777619;
39         for(uint i=0; i<This->inputSize; i++){
40                 h ^= This->inputs[i];
41                 h *= FNV_PRIME;
42         }
43         return h;
44 }
45
46 static inline bool table_entry_equals(TableEntry* key1, TableEntry* key2){
47         if(key1->inputSize != key2->inputSize)
48                 return false;
49         for(uint i=0; i<key1->inputSize; i++)
50                 if(key1->inputs[i]!=key2->inputs[i])
51                         return false;
52         return true;
53 }
54
55 static inline unsigned int order_node_hash_Function(OrderNode* This){
56         return (uint) ((int64)This->order << 2) ^ This->id;
57         
58 }
59
60 static inline bool order_node_equals(OrderNode* key1, OrderNode* key2){
61         return key1->id == key2->id && key1->order == key2->order;
62 }
63
64 static inline unsigned int order_edge_hash_Function(OrderEdge* This){
65         return (uint) (( (int64)This->sink << 2)^((int64)This->source << 6) ) ^ (int64)This->order;
66         
67 }
68
69 static inline bool order_edge_equals(OrderEdge* key1, OrderEdge* key2){
70         return key1->sink == key2->sink && key1->source == key2->source && key1->order == key2->order;
71 }
72
73 HashTableImpl(OrderPair, OrderPair *, OrderPair *, order_pair_hash_Function, order_pair_equals, ourfree);
74 HashSetImpl(TableEntry, TableEntry*, table_entry_hash_Function, table_entry_equals);
75 HashSetImpl(OrderNode, OrderNode*, order_node_hash_Function, order_node_equals);
76 HashSetImpl(OrderEdge, OrderEdge*, order_edge_hash_Function, order_edge_equals);
77
78