Subversion Repositories HomeAutomation

Rev

Rev 1597 | Rev 1642 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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