1
/*      Copyright (C) 2004 Garrett A. Kajmowicz
 
 
3
        This file is part of the uClibc++ Library.
 
 
5
        This library is free software; you can redistribute it and/or
 
 
6
        modify it under the terms of the GNU Lesser General Public
 
 
7
        License as published by the Free Software Foundation; either
 
 
8
        version 2.1 of the License, or (at your option) any later version.
 
 
10
        This library is distributed in the hope that it will be useful,
 
 
11
        but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
12
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
 
13
        Lesser General Public License for more details.
 
 
15
        You should have received a copy of the GNU Lesser General Public
 
 
16
        License along with this library; if not, write to the Free Software
 
 
17
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
20
#include <basic_definitions>
 
 
23
#include <func_exception>
 
 
27
#ifndef __STD_BITSET_HEADER
 
 
28
#define __STD_BITSET_HEADER 1
 
 
30
#pragma GCC visibility push(default)
 
 
33
        template <size_t N> class bitset;
 
 
36
        template <size_t N> bitset<N> operator&(const bitset<N>&, const bitset<N>&);
 
 
37
        template <size_t N> bitset<N> operator|(const bitset<N>&, const bitset<N>&);
 
 
38
        template <size_t N> bitset<N> operator^(const bitset<N>&, const bitset<N>&);
 
 
39
        template <class charT, class traits, size_t N> basic_istream<charT, traits>& 
 
 
40
                operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
 
 
42
        template <class charT, class traits, size_t N> basic_ostream<charT, traits>& 
 
 
43
                operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
 
 
48
        template<size_t N> class _UCXXEXPORT bitset {
 
 
50
                //Number of characters allocated to hold the bits
 
 
51
                static const size_t WORD_SIZE = CHAR_BIT;               //Use int maybe?
 
 
52
                static const size_t num_bytes = (N + WORD_SIZE - 1) / WORD_SIZE;
 
 
54
                //From the bit number, figure out which byte we are working with
 
 
55
                size_t byte_num(size_t bit_num) const{ 
 
 
57
                                return (bit_num >> 3);
 
 
60
                                return (bit_num >> 4);
 
 
63
                                return (bit_num >> 5);
 
 
66
                                return (bit_num >> 6);
 
 
68
                        return bit_num / WORD_SIZE;
 
 
70
                //From the bit number, figure out which bit inside the byte we need
 
 
71
                size_t bit_num(const size_t bit_num) const{
 
 
72
                        return bit_num % WORD_SIZE;
 
 
76
                //Point to the actual data
 
 
80
                class _UCXXEXPORT reference {
 
 
82
                        reference() : bit_num(0), parent(0) {  }
 
 
87
                        reference& operator=(bool x){                   // for b[i] = x;
 
 
88
                                parent->set(bit_num, x);
 
 
91
                        reference& operator=(const reference& x){       // for b[i] = b[j];
 
 
92
                                parent->set(bit_num, x);
 
 
95
                        bool operator~() const{                         // flips the bit
 
 
96
                                return !parent->test(bit_num);
 
 
98
                        operator bool() const{                          // for x = b[i];
 
 
99
                                return parent->test(bit_num);
 
 
101
                        reference& flip(){                              // for b[i].flip();
 
 
102
                                parent->flip(bit_num);
 
 
110
                bitset(unsigned long val){
 
 
112
                        size_t count = sizeof(val) * CHAR_BIT;
 
 
116
                        for(size_t i = 0; i < count; ++i){
 
 
117
                                set(i, ((val >> i) & 1));
 
 
121
                bitset(const bitset & val){
 
 
122
                        for(size_t i = 0; i < num_bytes; ++i){
 
 
123
                                data[i] = val.data[i];
 
 
127
                template<class charT, class traits, class Allocator> _UCXXEXPORT
 
 
129
                        const basic_string<charT,traits,Allocator>& str,
 
 
130
                        typename basic_string<charT,traits,Allocator>::size_type pos = 0,
 
 
131
                        typename basic_string<charT,traits,Allocator>::size_type n =
 
 
132
                        basic_string<charT>::npos
 
 
136
                        if(n > str.length()){
 
 
140
                        if (width + pos > str.length()){
 
 
141
                                width = str.length() - pos;
 
 
144
                        for(size_t i = 0; i < width; ++i){
 
 
145
                                switch(str[pos + width - i - 1]){
 
 
152
                                                __throw_invalid_argument();
 
 
157
                bitset<N>& operator&=(const bitset<N>& rhs){
 
 
158
                        for(size_t i =0; i < num_bytes; ++i){
 
 
159
                                data[i] &= rhs.data[i];
 
 
164
                bitset<N>& operator|=(const bitset<N>& rhs){
 
 
165
                        for(size_t i =0; i < num_bytes; ++i){
 
 
166
                                data[i] |= rhs.data[i];
 
 
170
                bitset<N>& operator^=(const bitset<N>& rhs){
 
 
171
                        for(size_t i=0; i < num_bytes; ++i){
 
 
172
                                data[i] ^= rhs.data[i];
 
 
177
                bitset<N>& operator<<=(size_t pos){
 
 
178
                        for(size_t i = N-1; i >=pos; --i){
 
 
179
                                set(i, test(i - pos));
 
 
181
                        for(size_t i = 0; i < pos; ++i){
 
 
187
                bitset<N>& operator>>=(size_t pos){
 
 
188
                        for(size_t i = 0; i < (N - pos); ++i){
 
 
189
                                set(i, test(i + pos));
 
 
191
                        for(size_t i = pos; i > 0; --i){
 
 
199
                        for(i = 0; i < N ; ++i){
 
 
200
                                data[byte_num(i)] |= (1<<bit_num(i));
 
 
204
                bitset<N>& set(size_t pos, int val = true){
 
 
206
                                data[byte_num(pos)] |= (1<<bit_num(pos));
 
 
208
                                data[byte_num(pos)] &= ~(1<<bit_num(pos));
 
 
213
                        for(size_t i = 0; i <= num_bytes; ++i){
 
 
218
                bitset<N>& reset(size_t pos){
 
 
219
                        data[byte_num(pos)] &= ~(1<<bit_num(pos));
 
 
222
                bitset<N>  operator~() const{
 
 
223
                        bitset<N> retval(*this);
 
 
229
                        for(size_t i = 0; i <= num_bytes; ++i){
 
 
234
                bitset<N>& flip(size_t pos){
 
 
235
                        char temp = data[byte_num(pos)] & (1 << bit_num(pos));
 
 
236
                        if(temp == 0){  //Bit was 0
 
 
237
                                data[byte_num(pos)] |= (1 << bit_num(pos));
 
 
239
                                data[byte_num(pos)] &= ~(1<<bit_num(pos));
 
 
244
                reference operator[](size_t pos){   // for b[i];
 
 
246
                        retval.parent = this;
 
 
247
                        retval.bit_num = pos;
 
 
251
                unsigned long to_ulong() const{
 
 
252
                        if(N > sizeof(unsigned long) * CHAR_BIT){
 
 
253
                                __throw_overflow_error();
 
 
255
                        unsigned long retval = 0;
 
 
257
                        for(size_t i = count; i > 0; --i){
 
 
269
                template <class charT, class traits, class Allocator>
 
 
270
                        basic_string<charT, traits, Allocator> to_string() const
 
 
272
                        basic_string<charT, traits, Allocator> retval;
 
 
274
                        for(size_t i = N ; i > 0; --i){
 
 
275
                                if(test(i-1) == true){
 
 
285
                size_t count() const{
 
 
287
                        for(size_t i =0; i < N; ++i){
 
 
298
                bitset<N>& operator=(const bitset<N> & rhs){
 
 
302
                        for(size_t i = 0; i <= byte_num(N); ++i){
 
 
303
                                data[i] = rhs.data[i];
 
 
309
                bool operator==(const bitset<N>& rhs) const{
 
 
310
                        for(size_t i =0; i< N; ++i){
 
 
311
                                if(test(i) != rhs.test(i)){
 
 
318
                bool operator!=(const bitset<N>& rhs) const{
 
 
319
                        for(size_t i =0; i< N; ++i){
 
 
320
                                if(test(i) != rhs.test(i)){
 
 
327
                bool test(size_t pos) const{
 
 
328
                        return (data[byte_num(pos)] & (1<<bit_num(pos)) ) != 0;
 
 
332
                        for(size_t i = 0; i< N; ++i){
 
 
347
                bitset<N> operator<<(size_t pos) const{
 
 
348
                        bitset retval(*this);
 
 
352
                bitset<N> operator>>(size_t pos) const{
 
 
353
                        bitset retval(*this);
 
 
359
        //Non-member functions
 
 
362
        template <size_t N> _UCXXEXPORT bitset<N> operator&(const bitset<N>& lhs, const bitset<N>& rhs){
 
 
363
                bitset<N> retval(lhs);
 
 
368
        template <size_t N> _UCXXEXPORT bitset<N> operator|(const bitset<N>& lhs, const bitset<N>& rhs){
 
 
369
                bitset<N> retval(lhs);
 
 
374
        template <size_t N> _UCXXEXPORT bitset<N> operator^(const bitset<N>& lhs, const bitset<N>& rhs){
 
 
375
                bitset<N> retval(lhs);
 
 
380
        template <class charT, class traits, size_t N> _UCXXEXPORT basic_istream<charT, traits>& 
 
 
381
                operator>>(basic_istream<charT, traits>& is, bitset<N>& x)
 
 
385
                for(size_t i = 0; i < N; ++i){
 
 
390
                        if(c != '1' && c !='0'){
 
 
402
        template <class charT, class traits, size_t N> _UCXXEXPORT basic_ostream<charT, traits>& 
 
 
403
                operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x)
 
 
405
                for(size_t i = N ; i > 0; --i){
 
 
406
                        if(x.test(i-1) == true){
 
 
420
#pragma GCC visibility pop