3 * Summary: Bit array data type.
4 * Created by: Robert Vollmert
13 #define LONGSIZE (sizeof(unsigned long)*8)
15 bit_array::bit_array(unsigned long s
)
18 nwords
= static_cast<int>((size
+ LONGSIZE
- 1) / LONGSIZE
);
19 data
= new unsigned long[nwords
];
23 bit_array::~bit_array()
28 void bit_array::reset()
30 for (int w
= 0; w
< nwords
; ++w
)
34 bool bit_array::get(unsigned long index
) const
37 int w
= index
/ LONGSIZE
;
38 int b
= index
% LONGSIZE
;
39 return (data
[w
] & (1UL << b
));
42 void bit_array::set(unsigned long index
, bool value
)
45 int w
= index
/ LONGSIZE
;
46 int b
= index
% LONGSIZE
;
48 data
[w
] |= (1UL << b
);
50 data
[w
] &= ~(1UL << b
);
53 bit_array
& bit_array::operator |= (const bit_array
& other
)
55 ASSERT(size
== other
.size
);
56 for (int w
= 0; w
< nwords
; ++w
)
57 data
[w
] |= other
.data
[w
];
61 bit_array
& bit_array::operator &= (const bit_array
& other
)
63 ASSERT(size
== other
.size
);
64 for (int w
= 0; w
< nwords
; ++w
)
65 data
[w
] &= other
.data
[w
];
69 bit_array
bit_array::operator & (const bit_array
& other
) const
71 ASSERT(size
== other
.size
);
72 bit_array res
= bit_array(size
);
73 for (int w
= 0; w
< nwords
; ++w
)
74 res
.data
[w
] = data
[w
] & other
.data
[w
];