Rev 1596 | Rev 1598 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1596 | runge | 1 | /* |
| 2 | * |
||
| 3 | * Copyright (C) 2010 Mattias Runge |
||
| 4 | * |
||
| 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 |
||
| 7 | * the Free Software Foundation; either version 2 of the License, or |
||
| 8 | * (at your option) any later version. |
||
| 9 | * |
||
| 10 | * This program is distributed in the hope that it will be useful, |
||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU General Public License for more details. |
||
| 14 | * |
||
| 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., |
||
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | |||
| 21 | #include "Bitset.h" |
||
| 22 | |||
| 23 | #include <math.h> |
||
| 24 | #include <string.h> |
||
| 25 | |||
| 26 | namespace atom { |
||
| 27 | namespace type { |
||
| 28 | |||
| 29 | Bitset::Bitset(unsigned int count) |
||
| 30 | { |
||
| 31 | this->count_ = count; |
||
| 32 | this->bytes_ = new unsigned char[(int)ceil(count / 8)]; |
||
| 33 | } |
||
| 34 | |||
| 35 | Bitset::Bitset(const Byteset& set) |
||
| 36 | { |
||
| 37 | this->count_ = set.GetSize() * 8; |
||
| 38 | this->bytes_ = new unsigned char[set.GetSize()]; |
||
| 39 | |||
| 40 | memcpy(this->bytes_, set.Get(), set.GetSize()); |
||
| 41 | } |
||
| 42 | |||
| 43 | Bitset::~Bitset() |
||
| 44 | { |
||
| 45 | delete [] this->bytes_; |
||
| 46 | } |
||
| 47 | |||
| 48 | unsigned int Bitset::GetCount() |
||
| 49 | { |
||
| 50 | return this->count_; |
||
| 51 | } |
||
| 52 | |||
| 53 | unsigned long Bitset::Read(unsigned int position, unsigned int length) |
||
| 54 | { |
||
| 55 | unsigned long value = 0; |
||
| 56 | |||
| 57 | for (unsigned int index = 0; index < length; index++) |
||
| 58 | { |
||
| 59 | value = (value << 1) | this->Get(position + index); |
||
| 60 | } |
||
| 61 | |||
| 62 | return value; |
||
| 63 | } |
||
| 64 | |||
| 65 | int Bitset::Set(unsigned int position) |
||
| 66 | { |
||
| 67 | if (position >= this->count_) |
||
| 68 | { |
||
| 69 | return -1; |
||
| 70 | } |
||
| 71 | |||
| 1597 | runge | 72 | unsigned int byte_position = position / 8; |
| 73 | unsigned int bit_position = 7 - (position - (byte_position * 8)); |
||
| 74 | |||
| 75 | this->bytes_[byte_position] |= (0x00000001 << (bit_position)); |
||
| 1596 | runge | 76 | |
| 77 | return 0; |
||
| 78 | } |
||
| 79 | |||
| 80 | int Bitset::Unset(unsigned int position) |
||
| 81 | { |
||
| 82 | if (position >= this->count_) |
||
| 83 | { |
||
| 84 | return -1; |
||
| 85 | } |
||
| 86 | |||
| 1597 | runge | 87 | unsigned int byte_position = position / 8; |
| 88 | unsigned int bit_position = 7 - (position - (byte_position * 8)); |
||
| 1596 | runge | 89 | |
| 1597 | runge | 90 | this->bytes_[byte_position] &= ~(0x00000001 << (bit_position)); |
| 91 | |||
| 1596 | runge | 92 | return 0; |
| 93 | } |
||
| 94 | |||
| 95 | int Bitset::Get(unsigned int position) |
||
| 96 | { |
||
| 97 | if (position >= this->count_) |
||
| 98 | { |
||
| 99 | return -1; |
||
| 100 | } |
||
| 101 | |||
| 1597 | runge | 102 | unsigned int byte_position = position / 8; |
| 103 | unsigned int bit_position = 7 - (position - (byte_position * 8)); |
||
| 104 | |||
| 105 | return (this->bytes_[byte_position] & (0x00000001 << bit_position) ? 1 : 0); |
||
| 1596 | runge | 106 | } |
| 107 | |||
| 108 | }; // namespace type |
||
| 109 | }; // namespace atom |