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