1 //------------------------------------------------------------------------
2 //  BIT VECTORS
3 //------------------------------------------------------------------------
4 //
5 //  Eureka DOOM Editor
6 //
7 //  Copyright (C) 2001-2015 Andrew Apted
8 //  Copyright (C) 1997-2003 André Majorel et al
9 //
10 //  This program is free software; you can redistribute it and/or
11 //  modify it under the terms of the GNU General Public License
12 //  as published by the Free Software Foundation; either version 2
13 //  of the License, or (at your option) any later version.
14 //
15 //  This program is distributed in the hope that it will be useful,
16 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 //  GNU General Public License for more details.
19 //
20 //------------------------------------------------------------------------
21 //
22 //  Based on Yadex which incorporated code from DEU 5.21 that was put
23 //  in the public domain in 1994 by Raphaël Quinet and Brendon Wyber.
24 //
25 //------------------------------------------------------------------------
26 
27 #ifndef __EUREKA_M_BITVEC_H__
28 #define __EUREKA_M_BITVEC_H__
29 
30 
31 typedef enum
32 {
33 	BOP_ADD = 0,   // Add to selection
34 	BOP_REMOVE,    // Remove from selection
35 	BOP_TOGGLE     // If not in selection, add it, else remove it
36 }
37 sel_op_e;
38 
39 
40 class bitvec_c
41 {
42 	//
43 	// Although this bit-vector has a current size, it acts as though it
44 	// was infinitely sized and all bits past the end are zero.  When
45 	// setting a bit past the end, it will automatically resize itself.
46 	//
47 
48 private:
49 	int num_elem;
50 
51 	byte *d;
52 
53 public:
54 	 bitvec_c(int n_elements = 64);
55 	~bitvec_c();
56 
size()57 	inline int size() const
58 	{
59 		return num_elem;
60 	}
61 
62 	// this preserves existing elements
63 	void resize(int n_elements);
64 
65 	bool get(int n) const;	// Get bit <n>
66 
67 	void set(int n);		// Set bit <n> to 1
68 	void clear(int n);		// Set bit <n> to 0
69 	void toggle(int n);		// Toggle bit <n>
70 
71 	void frob(int n, sel_op_e op);
72 
73 	void set_all();
74 	void clear_all();
75 	void toggle_all();
76 
77 private:
78 	// deliberately don't implement copying
79 	bitvec_c(const bitvec_c& other);
80 
81 private:
82 	/* NOTE : these functions do no range checking! */
83 
raw_get(int n)84 	inline bool raw_get(int n) const
85 	{
86 		return (d[n >> 3] & (1 << (n & 7))) ? true : false;
87 	}
88 
raw_set(int n)89 	inline void raw_set(int n)
90 	{
91 		d[n >> 3] |= (1 << (n & 7));
92 	}
93 
raw_clear(int n)94 	inline void raw_clear(int n)
95 	{
96 		d[n >> 3] &= ~(1 << (n & 7));
97 	}
98 
raw_toggle(int n)99 	inline void raw_toggle(int n)
100 	{
101 		d[n >> 3] ^= (1 << (n & 7));
102 	}
103 };
104 
105 
106 #endif  /* __EUREKA_M_BITVEC_H__ */
107 
108 //--- editor settings ---
109 // vi:ts=4:sw=4:noexpandtab
110