Switch to vector class
[satune.git] / src / Collections / structs.cc
1 #include "mymemory.h"
2 #include "structs.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 static inline unsigned int Ptr_hash_function(void *hash) {
11         return (unsigned int)((int64)hash >> 4);
12 }
13
14 static inline bool Ptr_equals(void *key1, void *key2) {
15         return key1 == key2;
16 }
17
18 static inline unsigned int order_pair_hash_function(OrderPair *This) {
19         return (uint) (This->first << 2) ^ This->second;
20 }
21
22 static inline unsigned int order_pair_equals(OrderPair *key1, OrderPair *key2) {
23         return key1->first == key2->first && key1->second == key2->second;
24 }
25
26 static inline unsigned int table_entry_hash_function(TableEntry *This) {
27         unsigned int h = 0;
28         for (uint i = 0; i < This->inputSize; i++) {
29                 h += This->inputs[i];
30                 h *= 31;
31         }
32         return h;
33 }
34
35 static inline bool table_entry_equals(TableEntry *key1, TableEntry *key2) {
36         if (key1->inputSize != key2->inputSize)
37                 return false;
38         for (uint i = 0; i < key1->inputSize; i++)
39                 if (key1->inputs[i] != key2->inputs[i])
40                         return false;
41         return true;
42 }
43
44 static inline unsigned int order_node_hash_function(OrderNode *This) {
45         return (uint) This->id;
46
47 }
48
49 static inline bool order_node_equals(OrderNode *key1, OrderNode *key2) {
50         return key1->id == key2->id;
51 }
52
53 static inline unsigned int order_edge_hash_function(OrderEdge *This) {
54         return (uint) (((uintptr_t)This->sink) ^ ((uintptr_t)This->source << 4));
55 }
56
57 static inline bool order_edge_equals(OrderEdge *key1, OrderEdge *key2) {
58         return key1->sink == key2->sink && key1->source == key2->source;
59 }
60
61 static inline unsigned int order_element_hash_function(OrderElement* This) {
62         return (uint)This->item;
63 }
64
65 static inline bool order_element_equals(OrderElement* key1, OrderElement* key2) {
66         return key1->item == key2->item;
67 }
68
69
70 HashSetImpl(Boolean, Boolean *, Ptr_hash_function, Ptr_equals);
71 HashSetImpl(TableEntry, TableEntry *, table_entry_hash_function, table_entry_equals);
72 HashSetImpl(OrderNode, OrderNode *, order_node_hash_function, order_node_equals);
73 HashSetImpl(OrderEdge, OrderEdge *, order_edge_hash_function, order_edge_equals);
74 HashSetImpl(OrderElement, OrderElement *, order_element_hash_function, order_element_equals);
75
76 HashTableImpl(NodeToNodeSet, OrderNode *, HashSetOrderNode *, Ptr_hash_function, Ptr_equals, deleteHashSetOrderNode);
77 HashTableImpl(OrderPair, OrderPair *, OrderPair *, order_pair_hash_function, order_pair_equals, ourfree);