1 /*
2 Copyright (c) 2013-2017 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module dmech.hashtable;
30 
31 import dlib.core.ownership;
32 import dlib.core.memory;
33 
34 class HashTable(T, K): Owner
35 {
36     struct Entry(T)
37     {
38         K key;
39         T value;
40         bool valid = false;
41     }
42 
43     const size_t size;
44     Entry!(T)[] table;
45 
46     this(Owner o, size_t size)
47     {
48         super(o);
49         this.size = size;
50         table = New!(Entry!(T)[])(size);
51     }
52 
53     T* get(K key)
54     {
55         uint hash = (key % size);
56 
57         while (table[hash].valid && table[hash].key != key)
58             hash = (hash + 1) % size;
59 
60         if (!table[hash].valid)
61             return null;
62         else
63             return &table[hash].value;
64     }
65 
66     void set(K key, T value)
67     {
68         uint hash = (key % size);
69 
70         while (table[hash].valid && table[hash].key != key)
71             hash = (hash + 1) % size;
72 
73         table[hash] = Entry!(T)(key, value, true);
74     }
75 
76     void remove(K key)
77     {
78         uint hash = (key % size);
79 
80         while (table[hash].valid && table[hash].key != key)
81             hash = (hash + 1) % size;
82 
83         table[hash].valid = false;
84     }
85 
86     int opApply(int delegate(ref T) func)
87     {
88         int result = 0;
89 
90         foreach(ref entry; table)
91         {
92             if (entry.valid)
93             {
94                 result = func(entry.value);
95                 if (result)
96                     break;
97             }
98         }
99 
100         return result;
101     }
102 
103     ~this()
104     {
105         Delete(table);
106     }
107 }