DSC Engine
Loading...
Searching...
No Matches
hash_set.hpp
1#pragma once
2
3#include "DSCEngine/types/hash.hpp"
5#include "DSCEngine/debug/assert.hpp"
6
7namespace DSC
8{
9 template<typename T, int (*H)(const T&) = default_hash<T, 128>, int S = 128>
10 class HashSet
11 {
12 private:
13 Vector<T> container[S];
14 public:
15 void add(const T& item);
16 void remove(const T& item);
17 bool includes(const T& item) const;
18 };
19
20 template<typename T, int (*H)(const T&), int S>
21 void HashSet<T,H,S>::add(const T& item)
22 {
23 int h = H(item);
24 nds_assert(0<=h && h<S);
25
26 bool found = false;
27 for(int i=0;i<container[h].size() && !found;i++)
28 found = (container[h][i]==item);
29
30 if(found) return;
31 container[h].push_back(item);
32 }
33
34 template<typename T, int (*H)(const T&), int S>
35 void HashSet<T,H,S>::remove(const T& item)
36 {
37 int h = H(item);
38 nds_assert(0<=h && h<S);
39 container[h].remove(item);
40 }
41
42 template<typename T, int (*H)(const T&), int S>
43 bool HashSet<T,H,S>::includes(const T& item) const
44 {
45 int h = H(item);
46 nds_assert(0<=h && h<S);
47
48 for(int i=0;i<container[h].size();i++)
49 if(container[h][i]==item)
50 return true;
51 return false;
52 }
53}
Definition: hash_set.hpp:11
Generic dynamic vector.
Definition: vector.hpp:23
Generic dynamic vector definition.