/* Copyright (c) 1997-2010
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
   http://www.polymake.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------
   $Project: polymake $$Id: hash_set 9436 2010-02-03 01:08:34Z gawrilow $
*/

#ifndef _POLYMAKE_HASH_SET
#define _POLYMAKE_HASH_SET

#ifdef __GNUC__
#if __GNUC__>4 || __GNUC__==4 && __GNUC_MINOR__>=2
#  include <tr1/unordered_set>
#  define pm_hash_set_impl std::tr1::unordered_set
#else  // gcc<=4.1
#  include <ext/hash_set>
#  define pm_hash_set_impl __gnu_cxx::hash_set
#endif

#include <hash_iterators.h>

namespace pm {

template <typename Key, typename Params=void>
class hash_set :
      public pm_hash_set_impl<Key, hash_func<Key>, typename hash_table_cmp_adapter<Key,Params>::type> {
   typedef pm_hash_set_impl<Key, hash_func<Key>, typename hash_table_cmp_adapter<Key,Params>::type> _super;
public:
   hash_set() {}
   explicit hash_set(size_t start_cap) : _super(start_cap) {}

   template <typename Iterator>
   hash_set(Iterator first, Iterator last) : _super(first,last) {}

   // let's make it at least partially compatible with Set

   hash_set& operator+= (const Key& k)
   {
      this->insert(k);
      return *this;
   }

   hash_set& operator-= (const Key& k)
   {
      this->erase(k);
      return *this;
   }

   hash_set& operator^= (const Key& k)
   {
      std::pair<typename _super::iterator, bool> inserted=this->insert(k);
      if (!inserted.second) this->erase(inserted.first);
      return *this;
   }

   bool exists(const Key& k) const
   {
      return this->find(k) != this->end();
   }

   bool operator== (const hash_set& other) const
   {
      if (this->size() != other.size()) return false;
      typename _super::const_iterator not_found=this->end();
      for (typename _super::const_iterator elem=other.begin(), other_end=other.end(); elem != other_end; ++elem)
	 if (this->find(*elem) == not_found)
	    return false;
      return true;
   }
};

#undef pm_hash_set_impl

template <typename Key, typename KeyComparator>
struct choose_generic_object_traits< hash_set<Key,KeyComparator>, false, false >
   : spec_object_traits<is_container> {
   typedef void generic_type;
   typedef hash_set<Key,KeyComparator> persistent_type;
   typedef is_unordered_set generic_tag;
   static const IO_separator_kind IO_separator=IO_sep_inherit;
};

}
namespace polymake {
   using pm::hash_set;
}

#endif // GNUC
#endif // _POLYMAKE_HASH_SET

// Local Variables:
// mode:C++
// End:
