Bullet Collision Detection & Physics Library: gim_bitset.h Source File

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gim_bitset.h
Go to the documentation of this file.
1#ifndef GIM_BITSET_H_INCLUDED
2#define GIM_BITSET_H_INCLUDED
6/*
7-----------------------------------------------------------------------------
8This source file is part of GIMPACT Library.
9
10For the latest info, see http://gimpact.sourceforge.net/
11
12Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371.
13email: projectileman@yahoo.com
14
15 This library is free software; you can redistribute it and/or
16 modify it under the terms of EITHER:
17 (1) The GNU Lesser General Public License as published by the Free
18 Software Foundation; either version 2.1 of the License, or (at
19 your option) any later version. The text of the GNU Lesser
20 General Public License is included with this library in the
21 file GIMPACT-LICENSE-LGPL.TXT.
22 (2) The BSD-style license that is included with this library in
23 the file GIMPACT-LICENSE-BSD.TXT.
24 (3) The zlib/libpng license that is included with this library in
25 the file GIMPACT-LICENSE-ZLIB.TXT.
26
27 This library is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
30 GIMPACT-LICENSE-LGPL.TXT, GIMPACT-LICENSE-ZLIB.TXT and GIMPACT-LICENSE-BSD.TXT for more details.
31
32-----------------------------------------------------------------------------
33*/
34
35#include "gim_array.h"
36
37#define GUINT_BIT_COUNT 32
38#define GUINT_EXPONENT 5
39
40 class gim_bitset
41{
42public:
43 gim_array<GUINT> m_container;
44
45 gim_bitset()
46 {
47 }
48
49 gim_bitset(GUINT bits_count)
50 {
51 resize(bits_count);
52 }
53
54 ~gim_bitset()
55 {
56 }
57
58 inline bool resize(GUINT newsize)
59 {
60 GUINT oldsize = m_container.size();
61 m_container.resize(newsize / GUINT_BIT_COUNT + 1, false);
62 while (oldsize < m_container.size())
63 {
64 m_container[oldsize] = 0;
65 }
66 return true;
67 }
68
69 inline GUINT size()
70 {
71 return m_container.size() * GUINT_BIT_COUNT;
72 }
73
74 inline void set_all()
75 {
76 for (GUINT i = 0; i < m_container.size(); ++i)
77 {
78 m_container[i] = 0xffffffff;
79 }
80 }
81
82 inline void clear_all()
83 {
84 for (GUINT i = 0; i < m_container.size(); ++i)
85 {
86 m_container[i] = 0;
87 }
88 }
89
90 inline void set(GUINT bit_index)
91 {
92 if (bit_index >= size())
93 {
94 resize(bit_index);
95 }
96 m_container[bit_index >> GUINT_EXPONENT] |= (1 << (bit_index & (GUINT_BIT_COUNT - 1)));
97 }
98
100 inline char get(GUINT bit_index)
101 {
102 if (bit_index >= size())
103 {
104 return 0;
105 }
106 char value = m_container[bit_index >> GUINT_EXPONENT] &
107 (1 << (bit_index & (GUINT_BIT_COUNT - 1)));
108 return value;
109 }
110
111 inline void clear(GUINT bit_index)
112 {
113 m_container[bit_index >> GUINT_EXPONENT] &= ~(1 << (bit_index & (GUINT_BIT_COUNT - 1)));
114 }
115};
116
117#endif // GIM_CONTAINERS_H_INCLUDED
gim_array
Very simple array container with fast access and simd memory.
Definition: gim_array.h:43
gim_array::size
GUINT size() const
Definition: gim_array.h:143
gim_array::resize
void resize(GUINT size, bool call_constructor=true, const T &fillData=T())
Definition: gim_array.h:287
gim_bitset
Definition: gim_bitset.h:41
gim_bitset::resize
bool resize(GUINT newsize)
Definition: gim_bitset.h:58
gim_bitset::~gim_bitset
~gim_bitset()
Definition: gim_bitset.h:54
gim_bitset::clear
void clear(GUINT bit_index)
Definition: gim_bitset.h:111
gim_bitset::get
char get(GUINT bit_index)
Return 0 or 1.
Definition: gim_bitset.h:100
gim_bitset::gim_bitset
gim_bitset()
Definition: gim_bitset.h:45
gim_bitset::set
void set(GUINT bit_index)
Definition: gim_bitset.h:90
gim_bitset::set_all
void set_all()
Definition: gim_bitset.h:74
gim_bitset::clear_all
void clear_all()
Definition: gim_bitset.h:82
gim_bitset::size
GUINT size()
Definition: gim_bitset.h:69
gim_bitset::m_container
gim_array< GUINT > m_container
Definition: gim_bitset.h:43
gim_bitset::gim_bitset
gim_bitset(GUINT bits_count)
Definition: gim_bitset.h:49
gim_array.h
GUINT_BIT_COUNT
#define GUINT_BIT_COUNT
Definition: gim_bitset.h:37
GUINT_EXPONENT
#define GUINT_EXPONENT
Definition: gim_bitset.h:38
GUINT
#define GUINT
Definition: gim_math.h:40