home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / bvector.h < prev    next >
C/C++ Source or Header  |  1996-10-12  |  12KB  |  422 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  */
  15.  
  16. // vector<bool> is replaced by bit_vector at present because bool is not
  17. // implemented.  
  18.  
  19. #ifndef BVECTOR_H
  20. #define BVECTOR_H
  21.  
  22. #include <function.h>
  23. #include <algobase.h>
  24. #include <iterator.h>
  25. #ifndef __GNUG__
  26. #include <bool.h>
  27. #endif
  28.  
  29. #ifndef Allocator
  30. #define Allocator allocator
  31. #include <defalloc.h>
  32. #endif
  33.  
  34. #define __WORD_BIT (int(CHAR_BIT*sizeof(unsigned int)))
  35.  
  36. class bit_vector {
  37. public:
  38.     typedef Allocator<unsigned int> vector_allocator;
  39.     typedef bool value_type;
  40.     typedef vector_allocator::size_type size_type;
  41.     typedef vector_allocator::difference_type difference_type; 
  42.  
  43.     class iterator;
  44.     class const_iterator;
  45.  
  46.     class reference {
  47.     friend class iterator;
  48.     friend class const_iterator;
  49.     protected:
  50.     unsigned int* p;
  51.     unsigned int mask;
  52.     reference(unsigned int* x, unsigned int y) : p(x), mask(y) {}
  53.     public:
  54.     reference() : p(0), mask(0) {}
  55.     operator bool() const { return !(!(*p & mask)); }
  56.     reference& operator=(bool x) {
  57.         if (x)      
  58.         *p |= mask;
  59.         else 
  60.         *p &= ~mask;
  61.         return *this;
  62.     }
  63.     reference& operator=(const reference& x) { return *this = bool(x); }
  64.     bool operator==(const reference& x) const {
  65.         return bool(*this) == bool(x);
  66.     }
  67.     bool operator<(const reference& x) const {
  68.         return bool(*this) < bool(x);
  69.     }
  70.     void flip() { *p ^= mask; }
  71.     };
  72.     typedef bool const_reference;
  73.     class iterator : public random_access_iterator<bool, difference_type> {
  74.     friend class bit_vector;
  75.     friend class const_iterator;
  76.     protected:
  77.     unsigned int* p;
  78.     unsigned int offset;
  79.     void bump_up() {
  80.         if (offset++ == __WORD_BIT - 1) {
  81.         offset = 0;
  82.         ++p;
  83.         }
  84.     }
  85.     void bump_down() {
  86.     if (offset-- == 0) {
  87.         offset = __WORD_BIT - 1;
  88.         --p;
  89.     }
  90.     }
  91.     public:
  92.     iterator() : p(0), offset(0) {}
  93.     iterator(unsigned int* x, unsigned int y) : p(x), offset(y) {}
  94.     reference operator*() const { return reference(p, 1U << offset); }
  95.     iterator& operator++() {
  96.         bump_up();
  97.         return *this;
  98.     }
  99.     iterator operator++(int) {
  100.         iterator tmp = *this;
  101.         bump_up();
  102.         return tmp;
  103.     }
  104.     iterator& operator--() {
  105.         bump_down();
  106.         return *this;
  107.     }
  108.     iterator operator--(int) {
  109.         iterator tmp = *this;
  110.         bump_down();
  111.         return tmp;
  112.     }
  113.     iterator& operator+=(difference_type i) {
  114.         difference_type n = i + offset;
  115.         p += n / __WORD_BIT;
  116.         n = n % __WORD_BIT;
  117.         if (n < 0) {
  118.         offset = n + __WORD_BIT;
  119.         --p;
  120.         } else
  121.         offset = n;
  122.         return *this;
  123.     }
  124.     iterator& operator-=(difference_type i) {
  125.         *this += -i;
  126.         return *this;
  127.     }
  128.     iterator operator+(difference_type i) const {
  129.         iterator tmp = *this;
  130.         return tmp += i;
  131.     }
  132.     iterator operator-(difference_type i) const {
  133.         iterator tmp = *this;
  134.         return tmp -= i;
  135.     }
  136.     difference_type operator-(iterator x) const {
  137.         return __WORD_BIT * (p - x.p) + offset - x.offset;
  138.     }
  139.     reference operator[](difference_type i) { return *(*this + i); }
  140.     bool operator==(const iterator& x) const {
  141.         return p == x.p && offset == x.offset;
  142.     }
  143.     bool operator<(iterator x) const {
  144.         return p < x.p || (p == x.p && offset < x.offset);
  145.     }
  146.     };
  147.  
  148.     class const_iterator 
  149.     : public random_access_iterator<bool, difference_type> {
  150.     friend class bit_vector;
  151.     protected:
  152.     unsigned int* p;
  153.     unsigned int offset;
  154.     void bump_up() {
  155.         if (offset++ == __WORD_BIT - 1) {
  156.         offset = 0;
  157.         ++p;
  158.         }
  159.     }
  160.     void bump_down() {
  161.     if (offset-- == 0) {
  162.         offset = __WORD_BIT - 1;
  163.         --p;
  164.     }
  165.     }
  166.     public:
  167.     const_iterator() : p(0), offset(0) {}
  168.     const_iterator(unsigned int* x, unsigned int y) : p(x), offset(y) {}
  169.     const_iterator(const iterator& x) : p(x.p), offset(x.offset) {}
  170.     const_reference operator*() const {
  171.         return reference(p, 1U << offset);
  172.     }
  173.     const_iterator& operator++() {
  174.         bump_up();
  175.         return *this;
  176.     }
  177.     const_iterator operator++(int) {
  178.         const_iterator tmp = *this;
  179.         bump_up();
  180.         return tmp;
  181.     }
  182.     const_iterator& operator--() {
  183.         bump_down();
  184.         return *this;
  185.     }
  186.     const_iterator operator--(int) {
  187.         const_iterator tmp = *this;
  188.         bump_down();
  189.         return tmp;
  190.     }
  191.     const_iterator& operator+=(difference_type i) {
  192.         difference_type n = i + offset;
  193.         p += n / __WORD_BIT;
  194.         n = n % __WORD_BIT;
  195.         if (n < 0) {
  196.         offset = n + __WORD_BIT;
  197.         --p;
  198.         } else
  199.         offset = n;
  200.         return *this;
  201.     }
  202.     const_iterator& operator-=(difference_type i) {
  203.         *this += -i;
  204.         return *this;
  205.     }
  206.     const_iterator operator+(difference_type i) const {
  207.         const_iterator tmp = *this;
  208.         return tmp += i;
  209.     }
  210.     const_iterator operator-(difference_type i) const {
  211.         const_iterator tmp = *this;
  212.         return tmp -= i;
  213.     }
  214.     difference_type operator-(const_iterator x) const {
  215.         return __WORD_BIT * (p - x.p) + offset - x.offset;
  216.     }
  217.     const_reference operator[](difference_type i) { 
  218.         return *(*this + i); 
  219.     }
  220.     bool operator==(const const_iterator& x) const {
  221.         return p == x.p && offset == x.offset;
  222.     }
  223.     bool operator<(const_iterator x) const {
  224.         return p < x.p || (p == x.p && offset < x.offset);
  225.     }
  226.     };
  227.  
  228.     typedef reverse_iterator<const_iterator, value_type, const_reference, 
  229.                              difference_type> const_reverse_iterator;
  230.     typedef reverse_iterator<iterator, value_type, reference, difference_type>
  231.         reverse_iterator;
  232.  
  233. protected:
  234.     static Allocator<unsigned int> static_allocator;
  235.     iterator start;
  236.     iterator finish;
  237.     unsigned int* end_of_storage;
  238.     unsigned int* bit_alloc(size_type n) {
  239.     return static_allocator.allocate((n + __WORD_BIT - 1)/__WORD_BIT);
  240.     }
  241.     void initialize(size_type n) {
  242.     unsigned int* q = bit_alloc(n);
  243.     end_of_storage = q + (n + __WORD_BIT - 1)/__WORD_BIT;
  244.     start = iterator(q, 0);
  245.     finish = start + n;
  246.     }
  247.     void insert_aux(iterator position, bool x);
  248.     typedef bit_vector self;
  249. public:
  250.     iterator begin() { return start; }
  251.     const_iterator begin() const { return start; }
  252.     iterator end() { return finish; }
  253.     const_iterator end() const { return finish; }
  254.  
  255.     reverse_iterator rbegin() { return reverse_iterator(end()); }
  256.     const_reverse_iterator rbegin() const { 
  257.         return const_reverse_iterator(end()); 
  258.     }
  259.     reverse_iterator rend() { return reverse_iterator(begin()); }
  260.     const_reverse_iterator rend() const { 
  261.         return const_reverse_iterator(begin()); 
  262.     }
  263.  
  264.     size_type size() const { return size_type(end() - begin()); }
  265.     size_type max_size() const { return static_allocator.max_size(); }
  266.     size_type capacity() const {
  267.     return size_type(const_iterator(end_of_storage, 0) - begin());
  268.     }
  269.     bool empty() const { return begin() == end(); }
  270.     reference operator[](size_type n) { return *(begin() + n); }
  271.     const_reference operator[](size_type n) const { return *(begin() + n); }
  272.     bit_vector() : start(iterator()), finish(iterator()), end_of_storage(0) {}
  273.     bit_vector(size_type n, bool value = bool()) {
  274.     initialize(n);
  275.     fill(start.p, end_of_storage, value ? ~0 : 0);
  276.     }
  277.     bit_vector(const self& x) {
  278.     initialize(x.size());
  279.     copy(x.begin(), x.end(), start);
  280.     }
  281.     bit_vector(const_iterator first, const_iterator last) {
  282.     size_type n = 0;
  283.     distance(first, last, n);
  284.     initialize(n);
  285.     copy(first, last, start);
  286.     }
  287.     ~bit_vector() { static_allocator.deallocate(start.p); }
  288.     self& operator=(const self& x) {
  289.     if (&x == this) return *this;
  290.     if (x.size() > capacity()) {
  291.         static_allocator.deallocate(start.p); 
  292.         initialize(x.size());
  293.     }
  294.     copy(x.begin(), x.end(), begin());
  295.     finish = begin() + x.size();
  296.     return *this;
  297.     }
  298.     void reserve(size_type n) {
  299.     if (capacity() < n) {
  300.         unsigned int* q = bit_alloc(n);
  301.         finish = copy(begin(), end(), iterator(q, 0));
  302.         static_allocator.deallocate(start.p);
  303.         start = iterator(q, 0);
  304.         end_of_storage = q + (n + __WORD_BIT - 1)/__WORD_BIT;
  305.     }
  306.     }
  307.     reference front() { return *begin(); }
  308.     const_reference front() const { return *begin(); }
  309.     reference back() { return *(end() - 1); }
  310.     const_reference back() const { return *(end() - 1); }
  311.     void push_back(bool x) {
  312.     if (finish.p != end_of_storage)
  313.         *finish++ = x;
  314.     else
  315.         insert_aux(end(), x);
  316.     }
  317.     void swap(bit_vector& x) {
  318.     ::swap(start, x.start);
  319.     ::swap(finish, x.finish);
  320.     ::swap(end_of_storage, x.end_of_storage);
  321.     }
  322.     iterator insert(iterator position, bool x) {
  323.     size_type n = position - begin();
  324.     if (finish.p != end_of_storage && position == end())
  325.         *finish++ = x;
  326.     else
  327.         insert_aux(position, x);
  328.     return begin() + n;
  329.     }
  330.     void insert(iterator position, const_iterator first, 
  331.         const_iterator last);
  332.     void insert(iterator position, size_type n, bool x);
  333.     void pop_back() { --finish; }
  334.     void erase(iterator position) {
  335.     if (position + 1 != end())
  336.         copy(position + 1, end(), position);
  337.     --finish;
  338.     }
  339.     void erase(iterator first, iterator last) {
  340.     finish = copy(last, end(), first);
  341.     }
  342. };
  343.  
  344. Allocator<unsigned int> bit_vector::static_allocator;
  345.  
  346. inline bool operator==(const bit_vector& x, const bit_vector& y) {
  347.     return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
  348. }
  349.  
  350. inline bool operator<(const bit_vector& x, const bit_vector& y) {
  351.     return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  352. }
  353.  
  354. void swap(bit_vector::reference x, bit_vector::reference y) {
  355.     bool tmp = x;
  356.     x = y;
  357.     y = tmp;
  358. }
  359.  
  360. void bit_vector::insert_aux(iterator position, bool x) {
  361.     if (finish.p != end_of_storage) {
  362.     copy_backward(position, finish - 1, finish);
  363.     *position = x;
  364.     ++finish;
  365.     } else {
  366.     size_type len = size() ? 2 * size() : __WORD_BIT;
  367.     unsigned int* q = bit_alloc(len);
  368.     iterator i = copy(begin(), position, iterator(q, 0));
  369.     *i++ = x;
  370.     finish = copy(position, end(), i);
  371.     static_allocator.deallocate(start.p);
  372.     end_of_storage = q + (len + __WORD_BIT - 1)/__WORD_BIT;
  373.     start = iterator(q, 0);
  374.     }
  375. }
  376.  
  377. void bit_vector::insert(iterator position, size_type n, bool x) {
  378.     if (n == 0) return;
  379.     if (capacity() - size() >= n) {
  380.     copy_backward(position, end(), finish + n);
  381.     fill(position, position + n, x);
  382.     finish += n;
  383.     } else {
  384.     size_type len = size() + max(size(), n);
  385.     unsigned int* q = bit_alloc(len);
  386.     iterator i = copy(begin(), position, iterator(q, 0));
  387.     fill_n(i, n, x);
  388.     finish = copy(position, end(), i + n);
  389.     static_allocator.deallocate(start.p);
  390.     end_of_storage = q + (n + __WORD_BIT - 1)/__WORD_BIT;
  391.     start = iterator(q, 0);
  392.     }
  393. }
  394.  
  395. void bit_vector::insert(iterator position, const_iterator first, 
  396.             const_iterator last) {
  397.     if (first == last) return;
  398.     size_type n = 0;
  399.     distance(first, last, n);
  400.     if (capacity() - size() >= n) {
  401.     copy_backward(position, end(), finish + n);
  402.     copy(first, last, position);
  403.     finish += n;
  404.     } else {
  405.     size_type len = size() + max(size(), n);
  406.     unsigned int* q = bit_alloc(len);
  407.     iterator i = copy(begin(), position, iterator(q, 0));
  408.     i = copy(first, last, i);
  409.     finish = copy(position, end(), i);
  410.     static_allocator.deallocate(start.p);
  411.     end_of_storage = q + (len + __WORD_BIT - 1)/__WORD_BIT;
  412.     start = iterator(q, 0);
  413.     }
  414. }
  415.  
  416. #undef Allocator
  417. #undef __WORD_BIT
  418.  
  419. #endif
  420.  
  421.  
  422.