Subversion Repositories HomeAutomation

Rev

Rev 1990 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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