2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * $TOG: BitField.hh /main/5 1998/04/17 11:35:57 mgreess $
26 * Copyright (c) 1992 HAL Computer Systems International, Ltd.
27 * All rights reserved. Unpublished -- rights reserved under
28 * the Copyright Laws of the United States. USE OF A COPYRIGHT
29 * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
32 * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33 * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. USE,
34 * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35 * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
38 * RESTRICTED RIGHTS LEGEND
39 * Use, duplication, or disclosure by the Government is subject
40 * to the restrictions as set forth in subparagraph (c)(l)(ii)
41 * of the Rights in Technical Data and Computer Software clause
42 * at DFARS 252.227-7013.
44 * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
49 #if defined(SVR4) || defined(hpux) || defined(__linux__) || defined(__NetBSD__)
52 #include <sys/limits.h>
55 const int bytes_per_word = sizeof(unsigned long);
56 const int bits_per_byte = CHAR_BIT ;
57 const int bits_per_word = bytes_per_word * bits_per_byte ;
59 typedef unsigned long BitHandle ;
64 BitField(unsigned long initial_bits = CHAR_BIT * sizeof(unsigned long) - 1) ;
67 void return_bit(unsigned long bit); // give back previously
69 BitHandle get_handle(); // allocate a bit for future use
70 void free_handle(BitHandle); // return bit for other use
72 /* -------- bit operations -------- */
73 bool is_set(BitHandle);
75 bool unset(BitHandle);
81 bool is_used(BitHandle);
82 ostream &dump(ostream &);
86 /* -------- internal functions that do the real work -------- */
87 void check_allocation (unsigned long word);
88 bool i_is_set(BitHandle);
89 bool i_set(BitHandle);
90 bool i_unset(BitHandle);
93 /* -------- suplementary (inline) functions -------- */
94 bool word_has_zero_bit(unsigned long);
95 bool byte_has_zero_bit(unsigned char byte);
96 unsigned char get_byte(int byte, unsigned long word);
100 unsigned long f_num_words ; // number of allocated machine words
101 unsigned long *f_words ; // array of words for bits
102 unsigned long *f_bits_in_use ; // maps bits already allocated
105 // the assert(is_used(handle)) is commented out as OutlineLists do not
106 // allocate separate data handles for thier child lists - jbm
109 BitField::is_set(BitHandle handle)
112 // assert(is_used(handle));
113 return i_is_set(handle);
117 BitField::set(BitHandle handle)
120 // assert(is_used(handle));
121 return i_set(handle);
125 BitField::unset(BitHandle handle)
128 // assert(is_used(handle));
129 return i_unset(handle);
134 BitField::word_has_zero_bit(unsigned long word)
136 return !((word & ULONG_MAX) == ULONG_MAX);
140 BitField::byte_has_zero_bit(unsigned char byte)
142 return !((byte & UCHAR_MAX) == UCHAR_MAX) ;
145 // return a byte within a word
148 BitField::get_byte(int byte, unsigned long word)
150 return (word >> (byte * bits_per_byte)) & UCHAR_MAX ;
156 operator << (ostream &o, BitField &bf)