dtinfo subtree dtinfo
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Basic / BitField.hh
1 /*
2  * $TOG: BitField.hh /main/5 1998/04/17 11:35:57 mgreess $
3  *
4  * Copyright (c) 1992 HAL Computer Systems International, Ltd.
5  * All rights reserved.  Unpublished -- rights reserved under
6  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
7  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
8  * OR DISCLOSURE.
9  * 
10  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
11  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
12  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
13  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
14  * INTERNATIONAL, LTD.
15  * 
16  *                         RESTRICTED RIGHTS LEGEND
17  * Use, duplication, or disclosure by the Government is subject
18  * to the restrictions as set forth in subparagraph (c)(l)(ii)
19  * of the Rights in Technical Data and Computer Software clause
20  * at DFARS 252.227-7013.
21  *
22  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
23  *                  1315 Dell Avenue
24  *                  Campbell, CA  95008
25  * 
26  */
27 #if defined(SVR4) || defined(hpux) || defined(linux)
28 #include <limits.h>
29 #else
30 #include <sys/limits.h>
31 #endif
32
33 const int bytes_per_word = sizeof(unsigned long);
34 const int bits_per_byte  = CHAR_BIT ;
35 const int bits_per_word = bytes_per_word * bits_per_byte ;
36
37 typedef unsigned long BitHandle ;
38
39 class BitField 
40 {
41 public:
42   BitField(unsigned long initial_bits = CHAR_BIT * sizeof(unsigned long) - 1) ; 
43   ~BitField();
44
45   void          return_bit(unsigned long bit); // give back previously
46                                                // allocated bit
47   BitHandle     get_handle();                  // allocate a bit for future use
48   void          free_handle(BitHandle);        // return bit for other use
49
50   /* -------- bit operations -------- */
51   bool  is_set(BitHandle);
52   bool  set(BitHandle);
53   bool  unset(BitHandle);
54
55
56   void          save();
57
58 #ifdef DEBUG
59   bool  is_used(BitHandle);
60   ostream      &dump(ostream &);
61 #endif
62
63 private:
64   /* -------- internal functions that do the real work -------- */
65   void check_allocation (unsigned long word);
66   bool  i_is_set(BitHandle);
67   bool  i_set(BitHandle);
68   bool  i_unset(BitHandle);
69
70
71   /* -------- suplementary (inline) functions -------- */
72   bool  word_has_zero_bit(unsigned long);
73   bool  byte_has_zero_bit(unsigned char byte);
74   unsigned char get_byte(int byte, unsigned long word);
75
76
77 private:
78   unsigned long         f_num_words ; // number of allocated machine words
79   unsigned long        *f_words ;     // array of words for bits
80   unsigned long        *f_bits_in_use ; // maps bits already allocated
81 };
82
83 // the assert(is_used(handle))  is commented out as OutlineLists do not
84 // allocate separate data handles for thier child lists - jbm
85
86 inline bool
87 BitField::is_set(BitHandle handle)
88 {
89   assert(handle);
90 //  assert(is_used(handle));
91   return i_is_set(handle);
92 }
93
94 inline bool
95 BitField::set(BitHandle handle)
96 {
97   assert(handle);
98 //  assert(is_used(handle));
99   return i_set(handle);
100 }
101
102 inline bool
103 BitField::unset(BitHandle handle)
104 {
105   assert(handle);
106 //  assert(is_used(handle));
107   return i_unset(handle);
108 }
109
110
111 inline bool
112 BitField::word_has_zero_bit(unsigned long word)
113 {
114   return !((word & ULONG_MAX) == ULONG_MAX);
115 }
116
117 inline bool
118 BitField::byte_has_zero_bit(unsigned char byte)
119 {
120   return !((byte & UCHAR_MAX) == UCHAR_MAX) ;
121 }
122
123 // return a byte within a word
124
125 inline unsigned char
126 BitField::get_byte(int byte, unsigned long word)
127 {
128   return (word >> (byte * bits_per_byte)) & UCHAR_MAX ;
129 }
130
131 #ifdef DEBUG
132 inline
133 ostream &
134 operator << (ostream &o, BitField &bf)
135 {
136   return bf.dump(o);
137 }
138
139 #endif