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