Subversion Repositories HomeAutomation

Rev

Rev 1642 | Rev 1990 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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