67b32a3e147f9d65f3bb5aed947a23d375be4c02
[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(BooleanOrder, BooleanOrder *, 4);
13 VectorImpl(Function, Function *, 4);
14 VectorImpl(Predicate, Predicate *, 4);
15 VectorImpl(Element, Element *, 4);
16 VectorImpl(Order, Order *, 4);
17 VectorImpl(TableEntry, TableEntry *, 4);
18 VectorImpl(ASTNode, ASTNode *, 4);
19 VectorImpl(Int, uint64_t, 4);
20 VectorImpl(OrderNode, OrderNode *, 4);
21 VectorImpl(OrderGraph, OrderGraph *, 4);
22
23 static inline unsigned int Ptr_hash_function(void *hash) {
24         return (unsigned int)((int64)hash >> 4);
25 }
26
27 static inline bool Ptr_equals(void *key1, void *key2) {
28         return key1 == key2;
29 }
30
31 static inline unsigned int order_pair_hash_function(OrderPair *This) {
32         return (uint) (This->first << 2) ^ This->second;
33 }
34
35 static inline unsigned int order_pair_equals(OrderPair *key1, OrderPair *key2) {
36         return key1->first == key2->first && key1->second == key2->second;
37 }
38
39 static inline unsigned int table_entry_hash_function(TableEntry *This) {
40         unsigned int h = 0;
41         for (uint i = 0; i < This->inputSize; i++) {
42                 h += This->inputs[i];
43                 h *= 31;
44         }
45         return h;
46 }
47
48 static inline bool table_entry_equals(TableEntry *key1, TableEntry *key2) {
49         if (key1->inputSize != key2->inputSize)
50                 return false;
51         for (uint i = 0; i < key1->inputSize; i++)
52                 if (key1->inputs[i] != key2->inputs[i])
53                         return false;
54         return true;
55 }
56
57 static inline unsigned int order_node_hash_function(OrderNode *This) {
58         return (uint) This->id;
59
60 }
61
62 static inline bool order_node_equals(OrderNode *key1, OrderNode *key2) {
63         return key1->id == key2->id;
64 }
65
66 static inline unsigned int order_edge_hash_function(OrderEdge *This) {
67         return (uint) (((uintptr_t)This->sink) ^ ((uintptr_t)This->source << 4));
68 }
69
70 static inline bool order_edge_equals(OrderEdge *key1, OrderEdge *key2) {
71         return key1->sink == key2->sink && key1->source == key2->source;
72 }
73
74 HashTableImpl(OrderPair, OrderPair *, OrderPair *, order_pair_hash_function, order_pair_equals, ourfree);
75
76 HashSetImpl(Boolean, Boolean *, Ptr_hash_function, Ptr_equals);
77 HashSetImpl(TableEntry, TableEntry *, table_entry_hash_function, table_entry_equals);
78 HashSetImpl(OrderNode, OrderNode *, order_node_hash_function, order_node_equals);
79 HashSetImpl(OrderEdge, OrderEdge *, order_edge_hash_function, order_edge_equals);
80
81 HashTableImpl(NodeToNodeSet, OrderNode *, HashSetOrderNode *, Ptr_hash_function, Ptr_equals, deleteHashSetOrderNode);