Visual Studio 2012 Support
[xy_vsfilter.git] / src / thirdparty / VirtualDub / h / vd2 / system / vdstl_hashtable.h
blob9cecbde653c6a1679dfbe27e9ab46243848edf32
1 // VirtualDub - Video processing and capture application
2 // System library component
3 // Copyright (C) 1998-2010 Avery Lee, All Rights Reserved.
4 //
5 // Beginning with 1.6.0, the VirtualDub system library is licensed
6 // differently than the remainder of VirtualDub. This particular file is
7 // thus licensed as follows (the "zlib" license):
8 //
9 // This software is provided 'as-is', without any express or implied
10 // warranty. In no event will the authors be held liable for any
11 // damages arising from the use of this software.
13 // Permission is granted to anyone to use this software for any purpose,
14 // including commercial applications, and to alter it and redistribute it
15 // freely, subject to the following restrictions:
17 // 1. The origin of this software must not be misrepresented; you must
18 // not claim that you wrote the original software. If you use this
19 // software in a product, an acknowledgment in the product
20 // documentation would be appreciated but is not required.
21 // 2. Altered source versions must be plainly marked as such, and must
22 // not be misrepresented as being the original software.
23 // 3. This notice may not be removed or altered from any source
24 // distribution.
26 #ifndef f_VD2_SYSTEM_VDSTL_HASHTABLE_H
27 #define f_VD2_SYSTEM_VDSTL_HASHTABLE_H
29 ///////////////////////////////////////////////////////////////////////////////
30 // vdhashtable_base_node
32 struct vdhashtable_base_node {
33 vdhashtable_base_node *mpHashNext;
36 ///////////////////////////////////////////////////////////////////////////////
37 // vdhashtable_base
39 class vdhashtable_base {
40 public:
41 typedef size_t size_type;
42 typedef ptrdiff_t difference_type;
44 vdhashtable_base();
46 // size and capacity
47 bool empty() const { return mElementCount == 0; }
48 size_type size() const { return mElementCount; }
49 size_type max_size() const { return (size_type)-1 >> 1; }
51 // bucket interface
52 size_type bucket_count() const;
53 size_type max_bucket_count() const;
54 size_type bucket_size(size_type n) const;
56 protected:
57 static size_type compute_bucket_count(size_type n);
59 size_type mBucketCount;
60 size_type mElementCount;
61 vdhashtable_base_node **mpBucketStart;
62 vdhashtable_base_node **mpBucketEnd;
64 static vdhashtable_base_node *const sEmptyBucket;
67 ///////////////////////////////////////////////////////////////////////////
69 template<class T>
70 struct vdhashtable_node : public vdhashtable_base_node {
71 T mData;
73 vdhashtable_node() {}
74 vdhashtable_node(vdhashtable_node<T> *next) : mData() {
75 mpHashNext = next;
78 vdhashtable_node(vdhashtable_node<T> *next, const T& val) : mData(val) {
79 mpHashNext = next;
83 ///////////////////////////////////////////////////////////////////////////
85 template<class T>
86 class vdhashtable_local_iterator {
87 public:
88 typedef std::forward_iterator_tag iterator_category;
89 typedef T value_type;
90 typedef T* pointer;
91 typedef T& reference;
92 typedef size_t size_type;
93 typedef ptrdiff_t difference_type;
95 vdhashtable_local_iterator(vdhashtable_base_node *node);
97 vdhashtable_local_iterator& operator++();
98 vdhashtable_local_iterator operator++(int);
100 T& operator*() const;
101 T* operator->() const;
103 private:
104 template<class U> friend bool operator==(const vdhashtable_local_iterator<U>& x, const vdhashtable_local_iterator<U>& y);
105 template<class U> friend bool operator==(const vdhashtable_local_iterator<const U>& x, const vdhashtable_local_iterator<U>& y);
106 template<class U> friend bool operator==(const vdhashtable_local_iterator<U>& x, const vdhashtable_local_iterator<const U>& y);
107 template<class U> friend bool operator!=(const vdhashtable_local_iterator<U>& x, const vdhashtable_local_iterator<U>& y);
108 template<class U> friend bool operator!=(const vdhashtable_local_iterator<const U>& x, const vdhashtable_local_iterator<U>& y);
109 template<class U> friend bool operator!=(const vdhashtable_local_iterator<U>& x, const vdhashtable_local_iterator<const U>& y);
111 vdhashtable_base_node *mpNode;
114 template<class T>
115 vdhashtable_local_iterator<T>::vdhashtable_local_iterator(vdhashtable_base_node *node)
116 : mpNode(node)
120 template<class T>
121 vdhashtable_local_iterator<T>& vdhashtable_local_iterator<T>::operator++() {
122 mpNode = mpNode->mpHashNext;
123 return *this;
126 template<class T>
127 vdhashtable_local_iterator<T> vdhashtable_local_iterator<T>::operator++(int) {
128 vdhashtable_local_iterator prev(*this);
129 mpNode = mpNode->mpHashNext;
130 return prev;
133 template<class T>
134 T& vdhashtable_local_iterator<T>::operator*() const {
135 return static_cast<vdhashtable_node<T> *>(mpNode)->mData;
138 template<class T>
139 T* vdhashtable_local_iterator<T>::operator->() const {
140 return &static_cast<vdhashtable_node<T> *>(mpNode)->mData;
143 template<class T>
144 bool operator==(const vdhashtable_local_iterator<T>& x, const vdhashtable_local_iterator<T>& y) {
145 return x.mpNode == y.mpNode;
148 template<class T>
149 bool operator==(const vdhashtable_local_iterator<const T>& x, const vdhashtable_local_iterator<T>& y) {
150 return x.mpNode == y.mpNode;
153 template<class T>
154 bool operator==(const vdhashtable_local_iterator<T>& x, const vdhashtable_local_iterator<const T>& y) {
155 return x.mpNode == y.mpNode;
158 template<class T>
159 bool operator!=(const vdhashtable_local_iterator<T>& x, const vdhashtable_local_iterator<T>& y) {
160 return x.mpNode != y.mpNode;
163 template<class T>
164 bool operator!=(const vdhashtable_local_iterator<const T>& x, const vdhashtable_local_iterator<T>& y) {
165 return x.mpNode != y.mpNode;
168 template<class T>
169 bool operator!=(const vdhashtable_local_iterator<T>& x, const vdhashtable_local_iterator<const T>& y) {
170 return x.mpNode != y.mpNode;
173 ///////////////////////////////////////////////////////////////////////////
175 template<class T> struct vdhashtable_iterator_nonconst {
176 typedef T result;
179 template<class T> struct vdhashtable_iterator_nonconst<const T> {
180 typedef T result;
183 template<class T>
184 class vdhashtable_iterator {
185 typedef typename vdhashtable_iterator_nonconst<T>::result T_NonConst;
186 public:
187 typedef std::forward_iterator_tag iterator_category;
188 typedef T value_type;
189 typedef T* pointer;
190 typedef T& reference;
191 typedef size_t size_type;
192 typedef ptrdiff_t difference_type;
194 vdhashtable_iterator();
195 vdhashtable_iterator(vdhashtable_base_node *node, vdhashtable_base_node **bucket, vdhashtable_base_node **bucketEnd);
196 vdhashtable_iterator(const vdhashtable_iterator<T_NonConst>& src);
198 vdhashtable_iterator& operator++();
199 vdhashtable_iterator operator++(int);
201 T& operator*() const;
202 T* operator->() const;
204 private:
205 friend class vdhashtable_iterator<const T>;
206 template<class U> friend bool operator==(const vdhashtable_iterator<U>& x, const vdhashtable_iterator<U>& y);
207 template<class U> friend bool operator==(const vdhashtable_iterator<const U>& x, const vdhashtable_iterator<U>& y);
208 template<class U> friend bool operator==(const vdhashtable_iterator<U>& x, const vdhashtable_iterator<const U>& y);
209 template<class U> friend bool operator!=(const vdhashtable_iterator<U>& x, const vdhashtable_iterator<U>& y);
210 template<class U> friend bool operator!=(const vdhashtable_iterator<const U>& x, const vdhashtable_iterator<U>& y);
211 template<class U> friend bool operator!=(const vdhashtable_iterator<U>& x, const vdhashtable_iterator<const U>& y);
213 vdhashtable_base_node *mpNode;
214 vdhashtable_base_node **mpBucket;
215 vdhashtable_base_node **mpBucketEnd;
218 template<class T>
219 vdhashtable_iterator<T>::vdhashtable_iterator()
220 : mpNode(NULL)
221 , mpBucket(NULL)
222 , mpBucketEnd(NULL)
226 template<class T>
227 vdhashtable_iterator<T>::vdhashtable_iterator(vdhashtable_base_node *node, vdhashtable_base_node **bucket, vdhashtable_base_node **bucketEnd)
228 : mpNode(node)
229 , mpBucket(bucket)
230 , mpBucketEnd(bucketEnd)
234 template<class T>
235 vdhashtable_iterator<T>::vdhashtable_iterator(const vdhashtable_iterator<T_NonConst>& src)
236 : mpNode(src.mpNode)
237 , mpBucket(src.mpBucket)
238 , mpBucketEnd(src.mpBucketEnd)
242 template<class T>
243 vdhashtable_iterator<T>& vdhashtable_iterator<T>::operator++() {
244 mpNode = mpNode->mpHashNext;
246 while(!mpNode && ++mpBucket != mpBucketEnd)
247 mpNode = static_cast<vdhashtable_node<T> *>(*mpBucket);
249 return *this;
252 template<class T>
253 vdhashtable_iterator<T> vdhashtable_iterator<T>::operator++(int) {
254 vdhashtable_iterator prev(*this);
255 mpNode = mpNode->mpHashNext;
256 return prev;
259 template<class T>
260 T& vdhashtable_iterator<T>::operator*() const {
261 return static_cast<vdhashtable_node<T> *>(mpNode)->mData;
264 template<class T>
265 T* vdhashtable_iterator<T>::operator->() const {
266 return &static_cast<vdhashtable_node<T> *>(mpNode)->mData;
269 template<class T>
270 bool operator==(const vdhashtable_iterator<T>& x, const vdhashtable_iterator<T>& y) {
271 return x.mpNode == y.mpNode;
274 template<class T>
275 bool operator==(const vdhashtable_iterator<const T>& x, const vdhashtable_iterator<T>& y) {
276 return x.mpNode == y.mpNode;
279 template<class T>
280 bool operator==(const vdhashtable_iterator<T>& x, const vdhashtable_iterator<const T>& y) {
281 return x.mpNode == y.mpNode;
284 template<class T>
285 bool operator!=(const vdhashtable_iterator<T>& x, const vdhashtable_iterator<T>& y) {
286 return x.mpNode != y.mpNode;
289 template<class T>
290 bool operator!=(const vdhashtable_iterator<const T>& x, const vdhashtable_iterator<T>& y) {
291 return x.mpNode != y.mpNode;
294 template<class T>
295 bool operator!=(const vdhashtable_iterator<T>& x, const vdhashtable_iterator<const T>& y) {
296 return x.mpNode != y.mpNode;
299 ///////////////////////////////////////////////////////////////////////////
301 template<class T>
302 class vdhashtable : public vdhashtable_base {
303 public:
304 typedef T value_type;
305 typedef vdhashtable_node<value_type> node_type;
306 typedef value_type *pointer;
307 typedef const value_type *const_pointer;
308 typedef value_type& reference;
309 typedef const value_type& const_reference;
310 typedef vdhashtable_iterator<value_type> iterator;
311 typedef vdhashtable_iterator<const value_type> const_iterator;
312 typedef vdhashtable_local_iterator<value_type> local_iterator;
313 typedef vdhashtable_local_iterator<const value_type> const_local_iterator;
315 // iterators
316 iterator begin();
317 const_iterator begin() const;
318 iterator end();
319 const_iterator end() const;
322 // iterators
323 template<class T>
324 typename vdhashtable<T>::iterator vdhashtable<T>::begin() {
325 vdhashtable_base_node **bucket = mpBucketStart;
326 vdhashtable_base_node *p = NULL;
328 while(bucket != mpBucketEnd) {
329 p = *bucket;
330 if (p)
331 break;
333 ++bucket;
336 return iterator(static_cast<node_type *>(p), bucket, mpBucketEnd);
339 template<class T>
340 typename vdhashtable<T>::const_iterator vdhashtable<T>::begin() const {
341 vdhashtable_base_node **bucket = mpBucketStart;
342 vdhashtable_base_node *p = NULL;
344 while(bucket != mpBucketEnd) {
345 p = *bucket;
346 if (p)
347 break;
349 ++bucket;
352 return const_iterator(static_cast<node_type *>(p), bucket, mpBucketEnd);
355 template<class T>
356 typename vdhashtable<T>::iterator vdhashtable<T>::end() {
357 return iterator(NULL, NULL, NULL);
360 template<class T>
361 typename vdhashtable<T>::const_iterator vdhashtable<T>::end() const {
362 return const_iterator(NULL, NULL, NULL);
365 #endif // f_VD2_SYSTEM_VDSTL_HASHTABLE_H