swr: [rasterizer] code styling and update copyrights
[mesa.git] / src / gallium / drivers / swr / rasterizer / common / containers.hpp
1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 ****************************************************************************/
23
24 #ifndef SWRLIB_CONTAINERS_HPP__
25 #define SWRLIB_CONTAINERS_HPP__
26
27 #include <functional>
28 #include "common/os.h"
29
30 namespace SWRL
31 {
32
33 template <typename T, int NUM_ELEMENTS>
34 struct UncheckedFixedVector
35 {
36 UncheckedFixedVector() : mSize(0)
37 {
38 }
39
40 UncheckedFixedVector(std::size_t size, T const& exemplar)
41 {
42 this->mSize = 0;
43 for (std::size_t i = 0; i < size; ++i)
44 this->push_back(exemplar);
45 }
46
47 template <typename Iter>
48 UncheckedFixedVector(Iter fst, Iter lst)
49 {
50 this->mSize = 0;
51 for ( ; fst != lst; ++fst)
52 this->push_back(*fst);
53 }
54
55 UncheckedFixedVector(UncheckedFixedVector const& UFV)
56 {
57 this->mSize = 0;
58 for (std::size_t i = 0, N = UFV.size(); i < N; ++i)
59 (*this)[i] = UFV[i];
60 this->mSize = UFV.size();
61 }
62
63 UncheckedFixedVector& operator=(UncheckedFixedVector const& UFV)
64 {
65 for (std::size_t i = 0, N = UFV.size(); i < N; ++i)
66 (*this)[i] = UFV[i];
67 this->mSize = UFV.size();
68 return *this;
69 }
70
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; }
75
76 friend bool operator==(UncheckedFixedVector const& L, UncheckedFixedVector const& R)
77 {
78 if (L.size() != R.size()) return false;
79 for (std::size_t i = 0, N = L.size(); i < N; ++i)
80 {
81 if (L[i] != R[i]) return false;
82 }
83 return true;
84 }
85
86 friend bool operator!=(UncheckedFixedVector const& L, UncheckedFixedVector const& R)
87 {
88 if (L.size() != R.size()) return true;
89 for (std::size_t i = 0, N = L.size(); i < N; ++i)
90 {
91 if (L[i] != R[i]) return true;
92 }
93 return false;
94 }
95
96 T& operator[](std::size_t idx)
97 {
98 return this->mElements[idx];
99 }
100 T const& operator[](std::size_t idx) const
101 {
102 return this->mElements[idx];
103 }
104 void push_back(T const& t)
105 {
106 this->mElements[this->mSize] = t;
107 ++this->mSize;
108 }
109 void pop_back()
110 {
111 SWR_ASSERT(this->mSize > 0);
112 --this->mSize;
113 }
114 T& back()
115 {
116 return this->mElements[this->mSize-1];
117 }
118 T const& back() const
119 {
120 return this->mElements[this->mSize-1];
121 }
122 bool empty() const
123 {
124 return this->mSize == 0;
125 }
126 std::size_t size() const
127 {
128 return this->mSize;
129 }
130 void resize(std::size_t sz)
131 {
132 this->mSize = sz;
133 }
134 void clear()
135 {
136 this->resize(0);
137 }
138 private:
139 std::size_t mSize{ 0 };
140 T mElements[NUM_ELEMENTS];
141 };
142
143 template <typename T, int NUM_ELEMENTS>
144 struct FixedStack : UncheckedFixedVector<T, NUM_ELEMENTS>
145 {
146 FixedStack() {}
147
148 void push(T const& t)
149 {
150 this->push_back(t);
151 }
152
153 void pop()
154 {
155 this->pop_back();
156 }
157
158 T& top()
159 {
160 return this->back();
161 }
162
163 T const& top() const
164 {
165 return this->back();
166 }
167 };
168
169 template <typename T>
170 struct CRCHash
171 {
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
174 {
175 UINT *pData = (UINT*)&k;
176 UINT crc = 0;
177 for (UINT i = 0; i < sizeof(T) / sizeof(UINT); ++i)
178 {
179 crc = _mm_crc32_u32(crc, pData[i]);
180 }
181 return crc;
182 }
183 };
184
185 }// end SWRL
186
187 namespace std
188 {
189
190 template <typename T, int N>
191 struct hash<SWRL::UncheckedFixedVector<T, N>>
192 {
193 size_t operator() (SWRL::UncheckedFixedVector<T, N> const& v) const
194 {
195 if (v.size() == 0) return 0;
196 std::hash<T> H;
197 size_t x = H(v[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);
201 return x;
202 }
203 };
204
205
206 }// end std.
207
208 #endif//SWRLIB_CONTAINERS_HPP__