Table.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 #ifndef H_COEDEUTIL_DICTIONARY_INCLUDED
34 #define H_COEDEUTIL_DICTIONARY_INCLUDED
35 
36 #include <cstddef>
37 #include <vector>
38 #include <stack>
39 #include <utility>
40 #include <cstdint>
41 
42 #include <iostream>
43 namespace Dynacoe {
44 
45 
46 
47 
48 
49 
50 class LookupID;
51 
57 template<typename T>
58 class Table {
59  public:
60  Table();
61  ~Table();
62 
66  LookupID Insert(T);
67 
70  void Remove(const LookupID &);
71 
72 
75  bool Query(const LookupID &);
76 
81  T & Find(LookupID);
82 
85  std::vector<T> List();
86 
87 
88  private:
89  uint64_t thisID;
90 
91  std::stack<size_t> deadList;
92  std::vector<T> entryList;
93  std::vector<bool> entryValid;
94 
95 
96 };
97 
98 
99 
100 
101 class LookupID {
102 public:
103  LookupID();
104 
105 
106  // Returns whether or not this LookupID refers to a valid data entry in a table
107  bool Valid() const;
108 
109  bool operator==(const LookupID & c) const;
110  bool operator!=(const LookupID & c) const;
111 
112  friend bool operator<(const LookupID & l, const LookupID & r) {
113  if (l.TableID == r.TableID) {
114  return l.lookupID < r.lookupID;
115  }
116  return false;
117  }
118 
119  private:
120  template<typename T>
121  friend class Table;
122 
123 
124  uint64_t TableID;
125  uint64_t lookupID;
126 
127  LookupID(size_t TableID_, size_t id_) :
128  TableID(TableID_),
129  lookupID(id_){}
130 };
131 
132 
133 
134 
135 
136 
138 
139 
140 
141 // private container class for Table<T>
142 class LookupLibrary {
143 
144  template<typename T>
145  friend class Table;
146 
147  friend class LookupID;
148 
149  // initializes the LookupLibrary assets. If already inited, does nothing
150  static void AssertOpen();
151 
152  // Returns if two ids refer to the same physical table
153  static bool IsTableAlias(uint64_t a, uint64_t b);
154 
155  // Assign a new id for the table pointer to by the pointer
156  static uint64_t AcquireTableID(void *);
157 
158  // Returns the Table instance for the ID given.
159  static void * CurrentTableID(uint64_t);
160 
161  // dissasociates all assets of the table
162  static void RemoveTable(void *);
163 
164 
165  // Claims the data slot for a new piece fo data. The lookupID belonging to the
166  // table id is returned
167  static uint64_t MapTableData(uint64_t TableID, uint64_t dataSlot);
168 
169  // Dissasociates the lookup id from the table id
170  static void UnmapTableData(uint64_t TableID, uint64_t lookupID);
171 
172  // Returns whether or not the tableID-lookupID combo refers to a slot held by a Table instance.
173  static bool IsDataMapped(uint64_t TableID, uint64_t lookupID);
174 
175  // Returns the data slot of the owning table-lookupID combo
176  static uint64_t GetDataSlot(uint64_t TableID, uint64_t lookupID);
177 
178 
179 
180 };
181 
182 
183 
184 template<typename T>
185 Table<T>::Table() {
186  LookupLibrary::AssertOpen();
187  thisID = LookupLibrary::AcquireTableID(this);
188 }
189 
190 template<typename T>
191 Table<T>::~Table() {
192  LookupLibrary::RemoveTable(this);
193 }
194 
195 template<typename T>
196 LookupID Table<T>::Insert(T item) {
197 
198  if (!deadList.empty()) {
199  size_t id = deadList.top();
200  entryList[id] = item;
201  entryValid[id] = true;
202  deadList.pop();
203  return LookupID(thisID, LookupLibrary::MapTableData(thisID, id));
204  }
205  entryList.push_back(item);
206  entryValid.push_back(true);
207  LookupID out(thisID, LookupLibrary::MapTableData(thisID, entryList.size() - 1));
208 
209 
210 
211  return out;
212 }
213 
214 template<typename T>
215 void Table<T>::Remove(const LookupID & id) {
216  if (!id.Valid()) return;
217 
218 
219  uint64_t vectorSlot = LookupLibrary::GetDataSlot(id.TableID, id.lookupID);
220  entryValid[vectorSlot] = false;
221  deadList.push(vectorSlot);
222  LookupLibrary::UnmapTableData(id.TableID, id.lookupID);
223 }
224 
225 
226 template<typename T>
227 bool Table<T>::Query(const LookupID & id) {
228  if (!id.Valid()) return false;
229 
230  return LookupLibrary::IsTableAlias(id.TableID, thisID);
231 }
232 
233 template<typename T>
234 T & Table<T>::Find(LookupID id) {
235  if (!id.Valid()) {
236  T * out = 0x0;
237  return *out;
238  }; // have fun, you dummy
239  return entryList[LookupLibrary::GetDataSlot(thisID, id.lookupID)];
240 }
241 
242 
243 
244 template<typename T>
245 std::vector<T> Table<T>::List() {
246  std::vector<T> out;
247  for(size_t i = 0; i < entryList.size(); ++i) {
248  if (entryValid[i]) {
249  out.push_back(entryList[i]);
250  }
251  }
252  return out;
253 }
254 
255 
256 }
257 
258 #endif
Definition: AssetID.h:37
T & Find(LookupID)
Finds an entry based on the ID.
Definition: Table.h:234
LookupID Insert(T)
Adds an entry to the lookup table. A lookupID is returned.
Definition: Table.h:196
void Remove(const LookupID &)
Dissasociates an ID from an entry.
Definition: Table.h:215
std::vector< T > List()
Returns a std::vector holding each member added.
Definition: Table.h:245
An associative container that issues each member an ID guaranteed to be unique for the duration of th...
Definition: Table.h:58
bool Query(const LookupID &)
Returns whehter or not the ID is owned by this table.
Definition: Table.h:227