/*
    C++ Mesh Generation Library
    Copyright (c) 2000echet <eric at bechet dot ca>

    This file is part of the C++ Mesh Generation Library.

    See the NOTICE & LICENSE files for conditions.
*/
//---------------------------------------------------------------------------
#ifndef VECTOR_UNIQUE_CONTAINER_H
#define VECTOR_UNIQUE_CONTAINER_H
//---------------------------------------------------------------------------


#include <vector>


using namespace std;


/// container for anything that should be kept unique (small size)
template<class T> class Vector_Unique_Container : public vector<T>
{
public:
  typedef typename vector<T>::iterator iterator;
  typedef typename vector<T>::size_type size_type;

  pair<iterator,bool> insert ( const T& I )
  {
    pair<iterator,bool> p;
    iterator it,itend;
    it=this->begin();
    itend=this->end();

    while ( it!=itend )
    {
      if ( ( *it ) ==I )
      {
        break;
      }

      ++it;
    }

    if ( it==itend )
    {
      this->push_back ( I );
      p.first=this->end();
      --p.first;
      p.second=true;
    }
    else
    {
      p.first=it;
      p.second=false;
    }

    return p;
  }

  size_type erase ( const T& I )
  {
    iterator it,itend,it2;
    it=this->begin();
    itend=this->end();

    while ( it!=itend )
    {
      if ( ( *it ) ==I )
      {
        break;
      }

      ++it;
    }

    if ( it!=itend )
    {
      it2=it;
      ++it2;

      while ( it2!=itend )
      {
        *it=*it2;
        it=it2;
        ++it2;
      }

      this->pop_back();
      return 1;
    }

    return 0;
  }
};


#endif // VECTOR_UNIQUE_CONTAINER_H

 
