Zen API
 All Classes Files Functions Variables Typedefs Friends Macros Modules Pages
kBitArray.h
Go to the documentation of this file.
1 
10 #ifndef K_API_BIT_ARRAY_H
11 #define K_API_BIT_ARRAY_H
12 
13 #include <kApi/kApiDef.h>
14 #include <kApi/Data/kBitArray.x.h>
15 
30 //typedef kObject kBitArray; --forward-declared in kApiDef.x.h
31 
41 kFx(kStatus) kBitArray_Construct(kBitArray* array, kSize length, kAlloc allocator);
42 
52 {
53  return xkBitArray_Reallocate(array, length, kFALSE);
54 }
55 
67 {
68  return xkBitArray_Reallocate(array, length, kTRUE);
69 }
70 
79 kFx(kStatus) kBitArray_Assign(kBitArray array, kBitArray source);
80 
90 {
91  kObj(kBitArray, array);
92  kSize wordCount = xkBitArray_WordCount(array);
93  k64u wordValue = (value) ? k64U_MAX : 0;
94 
95  for (kSize i = 0; i < wordCount; ++i)
96  {
97  obj->buffer[i] = wordValue;
98  }
99 
100  return kOK;
101 }
102 
113 {
114  kObj(kBitArray, array);
115 
116  kCheckArgs(index < obj->bitLength);
117 
118  kSize wordIndex = index >> kBIT_ARRAY_WORD_WIDTH_SHIFT;
119  kSize bitOffset = index & kBIT_ARRAY_BIT_OFFSET_MASK;
120 
121  if (value) obj->buffer[wordIndex] |= (1llu << bitOffset);
122  else obj->buffer[wordIndex] &= ~(1llu << bitOffset);
123 
124  return kOK;
125 }
126 
137 {
138  kObj(kBitArray, array);
139 
140  kCheckArgs(index < obj->bitLength);
141 
142  *value = kBitArray_At(array, index);
143 
144  return kOK;
145 }
146 
154 kFx(kStatus) kBitArray_Not(kBitArray array);
155 
166 kFx(kStatus) kBitArray_And(kBitArray array, kBitArray other);
167 
178 kFx(kStatus) kBitArray_Or(kBitArray array, kBitArray other);
179 
190 kFx(kStatus) kBitArray_Xor(kBitArray array, kBitArray other);
191 
204 {
205  kObj(kBitArray, array);
206 
207  kAssert(index < obj->bitLength);
208 
209  kSize wordIndex = index >> kBIT_ARRAY_WORD_WIDTH_SHIFT;
210  kSize bitOffset = index & kBIT_ARRAY_BIT_OFFSET_MASK;
211 
212  return (obj->buffer[wordIndex] >> bitOffset) & 0x1;
213 }
214 
223 {
224  kObj(kBitArray, array);
225 
226  return obj->bitLength;
227 }
228 
237 
246 {
247  return kBitArray_Length(array) - kBitArray_TrueCount(array);
248 }
249 
250 #endif
kStatus kBitArray_Item(kBitArray array, kSize index, kBool *value)
Gets the value of an item.
Definition: kBitArray.h:136
Represents a 64-bit unsigned integer.
k64u kBitArray_FalseCount(kBitArray array)
Counts the number of bits in the array that are set to kFALSE.
Definition: kBitArray.h:245
Represents an unsigned integer that can store a pointer address.
Abstract base class for memory allocator types.
#define kInlineFx(TYPE)
Inline method declaration helper.
Definition: kApiDef.h:26
kBool kBitArray_At(kBitArray array, kSize index)
Returns the specified item in the array.
Definition: kBitArray.h:203
kStatus kBitArray_Xor(kBitArray array, kBitArray other)
Performs bitwise-xor over all bits from the specified arrays.
kStatus kBitArray_Assign(kBitArray array, kBitArray source)
Copies the source array.
#define kTRUE
Boolean true.
Definition: kApiDef.h:356
kStatus kBitArray_Or(kBitArray array, kBitArray other)
Performs bitwise-or over all bits from the specified arrays.
Represents a 1D array of bits.
#define kObj(TypeName_T, T_object)
Declares a local "obj" (this-pointer) variable and initializes it from a type-checked object handle...
Definition: kApiDef.h:3383
kStatus kBitArray_SetAll(kBitArray array, kBool value)
Sets all array elements to the specified value.
Definition: kBitArray.h:89
kStatus kBitArray_Allocate(kBitArray array, kSize length)
Reallocates the array item buffer.
Definition: kBitArray.h:51
Core Zen type declarations.
kSize kBitArray_Length(kBitArray array)
Returns the array length, in bits.
Definition: kBitArray.h:222
#define kCheckArgs(EXPRESSION)
Executes a return statement if the given expression is not kTRUE.
Definition: kApiDef.h:584
kStatus kBitArray_Resize(kBitArray array, kSize length)
Resizes the internal array item buffer.
Definition: kBitArray.h:66
kStatus kBitArray_Construct(kBitArray *array, kSize length, kAlloc allocator)
Constructs a kBitArray object.
#define kAssert(EXPRESSION)
Aborts execution if EXPRESSION is kFALSE.
Definition: kApiDef.h:751
kStatus kBitArray_And(kBitArray array, kBitArray other)
Performs bitwise-and over all bits from the specified arrays.
#define kOK
Operation successful.
Definition: kApiDef.h:515
Represents an error code.
#define k64U_MAX
k64u maximum value.
Definition: kApiDef.h:91
k64u kBitArray_TrueCount(kBitArray array)
Counts the number of bits in the array that are set to kTRUE.
kStatus kBitArray_SetItem(kBitArray array, kSize index, kBool value)
Sets the value of the specified item.
Definition: kBitArray.h:112
Represents a boolean value.
#define kFALSE
Boolean false.
Definition: kApiDef.h:355
kStatus kBitArray_Not(kBitArray array)
Inverts all bits.