SMIL  0.9.1
DBitArray.h
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #ifndef _DBITARRAY_H
31 #define _DBITARRAY_H
32 
33 
34 #include "Core/include/private/DTypes.hpp"
35 
36 #include <iostream>
37 #include <algorithm>
38 
39 
40 
41 namespace smil
42 {
43  typedef bool Bit;
44 
45  class BitArray
46  {
47  public:
48 
49 
50  typedef size_t INT_TYPE;
51 
52  UINT index;
53  INT_TYPE *intArray;
54 
55 
56  static const INT_TYPE INT_TYPE_SIZE = sizeof(INT_TYPE)*CHAR_BIT;
57  static inline INT_TYPE INT_TYPE_MIN() { return numeric_limits<INT_TYPE>::min(); }
58  static inline INT_TYPE INT_TYPE_MAX() { return numeric_limits<INT_TYPE>::max(); }
59  static inline UINT INT_SIZE(UINT bitCount) { return (bitCount-1)/INT_TYPE_SIZE + 1; }
60 
62  static const INT_TYPE INT_MS_BIT = (1UL << (INT_TYPE_SIZE - 2));
64  static const INT_TYPE INT_LS_BIT = 0x01;
65 
66 
67  BitArray();
68  BitArray(const BitArray &rhs);
69  BitArray(UINT _bitWidth, UINT _bitHeight);
70  BitArray(INT_TYPE *arr, UINT _bitWidth, UINT _bitHeight=1);
71  BitArray(bool *arr, UINT _bitWidth, UINT _bitHeight=1);
72  ~BitArray();
73 
74 
75  inline UINT getBitWidth() { return bitWidth; }
76  inline UINT getIntWidth() { return intWidth; }
77  inline UINT getIntNbr() { return intWidth*height; }
78  inline UINT getHeight() { return height; }
79  inline UINT getBitPadX() const { return intWidth*INT_TYPE_SIZE - bitWidth; }
80 
81 
82  void setSize(UINT _bitWidth, UINT _bitHeight=1);
83  void createIntArray()
84  {
85  if (!intArray)
86  intArray = createAlignedBuffer<INT_TYPE>(intWidth*height);
87  }
88  void deleteIntArray()
89  {
90  if (intArray)
91  deleteAlignedBuffer<INT_TYPE>(intArray);
92  intArray = NULL;
93  }
94 
95  inline bool getValue() const
96  {
97  return getValue(index);
98  }
99  inline bool getValue(UINT ind) const
100  {
101  int Y = ind / bitWidth;
102  int X = (ind + Y*this->getBitPadX()) / INT_TYPE_SIZE;
103  int x = (ind-Y*bitWidth) % INT_TYPE_SIZE;
104  return (intArray[X] & (1UL << x))!=0;
105  }
106  inline void setValue(bool v)
107  {
108  setValue(index, v);
109  }
110  inline void setValue(UINT ind, bool val)
111  {
112  int Y = ind / bitWidth;
113  int X = (ind + Y*this->getBitPadX()) / INT_TYPE_SIZE;
114  int x = (ind-Y*bitWidth) % INT_TYPE_SIZE;
115  if (val)
116  intArray[X] |= (1UL << x);
117  else intArray[X] &= ~(1UL << x);
118  }
119 
120  operator bool() { return getValue(index); }
121  BitArray operator [] (UINT i); // lValue
122  const bool operator [] (UINT i) const; // rValue
123 
124  inline BitArray &operator * ()
125  {
126  return *this;
127  }
128 
129  // operator void* () { return (void*)this->intArray; }
130  // operator char* () { return (char*)this->intArray; }
131 // void operator=(void *ptr) { this->intArray = (INT_TYPE*)ptr; }
132  const bool &operator=(const bool &b)
133  {
134  setValue(b);
135  return b;
136  }
137  inline BitArray operator + (int dp) const;
138  inline BitArray operator + (long unsigned int dp) const
139  {
140  return operator+((int)dp);
141  }
142  inline BitArray operator + (UINT dp) const
143  {
144  return operator+((int)dp);
145  }
146  BitArray operator - (int dp) const;
147  BitArray& operator ++ (int);
148  BitArray& operator ++ ();
149 
150  BitArray& operator += (int dp) { index+=dp; return *this; }
151 
152  inline BitArray& operator = (const BitArray &rhs)
153  {
154  this->setSize(rhs.bitWidth, rhs.height);
155  this->intArray = rhs.intArray;
156  this->index = rhs.index;
157  return *this;
158  }
159 
160  ostream& printSelf(ostream &os=cout);
161 
162  private:
163  UINT intWidth;
164  UINT bitWidth;
165  UINT height;
166  };
167 
168 
169  BitArray BitArray::operator + (int dp) const
170  {
171  BitArray ba(this->intArray, this->bitWidth, this->height);
172  ba.index = this->index + dp;
173  return ba;
174  }
175 
176 
177 
178 
179 
180 
181  inline ostream& operator << (ostream &os, BitArray &b)
182  {
183  return b.printSelf(os);
184  }
185 
186 
187  template <>
188  inline const char *getDataTypeAsString(Bit *val)
189  {
190  return "Bit";
191  }
192 
193 
194  template <>
195  struct ImDtTypes<Bit>
196  {
197  typedef Bit pixelType;
198  typedef BitArray lineType;
199  typedef lineType* sliceType;
200  typedef sliceType* volType;
201 
202  static inline pixelType min() { return Bit(0); }
203  static inline pixelType max() { return Bit(1); }
204  static inline size_t cardinal() { return 2; }
205  static inline lineType createLine(UINT lineLen)
206  {
207  BitArray ba(lineLen, 1);
208  ba.createIntArray();
209  return ba;
210  }
211  static inline void deleteLine(lineType line)
212  {
213  line.deleteIntArray();
214  }
215  static inline unsigned long ptrOffset(lineType p, unsigned long n=SIMD_VEC_SIZE) { return ((unsigned long)(p.intArray)) & (n-1); }
216  static inline std::string toString(const Bit &val)
217  {
218  stringstream str;
219  str << int(val);
220  return str.str();
221  }
222  };
223 } // namespace smil
224 
225 
226 #endif // _DBITARRAY_H
227 
Definition: DColorConvert.h:38
Definition: DBitArray.h:45
static const INT_TYPE INT_LS_BIT
Less significant bit.
Definition: DBitArray.h:64
static const INT_TYPE INT_MS_BIT
Most significant bit.
Definition: DBitArray.h:62
Definition: DTypes.hpp:78