Fix T49078: Cycles tries to render volume from another render layer when camera is...
[blender.git] / intern / container / CTR_Map.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file container/CTR_Map.h
29  *  \ingroup ctr
30  */
31
32 #ifndef __CTR_MAP_H__
33 #define __CTR_MAP_H__
34
35 template <class Key, class Value>
36 class CTR_Map {
37 private:
38         struct Entry {
39                 Entry (Entry *next, Key key, Value value) :
40                         m_next(next),
41                         m_key(key),
42                         m_value(value) {
43                 }
44
45                 Entry *m_next;
46                 Key m_key;
47                 Value m_value;
48         };
49
50 public:
51         CTR_Map(int num_buckets = 100) : m_num_buckets(num_buckets) {
52                 m_buckets = new Entry *[num_buckets];
53                 for (int i = 0; i < num_buckets; ++i) {
54                         m_buckets[i] = 0;
55                 }
56         }
57
58         CTR_Map(const CTR_Map& map)
59         {
60                 m_num_buckets = map.m_num_buckets;
61                 m_buckets = new Entry *[m_num_buckets];
62
63                 for (int i = 0; i < m_num_buckets; ++i) {
64                         m_buckets[i] = 0;
65
66                         for (Entry *entry = map.m_buckets[i]; entry; entry = entry->m_next) {
67                                 insert(entry->m_key, entry->m_value);
68                         }
69                 }
70         }
71
72         int size()
73         {
74                 int count = 0;
75                 for (int i = 0; i < m_num_buckets; i++) {
76                         Entry *bucket = m_buckets[i];
77                         while (bucket) {
78                                 bucket = bucket->m_next;
79                                 count++;
80                         }
81                 }
82                 return count;
83         }
84
85         Value *at(int index)
86         {
87                 int count = 0;
88                 for (int i = 0; i < m_num_buckets; i++) {
89                         Entry *bucket = m_buckets[i];
90                         while (bucket) {
91                                 if (count == index) {
92                                         return &bucket->m_value;
93                                 }
94                                 bucket = bucket->m_next;
95                                 count++;
96                         }
97                 }
98                 return 0;
99         }
100
101         Key *getKey(int index)
102         {
103                 int count = 0;
104                 for (int i = 0; i < m_num_buckets; i++) {
105                         Entry *bucket = m_buckets[i];
106                         while (bucket) {
107                                 if (count == index) {
108                                         return &bucket->m_key;
109                                 }
110                                 bucket = bucket->m_next;
111                                 count++;
112                         }
113                 }
114                 return 0;
115         }
116
117         void clear()
118         {
119                 for (int i = 0; i < m_num_buckets; ++i) {
120                         Entry *entry_ptr = m_buckets[i];
121
122                         while (entry_ptr != 0) {
123                                 Entry *tmp_ptr = entry_ptr->m_next;
124                                 delete entry_ptr;
125                                 entry_ptr = tmp_ptr;
126                         }
127                         m_buckets[i] = 0;
128                 }
129         }
130
131         ~CTR_Map()
132         {
133                 clear();
134                 delete[] m_buckets;
135         }
136
137         void insert(const Key& key, const Value& value)
138         {
139                 Entry *entry_ptr = m_buckets[key.hash() % m_num_buckets];
140                 while ((entry_ptr != 0) && !(key == entry_ptr->m_key)) {
141                         entry_ptr = entry_ptr->m_next;
142                 }
143
144                 if (entry_ptr != 0) {
145                         entry_ptr->m_value = value;
146                 }
147                 else {
148                         Entry **bucket = &m_buckets[key.hash() % m_num_buckets];
149                         *bucket = new Entry(*bucket, key, value);
150                 }
151         }
152
153         void remove(const Key& key)
154         {
155                 Entry **entry_ptr = &m_buckets[key.hash() % m_num_buckets];
156                 while ((*entry_ptr != 0) && !(key == (*entry_ptr)->m_key)) {
157                         entry_ptr = &(*entry_ptr)->m_next;
158                 }
159
160                 if (*entry_ptr != 0) {
161                         Entry *tmp_ptr = (*entry_ptr)->m_next;
162                         delete *entry_ptr;
163                         *entry_ptr = tmp_ptr;
164                 }
165         }
166
167         Value *operator[](Key key)
168         {
169                 Entry *bucket = m_buckets[key.hash() % m_num_buckets];
170                 while ((bucket != 0) && !(key == bucket->m_key)) {
171                         bucket = bucket->m_next;
172                 }
173                 return bucket != 0 ? &bucket->m_value : 0;
174         }
175
176 private:
177         int     m_num_buckets;
178         Entry **m_buckets;
179 };
180
181 #endif  /* __CTR_MAP_H__ */