28 # error This is a userspace-only header, not allowed by the current build.
31 #include <sifteo/memory.h>
32 #include <sifteo/macros.h>
50 template <
typename T,
unsigned tCapacity,
typename sizeT = u
int32_t>
55 typedef const T* const_iterator;
56 static const unsigned NOT_FOUND = unsigned(-1);
127 T &item = items[numItems++];
135 return items[numItems++];
142 return items[numItems];
150 *outValue = items[numItems];
161 if (index == NOT_FOUND)
164 erase(items + index);
170 iterator next = item + 1;
171 memcpy((uint8_t*)item, (uint8_t*)next, (uint8_t*)
end() - (uint8_t*)next);
188 return &items[
count()];
202 for (iterator I=
begin(), E=
end(); I != E; ++I, ++index) {
203 if (*I == itemToFind)
230 template <
unsigned tSize>
233 static unsigned clz(uint32_t word) {
234 return __builtin_clz(word);
237 static uint32_t lz(
unsigned bit) {
238 return 0x80000000 >> bit;
242 static uint32_t range(
int bit) {
243 if (bit <= 0)
return 0xffffffff;
244 if (bit >= 32)
return 0;
245 return 0xffffffff >> bit;
249 uint32_t words[(tSize + 31) / 32];
259 const unsigned NUM_WORDS = (tSize + 31) / 32;
263 unsigned word = index >> 5;
264 unsigned bit = index & 31;
265 words[word] |= lz(bit);
267 words[0] |= lz(index);
274 const unsigned NUM_WORDS = (tSize + 31) / 32;
278 unsigned word = index >> 5;
279 unsigned bit = index & 31;
280 words[word] &= ~lz(bit);
282 words[0] &= ~lz(index);
289 const unsigned NUM_WORDS = (tSize + 31) / 32;
290 const unsigned NUM_FULL_WORDS = tSize / 32;
291 const unsigned REMAINDER_BITS = tSize & 31;
294 NUM_FULL_WORDS == NUM_WORDS);
297 _SYS_memset32(words, -1, NUM_FULL_WORDS);
299 if (NUM_FULL_WORDS != NUM_WORDS) {
301 uint32_t mask = ((uint32_t)-1) << ((32 - REMAINDER_BITS) & 31);
302 words[NUM_FULL_WORDS] = mask;
309 const unsigned NUM_WORDS = (tSize + 31) / 32;
310 _SYS_memset32(words, 0, NUM_WORDS);
314 bool test(
unsigned index)
const
316 const unsigned NUM_WORDS = (tSize + 31) / 32;
320 unsigned word = index >> 5;
321 unsigned bit = index & 31;
322 return (words[word] & lz(bit)) != 0;
324 return (words[0] & lz(index)) != 0;
331 const unsigned NUM_WORDS = (tSize + 31) / 32;
334 #pragma clang diagnostic push
335 #pragma clang diagnostic ignored "-Wtautological-compare"
336 for (
unsigned w = 0; w < NUM_WORDS; w++)
340 #pragma clang diagnostic pop
341 }
else if (NUM_WORDS == 1) {
342 return words[0] == 0;
351 const unsigned NUM_WORDS = (tSize + 31) / 32;
354 #pragma clang diagnostic push
355 #pragma clang diagnostic ignored "-Wtautological-compare"
357 for (
unsigned w = 0; w < NUM_WORDS; w++)
358 c += __builtin_popcount(words[w]);
360 #pragma clang diagnostic pop
361 }
else if (NUM_WORDS == 1) {
362 return __builtin_popcount(words[0]);
376 const unsigned NUM_WORDS = (tSize + 31) / 32;
379 #pragma clang diagnostic push
380 #pragma clang diagnostic ignored "-Wtautological-compare"
381 for (
unsigned w = 0; w < NUM_WORDS; w++) {
382 uint32_t v = words[w];
384 index = (w << 5) | clz(v);
389 #pragma clang diagnostic pop
390 }
else if (NUM_WORDS == 1) {
391 uint32_t v = words[0];
418 const unsigned NUM_WORDS = (tSize + 31) / 32;
421 #pragma clang diagnostic push
422 #pragma clang diagnostic ignored "-Wtautological-compare"
423 for (
unsigned w = 0; w < NUM_WORDS; w++) {
424 uint32_t v = words[w];
426 unsigned bit = clz(v);
428 index = (w << 5) | bit;
433 #pragma clang diagnostic pop
434 }
else if (NUM_WORDS == 1) {
435 uint32_t v = words[0];
437 unsigned bit = clz(v);
456 for (
unsigned i : *
this) {
472 bool operator == (
const iterator& other)
const {
473 return index == other.index;
476 bool operator != (
const iterator& other)
const {
477 return index != other.index;
480 unsigned operator* ()
const {
486 index =
unsigned(-1);
501 i.index = unsigned(-1);
512 const unsigned NUM_WORDS = (tSize + 31) / 32;
516 #pragma clang diagnostic push
517 #pragma clang diagnostic ignored "-Wtautological-compare"
518 for (
unsigned w = 0; w < NUM_WORDS; w++) {
519 words[w] = (w == (index >> 5)) ? lz(index & 31) : 0;
521 #pragma clang diagnostic pop
523 words[0] = lz(index);
533 const unsigned NUM_WORDS = (tSize + 31) / 32;
540 #pragma clang diagnostic push
541 #pragma clang diagnostic ignored "-Wtautological-compare"
542 for (
unsigned w = 0; w < NUM_WORDS; w++) {
544 words[w] = range(begin - offset) & ~range(end - offset);
546 #pragma clang diagnostic pop
548 words[0] = range(begin) & ~range(end);
555 const unsigned NUM_WORDS = (tSize + 31) / 32;
557 #pragma clang diagnostic push
558 #pragma clang diagnostic ignored "-Wtautological-compare"
559 for (
unsigned w = 0; w < NUM_WORDS; w++)
560 result.words[w] = words[w] & other.words[w];
561 #pragma clang diagnostic pop
568 const unsigned NUM_WORDS = (tSize + 31) / 32;
570 #pragma clang diagnostic push
571 #pragma clang diagnostic ignored "-Wtautological-compare"
572 for (
unsigned w = 0; w < NUM_WORDS; w++)
573 result.words[w] = words[w] | other.words[w];
574 #pragma clang diagnostic pop
581 const unsigned NUM_WORDS = (tSize + 31) / 32;
583 #pragma clang diagnostic push
584 #pragma clang diagnostic ignored "-Wtautological-compare"
585 for (
unsigned w = 0; w < NUM_WORDS; w++)
586 result.words[w] = words[w] ^ other.words[w];
587 #pragma clang diagnostic pop
594 const unsigned NUM_WORDS = (tSize + 31) / 32;
595 const unsigned NUM_FULL_WORDS = tSize / 32;
596 const unsigned REMAINDER_BITS = tSize & 31;
600 NUM_FULL_WORDS == NUM_WORDS);
603 #pragma clang diagnostic push
604 #pragma clang diagnostic ignored "-Wtautological-compare"
605 for (
unsigned w = 0; w < NUM_FULL_WORDS; w++)
606 result.words[w] = ~words[w];
607 #pragma clang diagnostic pop
609 if (NUM_FULL_WORDS != NUM_WORDS) {
611 uint32_t mask = ((uint32_t)-1) << ((32 - REMAINDER_BITS) & 31);
612 result.words[NUM_FULL_WORDS] = mask & ~words[NUM_FULL_WORDS];
void erase(unsigned index)
Remove a specific element, by index, and shift the others down.
Definition: array.h:159
T pop_back()
Deallocate the last element and return a copy of it's value.
Definition: array.h:139
void erase(iterator item)
Remove the element at iterator 'item' and shift the others down.
Definition: array.h:168
static unsigned capacity()
Retrieve the capacity of this array, always constant at compile-time.
Definition: array.h:64
T & append(const T &newItem)
Synonym for push_back()
Definition: array.h:115
BitArray()
Create a new empty BitArray.
Definition: array.h:506
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
T & operator[](int index)
Accessor for array elements.
Definition: array.h:109
const T & operator[](int index) const
Accessor for array elements.
Definition: array.h:97
T & push_back()
Allocate the first unused slot in the array, and return an reference to this uninitialized slot...
Definition: array.h:133
iterator end()
Return an iterator pointing just past the last in-use slot in the array.
Definition: array.h:187
Array()
Initialize a new empty array.
Definition: array.h:59
bool findFirst(unsigned &index) const
Find the lowest index where there's a marked (1) bit.
Definition: array.h:374
void mark()
Mark (set to 1) all bits in the array.
Definition: array.h:287
void clear()
Clear (set to 0) all bits in the array.
Definition: array.h:307
unsigned count() const
How many bits are marked in this vector?
Definition: array.h:349
An STL-style iterator for the BitArray.
Definition: array.h:468
void setCount(unsigned c)
Change the number of elements currently in the array.
Definition: array.h:80
void pop_back(T *outValue)
Pop-back, but avoid a potential unnecessary copy.
Definition: array.h:146
iterator begin()
Return an iterator pointing to the first slot in the array.
Definition: array.h:182
T & push_back(const T &newItem)
Copy 'newItem' to the first unused slot in the array, and return a reference to the copy...
Definition: array.h:125
BitArray< tSize > operator^(const BitArray< tSize > &other) const
Bitwise XOR of two BitArrays of the same size.
Definition: array.h:579
const T & operator[](unsigned index) const
Accessor for array elements.
Definition: array.h:91
A statically sized array.
Definition: array.h:51
BitArray(unsigned index)
Create a new BitArray with a single bit marked.
Definition: array.h:511
void erase_tail()
Remove the last element, which involves no shifting.
Definition: array.h:176
void clear(unsigned index)
Clear (set to 0) a single bit.
Definition: array.h:272
BitArray< tSize > operator~() const
Negate a BitArray, returning a new array in which each bit is inverted.
Definition: array.h:592
BitArray< tSize > operator|(const BitArray< tSize > &other) const
Bitwise OR of two BitArrays of the same size.
Definition: array.h:566
bool test(unsigned index) const
Is a particular bit marked?
Definition: array.h:314
bool empty() const
Is this array empty?
Definition: array.h:86
void mark(unsigned index)
Mark (set to 1) a single bit.
Definition: array.h:257
iterator end() const
Return an STL-style iterator for this array.
Definition: array.h:499
bool clearFirst(unsigned &index)
Find and clear the lowest marked bit.
Definition: array.h:416
#define STATIC_ASSERT(_x)
Definition: macros.h:342
T & operator[](unsigned index)
Accessor for array elements.
Definition: array.h:103
T & append()
Synonym for push_back()
Definition: array.h:120
BitArray< tSize > operator&(const BitArray< tSize > &other) const
Bitwise AND of two BitArrays of the same size.
Definition: array.h:553
bool empty() const
Is every bit in this array set to zero?
Definition: array.h:329
unsigned find(T itemToFind)
Definition: array.h:199
iterator begin() const
Return an STL-style iterator for this array.
Definition: array.h:492
unsigned count() const
How many items does this array currently hold?
Definition: array.h:69
BitArray(unsigned begin, unsigned end)
Create a new BitArray with a range of bits marked.
Definition: array.h:532
bool clearN(unsigned &index, unsigned n)
Find and clear the Nth lowest marked bit.
Definition: array.h:454
static unsigned size()
Retrieve the size of this array in bits, always constant at compile-time.
Definition: array.h:252
void clear()
Equivalent to setCount(0)
Definition: array.h:154
void memcpy(uint8_t *dest, const uint8_t *src, unsigned count)
memcpy(), with an implicit 8-bit data width
Definition: memory.h:94
A fixed-size array of bits, with compact storage and fast iteration.
Definition: array.h:231