forked from farisawan-2000/cfront-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBits.h
157 lines (135 loc) · 4.5 KB
/
Bits.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*ident "@(#)cls4:src/Bits.h 1.4" */
/*******************************************************************************
C++ source for the C++ Language System, Release 3.0. This product
is a new release of the original cfront developed in the computer
science research center of AT&T Bell Laboratories.
Copyright (c) 1993 UNIX System Laboratories, Inc.
Copyright (c) 1991, 1992 AT&T and UNIX System Laboratories, Inc.
Copyright (c) 1984, 1989, 1990 AT&T. All Rights Reserved.
THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE of AT&T and UNIX System
Laboratories, Inc. The copyright notice above does not evidence
any actual or intended publication of such source code.
*******************************************************************************/
#ifndef _BITS_H
#define _BITS_H
#include "Block.h"
typedef unsigned long Bits_chunk;
static const int Bits_shift_ATTLC = 5;
static const int Bits_len_ATTLC = 1 << Bits_shift_ATTLC;
static const int Bits_mask_ATTLC = Bits_len_ATTLC - 1;
Blockdeclare(Bits_chunk)
class Bits {
private:
Block(Bits_chunk) b;
unsigned n;
// the chunk number that contains bit n
unsigned chunk(unsigned n) const {
return n >> Bits_shift_ATTLC;
}
// the number of chunks needed to contain an n-bit string
unsigned bound(unsigned n) const {
return (n + Bits_len_ATTLC - 1) >> Bits_shift_ATTLC;
}
// a pointer to the (non-existent) chunk immediately
// after the last one in this Bits
Bits_chunk *limit() {
return (Bits_chunk *) b + bound(n);
}
const Bits_chunk *limit() const {
return (const Bits_chunk *) b + bound(n);
}
// turn off unused high-order bits in the high-order chunk
void normalize() {
register int ct = n & Bits_mask_ATTLC;
if (ct)
b[chunk(n)] &= ~(~Bits_chunk(0) << ct);
}
int compare(const Bits &) const;
int equal(const Bits &) const;
public:
Bits() {
n = 0;
}
Bits(Bits_chunk, unsigned = 1);
unsigned size() const {
return n;
}
unsigned size(unsigned);
friend Bits operator&(const Bits &, const Bits &);
friend Bits operator|(const Bits &, const Bits &);
friend Bits operator^(const Bits &, const Bits &);
friend Bits operator~(const Bits &);
friend Bits operator<<(const Bits &, int);
friend Bits operator>>(const Bits &, int);
friend inline int operator<(const Bits &, const Bits &);
friend inline int operator>(const Bits &, const Bits &);
friend inline int operator<=(const Bits &, const Bits &);
friend inline int operator>=(const Bits &, const Bits &);
friend inline int operator==(const Bits &, const Bits &);
friend inline int operator!=(const Bits &, const Bits &);
Bits &operator&=(const Bits &);
Bits &operator|=(const Bits &);
Bits &operator^=(const Bits &);
Bits &operator<<=(int);
Bits &operator>>=(int);
Bits & compl();
Bits &concat(const Bits &);
Bits &set(unsigned i) {
if (i < n)
b[chunk(i)] |= Bits_chunk(1) << (i & Bits_mask_ATTLC);
return *this;
}
Bits &set(unsigned i, unsigned long x) {
if (i < n) {
register Bits_chunk *p = &b[chunk(i)];
register Bits_chunk mask = Bits_chunk(1) << (i & Bits_mask_ATTLC);
if (x)
*p |= mask;
else
*p &= ~mask;
}
return *this;
}
Bits &reset(unsigned i) {
if (i < n)
b[chunk(i)] &= ~(Bits_chunk(1) << (i & Bits_mask_ATTLC));
return *this;
}
Bits & compl(unsigned i) {
if (i < n)
b[chunk(i)] ^= Bits_chunk(1) << (i & Bits_mask_ATTLC);
return *this;
}
unsigned count() const;
operator Bits_chunk() const;
int operator[](unsigned i) const {
if (i >= n)
return 0;
else
return (b[chunk(i)] >> (i & Bits_mask_ATTLC)) & 1;
}
unsigned signif() const;
unsigned trim() {
return size(signif());
}
};
inline int operator<(const Bits &a, const Bits &b) {
return a.compare(b) < 0;
}
inline int operator>(const Bits &a, const Bits &b) {
return a.compare(b) > 0;
}
inline int operator<=(const Bits &a, const Bits &b) {
return a.compare(b) <= 0;
}
inline int operator>=(const Bits &a, const Bits &b) {
return a.compare(b) >= 0;
}
inline int operator==(const Bits &a, const Bits &b) {
return a.equal(b);
}
inline int operator!=(const Bits &a, const Bits &b) {
return !a.equal(b);
}
Bits concat(const Bits &, const Bits &);
#endif