Adding support for Integer Encoding ...
[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 #include "orderelement.h"
9
10 VectorImpl(Table, Table *, 4);
11 VectorImpl(Set, Set *, 4);
12 VectorImpl(Boolean, Boolean *, 4);
13 VectorImpl(BooleanOrder, BooleanOrder *, 4);
14 VectorImpl(Function, Function *, 4);
15 VectorImpl(Predicate, Predicate *, 4);
16 VectorImpl(Element, Element *, 4);
17 VectorImpl(Order, Order *, 4);
18 VectorImpl(TableEntry, TableEntry *, 4);
19 VectorImpl(ASTNode, ASTNode *, 4);
20 VectorImpl(Int, uint64_t, 4);
21 VectorImpl(OrderNode, OrderNode *, 4);
22 VectorImpl(OrderGraph, OrderGraph *, 4);
23
24 static inline unsigned int Ptr_hash_function(void *hash) {
25         return (unsigned int)((int64)hash >> 4);
26 }
27
28 static inline bool Ptr_equals(void *key1, void *key2) {
29         return key1 == key2;
30 }
31
32 static inline unsigned int order_pair_hash_function(OrderPair *This) {
33         return (uint) (This->first << 2) ^ This->second;
34 }
35
36 static inline unsigned int order_pair_equals(OrderPair *key1, OrderPair *key2) {
37         return key1->first == key2->first && key1->second == key2->second;
38 }
39
40 static inline unsigned int table_entry_hash_function(TableEntry *This) {
41         unsigned int h = 0;
42         for (uint i = 0; i < This->inputSize; i++) {
43                 h += This->inputs[i];
44                 h *= 31;
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) This->id;
60
61 }
62
63 static inline bool order_node_equals(OrderNode *key1, OrderNode *key2) {
64         return key1->id == key2->id;
65 }
66
67 static inline unsigned int order_edge_hash_function(OrderEdge *This) {
68         return (uint) (((uintptr_t)This->sink) ^ ((uintptr_t)This->source << 4));
69 }
70
71 static inline bool order_edge_equals(OrderEdge *key1, OrderEdge *key2) {
72         return key1->sink == key2->sink && key1->source == key2->source;
73 }
74
75 static inline unsigned int order_element_hash_function(OrderElement* This) {
76         return (uint)This->item;
77 }
78
79 static inline bool order_element_equals(OrderElement* key1, OrderElement* key2) {
80         return key1->item == key2->item;
81 }
82
83
84 HashSetImpl(Boolean, Boolean *, Ptr_hash_function, Ptr_equals);
85 HashSetImpl(TableEntry, TableEntry *, table_entry_hash_function, table_entry_equals);
86 HashSetImpl(OrderNode, OrderNode *, order_node_hash_function, order_node_equals);
87 HashSetImpl(OrderEdge, OrderEdge *, order_edge_hash_function, order_edge_equals);
88 HashSetImpl(OrderElement, OrderElement *, order_element_hash_function, order_element_equals);
89
90 HashTableImpl(NodeToNodeSet, OrderNode *, HashSetOrderNode *, Ptr_hash_function, Ptr_equals, deleteHashSetOrderNode);
91 HashTableImpl(OrderPair, OrderPair *, OrderPair *, order_pair_hash_function, order_pair_equals, ourfree);