QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
bool_array.h
Go to the documentation of this file.
1// -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
2// vim:tabstop=4:shiftwidth=4:expandtab:
3
4/*
5 * Copyright (C) 2004-2008 Wu Yongwei <adah at users dot sourceforge dot net>
6 *
7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any
9 * damages arising from the use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute
13 * it freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must
16 * not claim that you wrote the original software. If you use this
17 * software in a product, an acknowledgement in the product
18 * documentation would be appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must
20 * not be misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source
22 * distribution.
23 *
24 * This file is part of Stones of Nvwa:
25 * http://sourceforge.net/projects/nvwa
26 *
27 */
28
40#ifndef QT_NO_DEBUG
41#ifndef _QX_MODE_RELEASE
42#if _QX_USE_MEM_LEAK_DETECTION
43
44#ifndef _BOOL_ARRAY_H
45#define _BOOL_ARRAY_H
46
47#ifdef _MSC_VER
48#pragma once
49#endif
50
51#include <assert.h> // assert
52#include <stdlib.h> // exit, free, and NULL
53#include <new> // std::bad_alloc
54#include <stdexcept> // std::out_of_range
55#include <string> // for exception constructors
56
57namespace qx {
58namespace memory {
59
60#ifndef _BYTE_DEFINED
61#define _BYTE_DEFINED
62typedef unsigned char BYTE;
63#endif // !_BYTE_DEFINED
64
80{
83 {
84 public:
85 _Element(BYTE* __ptr, unsigned long __idx);
86 bool operator=(bool ___value);
87 operator bool() const;
88 private:
91 size_t _M_bit_idx;
92 };
93
94public:
95 bool_array() : _M_byte_ptr(NULL), _M_length(0) {}
96 explicit bool_array(unsigned long __size);
97 ~bool_array() { if (_M_byte_ptr != NULL) free(_M_byte_ptr); }
98
99 bool create(unsigned long __size);
100 void initialize(bool ___value);
101
102 // Using unsigned type here can increase performance!
103 _Element operator[](unsigned long __idx);
104 bool at(unsigned long __idx) const;
105 void reset(unsigned long __idx);
106 void set(unsigned long __idx);
107
108 unsigned long size() const { return _M_length; }
109 unsigned long count() const;
110 unsigned long count(unsigned long __beg, unsigned long __end) const;
111 void flip();
112
113private:
115 unsigned long _M_length;
116 static BYTE _S_bit_count[256];
117};
118
119
120/* Inline functions */
121
128inline bool_array::_Element::_Element(BYTE* __ptr, unsigned long __idx)
129{
130 _M_byte_ptr = __ptr;
131 _M_byte_idx = (size_t)(__idx / 8);
132 _M_bit_idx = (size_t)(__idx % 8);
133}
134
141inline bool bool_array::_Element::operator=(bool ___value)
142{
143 if (___value)
144 *(_M_byte_ptr + _M_byte_idx) |= 1 << _M_bit_idx;
145 else
146 *(_M_byte_ptr + _M_byte_idx) &= ~(1 << _M_bit_idx);
147 return ___value;
148}
149
155inline bool_array::_Element::operator bool() const
156{
157 return *(_M_byte_ptr + _M_byte_idx) & (1 << _M_bit_idx) ? true : false;
158}
159
167inline bool_array::bool_array(unsigned long __size)
168 : _M_byte_ptr(NULL), _M_length(0)
169{
170 if (__size == 0)
171 throw std::out_of_range("invalid bool_array size");
172
173 if (!create(__size))
174 throw std::bad_alloc();
175}
176
183{
184 assert(_M_byte_ptr);
185 assert(__idx < _M_length);
186 return _Element(_M_byte_ptr, __idx);
187}
188
196inline bool bool_array::at(unsigned long __idx) const
197{
198 size_t __byte_idx, __bit_idx;
199 if (__idx >= _M_length)
200 throw std::out_of_range("invalid bool_array subscript");
201 __byte_idx = (size_t)(__idx / 8);
202 __bit_idx = (size_t)(__idx % 8);
203 return *(_M_byte_ptr + __byte_idx) & (1 << __bit_idx) ? true : false;
204}
205
212inline void bool_array::reset(unsigned long __idx)
213{
214 size_t __byte_idx, __bit_idx;
215 if (__idx >= _M_length)
216 throw std::out_of_range("invalid bool_array subscript");
217 __byte_idx = (size_t)(__idx / 8);
218 __bit_idx = (size_t)(__idx % 8);
219 *(_M_byte_ptr + __byte_idx) &= ~(1 << __bit_idx);
220}
221
228inline void bool_array::set(unsigned long __idx)
229{
230 size_t __byte_idx, __bit_idx;
231 if (__idx >= _M_length)
232 throw std::out_of_range("invalid bool_array subscript");
233 __byte_idx = (size_t)(__idx / 8);
234 __bit_idx = (size_t)(__idx % 8);
235 *(_M_byte_ptr + __byte_idx) |= 1 << __bit_idx;
236}
237
238} // namespace memory
239} // namespace qx
240
241#endif // _BOOL_ARRAY_H
242#endif // _QX_USE_MEM_LEAK_DETECTION
243#endif // _QX_MODE_RELEASE
244#endif // QT_NO_DEBUG
#define QX_DLL_EXPORT
Definition QxMacro.h:182
bool operator=(bool ___value)
Definition bool_array.h:141
_Element(BYTE *__ptr, unsigned long __idx)
Definition bool_array.h:128
void reset(unsigned long __idx)
Definition bool_array.h:212
bool create(unsigned long __size)
unsigned long _M_length
Definition bool_array.h:115
_Element operator[](unsigned long __idx)
Definition bool_array.h:182
bool at(unsigned long __idx) const
Definition bool_array.h:196
void initialize(bool ___value)
void set(unsigned long __idx)
Definition bool_array.h:228
unsigned long size() const
Definition bool_array.h:108
unsigned long count(unsigned long __beg, unsigned long __end) const
unsigned long count() const
unsigned char BYTE
Definition bool_array.h:62
Root namespace for all QxOrm library features.