Rev 1987 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1987 | Rev 1990 | ||
|---|---|---|---|
| 1 | /* |
1 | /* |
| 2 | * |
2 | * |
| 3 | * Copyright (C) 2010 Mattias Runge |
3 | * Copyright (C) 2010 Mattias Runge |
| 4 | * |
4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
8 | * (at your option) any later version. |
| 9 | * |
9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
13 | * GNU General Public License for more details. |
| 14 | * |
14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
18 | * |
| 19 | */ |
19 | */ |
| 20 | 20 | ||
| 21 | #include "Bitset.h" |
21 | #include "Bitset.h" |
| 22 | 22 | ||
| 23 | #include <math.h> |
23 | #include <math.h> |
| 24 | #include <string.h> |
24 | #include <string.h> |
| 25 | 25 | ||
| 26 | #include <boost/lexical_cast.hpp> |
26 | #include <boost/lexical_cast.hpp> |
| 27 | 27 | ||
| 28 | namespace atom { |
28 | namespace atom { |
| 29 | namespace common { |
29 | namespace common { |
| 30 | 30 | ||
| 31 | Bitset::Bitset(unsigned int count) |
31 | Bitset::Bitset(unsigned int count) |
| 32 | { |
32 | { |
| 33 | unsigned int byte_size = (int)ceil(count / 8); |
33 | unsigned int byte_size = (int)ceil(count / 8); |
| 34 | 34 | ||
| 35 | this->count_ = count; |
35 | this->count_ = count; |
| 36 | this->bytes_ = new unsigned char[byte_size]; |
36 | this->bytes_ = new unsigned char[byte_size]; |
| 37 | 37 | ||
| 38 | memset(this->bytes_, 0, byte_size); |
38 | memset(this->bytes_, 0, byte_size); |
| 39 | } |
39 | } |
| 40 | 40 | ||
| 41 | Bitset::Bitset(const Byteset& set) |
41 | Bitset::Bitset(const Byteset& set) |
| 42 | { |
42 | { |
| 43 | this->count_ = set.size() * 8; |
43 | this->count_ = set.size() * 8; |
| 44 | this->bytes_ = new unsigned char[set.size()]; |
44 | this->bytes_ = new unsigned char[set.size()]; |
| 45 | 45 | ||
| - | 46 | for (unsigned int n = 0; n < set.size(); n++) |
|
| - | 47 | { |
|
| 46 |
|
48 | this->bytes_[n] = set[n]; |
| - | 49 | } |
|
| 47 | } |
50 | } |
| 48 | 51 | ||
| 49 | Bitset::~Bitset() |
52 | Bitset::~Bitset() |
| 50 | { |
53 | { |
| 51 | delete [] this->bytes_; |
54 | delete [] this->bytes_; |
| 52 | } |
55 | } |
| 53 | 56 | ||
| 54 | unsigned int Bitset::GetCount() |
57 | unsigned int Bitset::GetCount() |
| 55 | { |
58 | { |
| 56 | return this->count_; |
59 | return this->count_; |
| 57 | } |
60 | } |
| 58 | 61 | ||
| 59 | unsigned char* Bitset::GetBytes() const |
62 | unsigned char* Bitset::GetBytes() const |
| 60 | { |
63 | { |
| 61 | return this->bytes_; |
64 | return this->bytes_; |
| 62 | } |
65 | } |
| 63 | 66 | ||
| 64 | unsigned long Bitset::Read(unsigned int position, unsigned int length) |
67 | unsigned long Bitset::Read(unsigned int position, unsigned int length) |
| 65 | { |
68 | { |
| 66 | unsigned long value = 0; |
69 | unsigned long value = 0; |
| 67 | 70 | ||
| 68 | for (unsigned int index = 0; index < length; index++) |
71 | for (unsigned int index = 0; index < length; index++) |
| 69 | { |
72 | { |
| 70 | value = (value << 1) | this->Get(position + index); |
73 | value = (value << 1) | this->Get(position + index); |
| 71 | } |
74 | } |
| 72 | 75 | ||
| 73 | return value; |
76 | return value; |
| 74 | } |
77 | } |
| 75 | 78 | ||
| 76 | void Bitset::Write(unsigned int position, unsigned int length, long unsigned value) |
79 | void Bitset::Write(unsigned int position, unsigned int length, long unsigned value) |
| 77 | { |
80 | { |
| 78 | for (unsigned int index = 0; index < length; index++) |
81 | for (unsigned int index = 0; index < length; index++) |
| 79 | { |
82 | { |
| 80 | if (value & 0x01) |
83 | if (value & 0x01) |
| 81 | { |
84 | { |
| 82 | this->Set(position + (length - index - 1)); |
85 | this->Set(position + (length - index - 1)); |
| 83 | } |
86 | } |
| 84 | else |
87 | else |
| 85 | { |
88 | { |
| 86 | this->Unset(position + (length - index - 1)); |
89 | this->Unset(position + (length - index - 1)); |
| 87 | } |
90 | } |
| 88 | 91 | ||
| 89 | value = (value >> 1); |
92 | value = (value >> 1); |
| 90 | } |
93 | } |
| 91 | } |
94 | } |
| 92 | 95 | ||
| 93 | int Bitset::Set(unsigned int position) |
96 | int Bitset::Set(unsigned int position) |
| 94 | { |
97 | { |
| 95 | if (position >= this->count_) |
98 | if (position >= this->count_) |
| 96 | { |
99 | { |
| 97 | return -1; |
100 | return -1; |
| 98 | } |
101 | } |
| 99 | 102 | ||
| 100 | this->bytes_[position / 8] |= (0x00000001 << (7 - (position % 8))); |
103 | this->bytes_[position / 8] |= (0x00000001 << (7 - (position % 8))); |
| 101 | 104 | ||
| 102 | return 0; |
105 | return 0; |
| 103 | } |
106 | } |
| 104 | 107 | ||
| 105 | int Bitset::Unset(unsigned int position) |
108 | int Bitset::Unset(unsigned int position) |
| 106 | { |
109 | { |
| 107 | if (position >= this->count_) |
110 | if (position >= this->count_) |
| 108 | { |
111 | { |
| 109 | return -1; |
112 | return -1; |
| 110 | } |
113 | } |
| 111 | 114 | ||
| 112 | this->bytes_[position / 8] &= ~(0x00000001 << (7 - (position % 8))); |
115 | this->bytes_[position / 8] &= ~(0x00000001 << (7 - (position % 8))); |
| 113 | 116 | ||
| 114 | return 0; |
117 | return 0; |
| 115 | } |
118 | } |
| 116 | 119 | ||
| 117 | int Bitset::Get(unsigned int position) |
120 | int Bitset::Get(unsigned int position) |
| 118 | { |
121 | { |
| 119 | if (position >= this->count_) |
122 | if (position >= this->count_) |
| 120 | { |
123 | { |
| 121 | return -1; |
124 | return -1; |
| 122 | } |
125 | } |
| 123 | 126 | ||
| 124 | return (this->bytes_[position / 8] & (0x00000001 << (7 - (position % 8))) ? 1 : 0); |
127 | return (this->bytes_[position / 8] & (0x00000001 << (7 - (position % 8))) ? 1 : 0); |
| 125 | } |
128 | } |
| 126 | 129 | ||
| 127 | std::string Bitset::ToDebugString() |
130 | std::string Bitset::ToDebugString() |
| 128 | { |
131 | { |
| 129 | std::string debug_string; |
132 | std::string debug_string; |
| 130 | 133 | ||
| 131 | for (unsigned int n = 0; n < this->count_; n++) |
134 | for (unsigned int n = 0; n < this->count_; n++) |
| 132 | { |
135 | { |
| 133 | debug_string += boost::lexical_cast<std::string>(this->Get(n)); |
136 | debug_string += boost::lexical_cast<std::string>(this->Get(n)); |
| 134 | } |
137 | } |
| 135 | 138 | ||
| 136 | return debug_string; |
139 | return debug_string; |
| 137 | } |
140 | } |
| 138 | 141 | ||
| 139 | }; // namespace type |
142 | }; // namespace type |
| 140 | }; // namespace atom |
143 | }; // namespace atom |
| 141 | 144 | ||