2 #ifndef GAINPUTCONTAINERS_H_ 3 #define GAINPUTCONTAINERS_H_ 15 inline uint32_t rotl32(uint32_t x, int8_t r)
17 return (x << r) | (x >> (32 - r));
20 inline uint32_t getblock(
const uint32_t * p,
int i)
26 inline uint32_t fmix(uint32_t h)
46 const uint8_t * data = (
const uint8_t*)key;
47 const int nblocks = len / 4;
51 const uint32_t c1 = 0xcc9e2d51;
52 const uint32_t c2 = 0x1b873593;
54 const uint32_t * blocks = (
const uint32_t *)(data + nblocks*4);
56 for(
int i = -nblocks; i; i++)
58 uint32_t k1 = getblock(blocks,i);
69 const uint8_t * tail = (
const uint8_t*)(data + nblocks*4);
75 case 3: k1 ^= tail[2] << 16;
76 case 2: k1 ^= tail[1] << 8;
77 case 1: k1 ^= tail[0];
78 k1 *= c1; k1 = rotl32(k1,15); k1 *= c2; h1 ^= k1;
99 static const size_t DefaultCapacity = 8;
102 typedef const T* const_iterator;
103 typedef T value_type;
106 allocator_(allocator),
110 data_ =
static_cast<T*
>(allocator_.Allocate(
sizeof(T)*capacity_));
115 allocator_.Delete(data_);
118 iterator begin() {
return data_; }
119 const_iterator begin()
const {
return data_; }
120 iterator end() {
return data_ + size_; }
121 const_iterator end()
const {
return data_ + size_; }
123 T& operator[] (
size_t i)
125 GAINPUT_ASSERT(i < size_);
129 const T& operator[] (
size_t i)
const 131 GAINPUT_ASSERT(i < size_);
135 void push_back(
const value_type& val)
137 if (size_ + 1 > capacity_)
141 data_[size_++] = val;
150 void reserve(
size_t capacity)
152 if (capacity <= capacity_)
154 capacity = (capacity_*2) < capacity ? capacity : (capacity_*2);
155 T* newData =
static_cast<T*
>(allocator_.Allocate(
sizeof(T)*capacity));
156 memcpy(newData, data_,
sizeof(T)*capacity_);
157 allocator_.Deallocate(data_);
159 capacity_ = capacity;
164 GAINPUT_ASSERT(&allocator_ == &x.allocator_);
166 const size_t thisSize = size_;
167 const size_t capacity = capacity_;
171 capacity_ = x.capacity_;
175 x.capacity_ = capacity;
179 iterator erase(iterator pos)
183 GAINPUT_ASSERT(pos >= begin() && pos < end());
184 memcpy(pos, pos+1,
sizeof(T)*(end()-(pos+1)));
189 void clear() { size_ = 0; }
191 bool empty()
const {
return size_ == 0; }
192 size_t size()
const {
return size_; }
194 iterator find(
const value_type& val)
196 for (
size_t i = 0; i < size_; ++i)
206 const_iterator find(
const value_type& val)
const 208 for (
size_t i = 0; i < size_; ++i)
231 template<
class K,
class V>
235 static const unsigned Seed = 329856235;
236 enum { InvalidKey = unsigned(-1) };
251 allocator_(allocator),
257 iterator begin() {
return values_.begin(); }
258 const_iterator begin()
const {
return values_.begin(); }
259 iterator end() {
return values_.begin() + values_.size(); }
260 const_iterator end()
const {
return values_.begin() + values_.size(); }
262 size_t size()
const {
return size_; }
263 bool empty()
const {
return size_ == 0; }
265 size_t count(
const K& k)
const 267 return find(k) != end() ? 1 : 0;
270 iterator find(
const K& k)
272 if (keys_.empty() || values_.empty())
276 const uint32_t ha = h % keys_.size();
277 volatile uint32_t vi = keys_[ha];
278 while (vi != InvalidKey)
280 if (values_[vi].first == k)
284 vi = values_[vi].next;
289 const_iterator find(
const K& k)
const 291 if (keys_.empty() || values_.empty())
295 const uint32_t ha = h % keys_.size();
296 volatile uint32_t vi = keys_[ha];
297 while (vi != InvalidKey)
299 if (values_[vi].first == k)
303 vi = values_[vi].next;
308 iterator insert(
const K& k,
const V& v)
310 if (values_.size() >= 0.6f*keys_.size())
312 Rehash(values_.size()*2 + 10);
317 const uint32_t ha = h % keys_.size();
318 uint32_t vi = keys_[ha];
320 if (vi == InvalidKey)
322 keys_[ha] = values_.size();
328 if (values_[vi].next == InvalidKey)
330 values_[vi].next = values_.size();
335 vi = values_[vi].next;
343 node.
next = InvalidKey;
344 values_.push_back(node);
348 return &values_[values_.size()-1];
351 V& operator[] (
const K& k)
353 iterator it = find(k);
356 return insert(k, V())->second;
364 size_t erase(
const K& k)
370 const uint32_t ha = h % keys_.size();
371 uint32_t vi = keys_[ha];
372 uint32_t prevVi = InvalidKey;
373 while (vi != InvalidKey)
375 if (values_[vi].first == k)
377 if (prevVi == InvalidKey)
379 keys_[ha] = values_[vi].next;
383 values_[prevVi].next = values_[vi].next;
387 if (vi == values_.size() - 1)
394 size_t lastVi = values_.size()-1;
395 values_[vi] = values_[lastVi];
406 for (
typename Array<Node>::iterator it = values_.begin(); it != values_.end(); ++it)
408 if (it->next == lastVi)
418 vi = values_[vi].next;
435 void Rehash(
size_t newSize)
440 for (
size_t i = 0; i < newSize; ++i)
441 keys.push_back(InvalidKey);
444 values_.swap(values);
447 for (
typename Array<Node>::const_iterator it = values.begin();
451 insert(it->first, it->second);
464 template<
int N,
class T>
476 return nextRead_ < nextWrite_;
479 size_t GetCount()
const 481 const size_t d = nextWrite_ - nextRead_;
482 return d > N ? N : d;
487 return buf_[(nextRead_++) % N];
492 buf_[(nextWrite_++) % N] = d;
493 while (nextRead_ + N < nextWrite_)