home *** CD-ROM | disk | FTP | other *** search
/ Beginning C++ Through Gam…rogramming (2nd Edition) / BCGP2E.ISO / bloodshed / devcpp-4.9.9.2_setup.exe / hash_set.h < prev    next >
C/C++ Source or Header  |  2005-01-29  |  8KB  |  246 lines

  1. // Debugging hash_set implementation -*- C++ -*-
  2.  
  3. // Copyright (C) 2003
  4. // Free Software Foundation, Inc.
  5. //
  6. // This file is part of the GNU ISO C++ Library.  This library is free
  7. // software; you can redistribute it and/or modify it under the
  8. // terms of the GNU General Public License as published by the
  9. // Free Software Foundation; either version 2, or (at your option)
  10. // any later version.
  11.  
  12. // This library is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. // GNU General Public License for more details.
  16.  
  17. // You should have received a copy of the GNU General Public License along
  18. // with this library; see the file COPYING.  If not, write to the Free
  19. // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  20. // USA.
  21.  
  22. // As a special exception, you may use this file as part of a free software
  23. // library without restriction.  Specifically, if other files instantiate
  24. // templates or use macros or inline functions from this file, or you compile
  25. // this file and link it with other files to produce an executable, this
  26. // file does not by itself cause the resulting executable to be covered by
  27. // the GNU General Public License.  This exception does not however
  28. // invalidate any other reasons why the executable file might be covered by
  29. // the GNU General Public License.
  30.  
  31. #ifndef _GLIBCXX_DEBUG_HASH_SET_H
  32. #define _GLIBCXX_DEBUG_HASH_SET_H 1
  33.  
  34. #include <debug/safe_sequence.h>
  35. #include <debug/safe_iterator.h>
  36.  
  37. namespace __gnu_debug_def
  38. {
  39.   template<typename _Value,
  40.        typename _HashFcn  = __gnu_cxx::hash<_Value>,
  41.        typename _EqualKey = std::equal_to<_Value>,
  42.        typename _Alloc =  std::allocator<_Value> >
  43.     class hash_set
  44.     : public __gnu_cxx::hash_set<_Value, _HashFcn, _EqualKey, _Alloc>,
  45.       public __gnu_debug::_Safe_sequence<hash_set<_Value, _HashFcn, _EqualKey,
  46.                           _Alloc> >
  47.     {
  48.       typedef __gnu_cxx::hash_set<_Value, _HashFcn, _EqualKey, _Alloc> _Base;
  49.       typedef __gnu_debug::_Safe_sequence<hash_set> _Safe_base;
  50.  
  51.     public:
  52.       typedef typename _Base::key_type        key_type;
  53.       typedef typename _Base::value_type      value_type;
  54.       typedef typename _Base::hasher          hasher;
  55.       typedef typename _Base::key_equal       key_equal;
  56.       typedef typename _Base::size_type       size_type;
  57.       typedef typename _Base::difference_type difference_type;
  58.       typedef typename _Base::pointer         pointer;
  59.       typedef typename _Base::const_pointer   const_pointer;
  60.       typedef typename _Base::reference       reference;
  61.       typedef typename _Base::const_reference const_reference;
  62.  
  63.       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, hash_set>
  64.                                               iterator;
  65.       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
  66.                       hash_set>
  67.                                               const_iterator;
  68.  
  69.       typedef typename _Base::allocator_type allocator_type;
  70.  
  71.       using _Base::hash_funct;
  72.       using _Base::key_eq;
  73.       using _Base::get_allocator;
  74.  
  75.       hash_set() { }
  76.  
  77.       explicit hash_set(size_type __n) : _Base(__n) { }
  78.  
  79.       hash_set(size_type __n, const hasher& __hf) : _Base(__n, __hf) { }
  80.  
  81.       hash_set(size_type __n, const hasher& __hf, const key_equal& __eql,
  82.            const allocator_type& __a = allocator_type())
  83.       : _Base(__n, __hf, __eql, __a) { }
  84.  
  85.       template<typename _InputIterator>
  86.         hash_set(_InputIterator __f, _InputIterator __l)
  87.         : _Base(__gnu_debug::__check_valid_range(__f, __l), __l) { }
  88.  
  89.       template<typename _InputIterator>
  90.         hash_set(_InputIterator __f, _InputIterator __l, size_type __n)
  91.     : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n) { }
  92.  
  93.       template<typename _InputIterator>
  94.         hash_set(_InputIterator __f, _InputIterator __l, size_type __n,
  95.          const hasher& __hf)
  96.     : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf) { }
  97.  
  98.       template<typename _InputIterator>
  99.         hash_set(_InputIterator __f, _InputIterator __l, size_type __n,
  100.          const hasher& __hf, const key_equal& __eql,
  101.          const allocator_type& __a = allocator_type())
  102.     : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf,
  103.         __eql, __a) { }
  104.  
  105.       hash_set(const _Base& __x) : _Base(__x), _Safe_base() { }
  106.  
  107.       using _Base::size;
  108.       using _Base::max_size;
  109.       using _Base::empty;
  110.  
  111.       void
  112.       swap(hash_set& __x)
  113.       {
  114.     _Base::swap(__x);
  115.     this->_M_swap(__x);
  116.       }
  117.  
  118.       iterator
  119.       begin() const { return iterator(_Base::begin(), this); }
  120.  
  121.       iterator
  122.       end() const   { return iterator(_Base::end(),   this); }
  123.  
  124.       std::pair<iterator, bool>
  125.       insert(const value_type& __obj)
  126.       {
  127.     std::pair<typename _Base::iterator, bool> __res =
  128.         _Base::insert(__obj);
  129.     return std::make_pair(iterator(__res.first, this), __res.second);
  130.       }
  131.  
  132.       template <typename _InputIterator>
  133.         void
  134.         insert(_InputIterator __first, _InputIterator __last)
  135.         {
  136.       __glibcxx_check_valid_range(__first, __last);
  137.       _Base::insert(__first.base(), __last.base());
  138.     }
  139.  
  140.  
  141.       std::pair<iterator, bool>
  142.       insert_noresize(const value_type& __obj)
  143.       {
  144.     std::pair<typename _Base::iterator, bool> __res =
  145.         _Base::insert_noresize(__obj);
  146.     return std::make_pair(iterator(__res.first, this), __res.second);
  147.       }
  148.  
  149.       iterator
  150.       find(const key_type& __key) const
  151.       { return iterator(_Base::find(__key), this); }
  152.  
  153.       using _Base::count;
  154.  
  155.       std::pair<iterator, iterator>
  156.       equal_range(const key_type& __key) const
  157.       {
  158.     typedef typename _Base::iterator _Base_iterator;
  159.     std::pair<_Base_iterator, _Base_iterator> __res =
  160.       _Base::equal_range(__key);
  161.     return std::make_pair(iterator(__res.first, this),
  162.                   iterator(__res.second, this));
  163.       }
  164.  
  165.       size_type
  166.       erase(const key_type& __key)
  167.       {
  168.     iterator __victim(_Base::find(__key), this);
  169.     if (__victim != end())
  170.       return this->erase(__victim), 1;
  171.     else
  172.       return 0;
  173.       }
  174.  
  175.       void
  176.       erase(iterator __it)
  177.       {
  178.     __glibcxx_check_erase(__it);
  179.     __it._M_invalidate();
  180.     _Base::erase(__it.base());
  181.       }
  182.  
  183.       void
  184.       erase(iterator __first, iterator __last)
  185.       {
  186.     __glibcxx_check_erase_range(__first, __last);
  187.     for (iterator __tmp = __first; __tmp != __last;)
  188.     {
  189.       iterator __victim = __tmp++;
  190.       __victim._M_invalidate();
  191.     }
  192.     _Base::erase(__first.base(), __last.base());
  193.       }
  194.  
  195.       void
  196.       clear()
  197.       {
  198.     _Base::clear();
  199.     this->_M_invalidate_all();
  200.       }
  201.  
  202.       using _Base::resize;
  203.       using _Base::bucket_count;
  204.       using _Base::max_bucket_count;
  205.       using _Base::elems_in_bucket;
  206.  
  207.       _Base&
  208.       _M_base()       { return *this; }
  209.  
  210.       const _Base&
  211.       _M_base() const { return *this; }
  212.  
  213.     private:
  214.       void
  215.       _M_invalidate_all()
  216.       {
  217.     typedef typename _Base::const_iterator _Base_const_iterator;
  218.     typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
  219.     this->_M_invalidate_if(_Not_equal(_M_base().end()));
  220.       }
  221.     };
  222.  
  223.   template<typename _Value, typename _HashFcn, typename _EqualKey,
  224.        typename _Alloc>
  225.     inline bool
  226.     operator==(const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __x,
  227.            const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __y)
  228.     { return __x._M_base() == __y._M_base(); }
  229.  
  230.   template<typename _Value, typename _HashFcn, typename _EqualKey,
  231.        typename _Alloc>
  232.     inline bool
  233.     operator!=(const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __x,
  234.            const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __y)
  235.     { return __x._M_base() != __y._M_base(); }
  236.  
  237.   template<typename _Value, typename _HashFcn, typename _EqualKey,
  238.        typename _Alloc>
  239.     inline void
  240.     swap(hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __x,
  241.      hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __y)
  242.     { __x.swap(__y); }
  243. } // namespace __gnu_debug_def
  244.  
  245. #endif
  246.