Subversion Repositories HomeAutomation

Rev

Rev 1990 | 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
 
1598 runge 26
#include <boost/lexical_cast.hpp>
27
 
1596 runge 28
namespace atom {
1642 runge 29
namespace common {
1596 runge 30
 
31
Bitset::Bitset(unsigned int count)
32
{
1598 runge 33
    unsigned int byte_size = (int)ceil(count / 8);
34
 
1596 runge 35
    this->count_ = count;
1598 runge 36
    this->bytes_ = new unsigned char[byte_size];
37
 
38
    memset(this->bytes_, 0, byte_size);
1596 runge 39
}
40
 
41
Bitset::Bitset(const Byteset& set)
42
{
1987 runge 43
    this->count_ = set.size() * 8;
44
    this->bytes_ = new unsigned char[set.size()];
1596 runge 45
 
1990 runge 46
    for (unsigned int n = 0; n < set.size(); n++)
47
    {
48
      this->bytes_[n] = set[n];
49
    }
1596 runge 50
}
51
 
52
Bitset::~Bitset()
53
{
54
    delete [] this->bytes_;
55
}
56
 
57
unsigned int Bitset::GetCount()
58
{
59
    return this->count_;
60
}
61
 
1598 runge 62
unsigned char* Bitset::GetBytes() const
63
{
64
    return this->bytes_;
65
}
66
 
2118 runge 67
uint64_t Bitset::Read(unsigned int position, unsigned int length)
1596 runge 68
{
2118 runge 69
    uint64_t value = 0;
1596 runge 70
 
71
    for (unsigned int index = 0; index < length; index++)
72
    {
73
        value = (value << 1) | this->Get(position + index);
74
    }
75
 
76
    return value;
77
}
78
 
2118 runge 79
void Bitset::Write(unsigned int position, unsigned int length, uint64_t value)
1598 runge 80
{
81
    for (unsigned int index = 0; index < length; index++)
82
    {
83
        if (value & 0x01)
84
        {
85
            this->Set(position + (length - index - 1));
86
        }
87
        else
88
        {
89
            this->Unset(position + (length - index - 1));
90
        }
91
 
92
        value = (value >> 1);
93
    }
94
}
95
 
1596 runge 96
int Bitset::Set(unsigned int position)
97
{
98
    if (position >= this->count_)
99
    {
100
        return -1;
101
    }
102
 
1598 runge 103
    this->bytes_[position / 8] |= (0x00000001 << (7 - (position % 8)));
1596 runge 104
 
105
    return 0;
106
}
107
 
108
int Bitset::Unset(unsigned int position)
109
{
110
    if (position >= this->count_)
111
    {
112
        return -1;
113
    }
114
 
1598 runge 115
    this->bytes_[position / 8] &= ~(0x00000001 << (7 - (position % 8)));
1596 runge 116
 
117
    return 0;
118
}
119
 
120
int Bitset::Get(unsigned int position)
121
{
122
    if (position >= this->count_)
123
    {
124
        return -1;
125
    }
126
 
1598 runge 127
    return (this->bytes_[position / 8] & (0x00000001 << (7 - (position % 8))) ? 1 : 0);
128
}
129
 
130
std::string Bitset::ToDebugString()
131
{
132
    std::string debug_string;
1597 runge 133
 
1598 runge 134
    for (unsigned int n = 0; n < this->count_; n++)
135
    {
136
        debug_string += boost::lexical_cast<std::string>(this->Get(n));
137
    }
138
 
139
    return debug_string;
1596 runge 140
}
141
 
142
}; // namespace type
143
}; // namespace atom