[2431] | 1 | #ifndef GIM_BITSET_H_INCLUDED |
---|
| 2 | #define GIM_BITSET_H_INCLUDED |
---|
| 3 | /*! \file gim_bitset.h |
---|
| 4 | \author Francisco Len Nßjera |
---|
| 5 | */ |
---|
| 6 | /* |
---|
| 7 | ----------------------------------------------------------------------------- |
---|
| 8 | This source file is part of GIMPACT Library. |
---|
| 9 | |
---|
| 10 | For the latest info, see http://gimpact.sourceforge.net/ |
---|
| 11 | |
---|
| 12 | Copyright (c) 2006 Francisco Leon Najera. C.C. 80087371. |
---|
| 13 | email: 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 | |
---|
| 38 | #define GUINT_BIT_COUNT 32 |
---|
| 39 | #define GUINT_EXPONENT 5 |
---|
| 40 | |
---|
| 41 | class gim_bitset |
---|
| 42 | { |
---|
| 43 | public: |
---|
| 44 | gim_array<GUINT> m_container; |
---|
| 45 | |
---|
| 46 | gim_bitset() |
---|
| 47 | { |
---|
| 48 | |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | gim_bitset(GUINT bits_count) |
---|
| 52 | { |
---|
| 53 | resize(bits_count); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | ~gim_bitset() |
---|
| 57 | { |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | inline bool resize(GUINT newsize) |
---|
| 61 | { |
---|
| 62 | GUINT oldsize = m_container.size(); |
---|
| 63 | m_container.resize(newsize/GUINT_BIT_COUNT + 1,false); |
---|
| 64 | while(oldsize<m_container.size()) |
---|
| 65 | { |
---|
| 66 | m_container[oldsize] = 0; |
---|
| 67 | } |
---|
| 68 | return true; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | inline GUINT size() |
---|
| 72 | { |
---|
| 73 | return m_container.size()*GUINT_BIT_COUNT; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | inline void set_all() |
---|
| 77 | { |
---|
| 78 | for(GUINT i = 0;i<m_container.size();++i) |
---|
| 79 | { |
---|
| 80 | m_container[i] = 0xffffffff; |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | inline void clear_all() |
---|
| 85 | { |
---|
| 86 | for(GUINT i = 0;i<m_container.size();++i) |
---|
| 87 | { |
---|
| 88 | m_container[i] = 0; |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | inline void set(GUINT bit_index) |
---|
| 93 | { |
---|
| 94 | if(bit_index>=size()) |
---|
| 95 | { |
---|
| 96 | resize(bit_index); |
---|
| 97 | } |
---|
| 98 | m_container[bit_index >> GUINT_EXPONENT] |= (1 << (bit_index & (GUINT_BIT_COUNT-1))); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | ///Return 0 or 1 |
---|
| 102 | inline char get(GUINT bit_index) |
---|
| 103 | { |
---|
| 104 | if(bit_index>=size()) |
---|
| 105 | { |
---|
| 106 | return 0; |
---|
| 107 | } |
---|
| 108 | char value = m_container[bit_index >> GUINT_EXPONENT] & |
---|
| 109 | (1 << (bit_index & (GUINT_BIT_COUNT-1))); |
---|
| 110 | return value; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | inline void clear(GUINT bit_index) |
---|
| 114 | { |
---|
| 115 | m_container[bit_index >> GUINT_EXPONENT] &= ~(1 << (bit_index & (GUINT_BIT_COUNT-1))); |
---|
| 116 | } |
---|
| 117 | }; |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | #endif // GIM_CONTAINERS_H_INCLUDED |
---|