• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 
19 #ifndef BOOL_ARRAY_H
20 #define BOOL_ARRAY_H
21 
22 #include "oscl_base.h"
23 
24 template <uint32 max_array_size> class BoolArray
25 {
26 
27     private:
28 
29         static uint32 mask_array[33];
30 
31 
32 
33     public:
34 
BoolArray()35         BoolArray()
36         {
37             size = max_array_size;
38             elements = (max_array_size + 31) / 32;
39         };
40 
~BoolArray()41         ~BoolArray() {};
42 
43 
set_size(int in_size)44         bool set_size(int in_size)
45         {
46             if (size <= 0 || size > (int) max_array_size)
47             {
48                 return false;
49             }
50             size = in_size;
51             return true;
52         };
53 
54 
55         bool set_range(bool value, int first_index = 0, int last_index = -1)
56         {
57             if (last_index < 0)
58             {
59                 last_index = size - 1;
60             };
61             if (first_index < 0 || first_index > last_index || last_index >= size)
62             {
63                 return false;
64             }
65 
66 
67             static const uint32 mask_array[33] =
68             {
69                 0, 0x1, 0x3, 0x7,
70                 0xF, 0x1F, 0x3F, 0x7F,
71                 0xFF, 0x1FF, 0x3FF, 0x7FF,
72                 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF,
73                 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF,
74                 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF,
75                 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF,
76                 0xFFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF,
77                 0xFFFFFFFF
78             };
79 
80 
81             int starting_word = first_index / 32;
82             int ending_word = (last_index) / 32;
83 
84             for (int ii = starting_word; ii <= ending_word; ++ii)
85             {
86 
87                 if (ii == ending_word)
88                 {
89                     // compute the word offset
90                     int word_index = (last_index % 32) + 1;
91                     uint32 mask = mask_array[word_index];
92                     if (value)
93                     {
94                         array[ii] |= (mask);
95                     }
96                     else
97                     {
98                         array[ii] &= (~mask);
99                     }
100                 }
101                 else if (ii == starting_word)
102                 {
103                     // compute the word offset
104                     int word_index = first_index % 32;
105                     uint32 mask = mask_array[word_index];
106                     if (value)
107                     {
108                         array[ii] |= (~mask);
109                     }
110                     else
111                     {
112                         array[ii] &= (mask);
113                     }
114 
115                 }
116                 else
117                 {
118                     uint32 mask = mask_array[32];
119                     if (value)
120                     {
121                         array[ii] |= (mask);
122                     }
123                     else
124                     {
125                         array[ii] &= (~mask);
126                     }
127 
128                 }
129 
130             }
131             return true;
132         }
133 
set_value(int index,bool value)134         bool set_value(int index, bool value)
135         {
136 
137             if (index < 0 || index >= size)
138             {
139                 return false;
140             }
141 
142             int array_index = (index) / 32;
143             int word_index = index % 32;
144             uint32 mask = 1 << word_index;
145 
146             if (value)
147             {
148                 array[array_index] |= mask;
149             }
150             else
151             {
152                 array[array_index] &= (~mask);
153             }
154 
155             return true;
156         }
157 
158 
get_value(int index)159         bool get_value(int index) const
160         {
161             if (index < 0 || index >= size)
162             {
163                 return false;
164             }
165 
166             int array_index = (index) / 32;
167             int word_index = index % 32;
168             uint32 mask = 1 << word_index;
169 
170             return ((array[array_index] & mask) != 0);
171         }
172 
173 
174 
175 
176 
177         bool operator[](int index) const
178         {
179             return get_value(index);
180         };
181 
182     private:
183         int size;
184         int elements;
185         uint32 array[(max_array_size+31)/32];
186 
187 };
188 
189 
190 
191 
192 
193 
194 #endif
195