1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 ****************************************************************************/
24 #ifndef SWRLIB_CONTAINERS_HPP__
25 #define SWRLIB_CONTAINERS_HPP__
28 #include "common/os.h"
33 template <typename T, int NUM_ELEMENTS>
34 struct UncheckedFixedVector
36 UncheckedFixedVector() : mSize(0)
40 UncheckedFixedVector(std::size_t size, T const& exemplar)
43 for (std::size_t i = 0; i < size; ++i)
44 this->push_back(exemplar);
47 template <typename Iter>
48 UncheckedFixedVector(Iter fst, Iter lst)
51 for ( ; fst != lst; ++fst)
52 this->push_back(*fst);
55 UncheckedFixedVector(UncheckedFixedVector const& UFV)
58 for (std::size_t i = 0, N = UFV.size(); i < N; ++i)
60 this->mSize = UFV.size();
63 UncheckedFixedVector& operator=(UncheckedFixedVector const& UFV)
65 for (std::size_t i = 0, N = UFV.size(); i < N; ++i)
67 this->mSize = UFV.size();
71 T* begin() { return &this->mElements[0]; }
72 T* end() { return &this->mElements[0] + this->mSize; }
73 T const* begin() const { return &this->mElements[0]; }
74 T const* end() const { return &this->mElements[0] + this->mSize; }
76 friend bool operator==(UncheckedFixedVector const& L, UncheckedFixedVector const& R)
78 if (L.size() != R.size()) return false;
79 for (std::size_t i = 0, N = L.size(); i < N; ++i)
81 if (L[i] != R[i]) return false;
86 friend bool operator!=(UncheckedFixedVector const& L, UncheckedFixedVector const& R)
88 if (L.size() != R.size()) return true;
89 for (std::size_t i = 0, N = L.size(); i < N; ++i)
91 if (L[i] != R[i]) return true;
96 T& operator[](std::size_t idx)
98 return this->mElements[idx];
100 T const& operator[](std::size_t idx) const
102 return this->mElements[idx];
104 void push_back(T const& t)
106 this->mElements[this->mSize] = t;
111 SWR_ASSERT(this->mSize > 0);
116 return this->mElements[this->mSize-1];
118 T const& back() const
120 return this->mElements[this->mSize-1];
124 return this->mSize == 0;
126 std::size_t size() const
130 void resize(std::size_t sz)
139 std::size_t mSize{ 0 };
140 T mElements[NUM_ELEMENTS];
143 template <typename T, int NUM_ELEMENTS>
144 struct FixedStack : UncheckedFixedVector<T, NUM_ELEMENTS>
148 void push(T const& t)
169 template <typename T>
172 static_assert((sizeof(T) % sizeof(UINT)) == 0, "CRCHash expects templated type size is even multiple of 4B");
173 UINT operator()(const T& k) const
175 UINT *pData = (UINT*)&k;
177 for (UINT i = 0; i < sizeof(T) / sizeof(UINT); ++i)
179 crc = _mm_crc32_u32(crc, pData[i]);
190 template <typename T, int N>
191 struct hash<SWRL::UncheckedFixedVector<T, N>>
193 size_t operator() (SWRL::UncheckedFixedVector<T, N> const& v) const
195 if (v.size() == 0) return 0;
198 if (v.size() == 1) return x;
199 for (size_t i = 1; i < v.size(); ++i)
200 x ^= H(v[i]) + 0x9e3779b9 + (x<<6) + (x>>2);
208 #endif//SWRLIB_CONTAINERS_HPP__