-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmybitset.h
98 lines (73 loc) · 1.42 KB
/
mybitset.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#ifndef _MYBITSET_H
#define _MYBITSET_H
#include "mytool.h"
#include <bitset>
#include <stdlib.h>
class mybitset{
public:
mybitset();
mybitset(const mybitset& rhs);
mybitset& operator =(const mybitset& rhs);
~mybitset();
void push_back(int bit);
void pop();
void SetValue(int bit, int i);
int GetValue(int i);
int size();
private:
char* m_Ptr;
size_t m_Alloc;
size_t m_Len;
};
mybitset::mybitset()
{
m_Ptr = (char*)malloc(1);
m_Alloc = bitsOfByte * 1;
m_Len = 0;
}
mybitset::mybitset(const mybitset& rhs)
{
m_Ptr = (char*)malloc(rhs.m_Alloc / bitsOfByte);
memcpy(m_Ptr, rhs.m_Ptr, rhs.m_Len);
m_Alloc = rhs.m_Alloc;
m_Len = rhs.m_Len;
}
mybitset& mybitset::operator =(const mybitset& rhs)
{
m_Ptr = (char*)malloc(rhs.m_Alloc / bitsOfByte);
memcpy(m_Ptr, rhs.m_Ptr, rhs.m_Len);
m_Alloc = rhs.m_Alloc;
m_Len = rhs.m_Len;
return *this;
}
mybitset::~mybitset()
{
free(m_Ptr);
}
inline int mybitset::size()
{
return m_Len;
}
inline void mybitset::pop()
{
m_Len--;
}
inline void mybitset::push_back(int bit)
{
if(m_Len == m_Alloc)
{
m_Ptr = (char*)realloc(m_Ptr, m_Alloc / bitsOfByte * 2);
m_Alloc *= 2;
}
SetByte(m_Ptr[m_Len / bitsOfByte], m_Len % bitsOfByte, bit);
m_Len++;
}
inline int mybitset::GetValue(int i)
{
return GetByte(m_Ptr[i / bitsOfByte], i % bitsOfByte);
}
inline void mybitset::SetValue(int bit, int i)
{
SetByte(m_Ptr[i / bitsOfByte], i % bitsOfByte, bit);
}
#endif