Back to index

salome-gui  6.5.0
QtxMap.h
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // File   : QtxMap.h
00021 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00022 //
00023 #ifndef QTXMAP_H
00024 #define QTXMAP_H
00025 
00026 template <class Key, class Value> class IMap;
00027 template <class Key, class Value> class IMapIterator;
00028 template <class Key, class Value> class IMapConstIterator;
00029 
00033 template <class Key, class Value> class IMap
00034 {
00035 public:
00036   typedef IMapIterator<Key,Value>      Iterator;
00037   typedef IMapConstIterator<Key,Value> ConstIterator;
00038 
00039 public:
00040   IMap() {}
00041   IMap( const IMap& m ) : myKeys( m.myKeys ), myData( m.myData ) {}
00042   IMap& operator=( const IMap& m ) { myKeys = m.myKeys; myData = m.myData; return *this; }
00043   
00044   int  count() const   { return myData.count(); }
00045   int  size() const    { return myData.count(); }
00046   bool empty() const   { return myData.empty(); }
00047   bool isEmpty() const { return myData.empty(); }
00048   
00049   void clear() { myKeys.clear(); myData.clear(); }
00050   
00051   QList<Key>   keys()   const { return myKeys; }
00052   QList<Value> values() const { QList<Value> l; for ( int i = 0; i < count(); i++ ) l.append( value( i ) ); return l; }
00053   bool         contains ( const Key& key ) const { return myData.contains( key ); }
00054   
00055   Iterator      begin()       { return Iterator( this );               }
00056   Iterator      end()         { return Iterator( this, count() );      }
00057   ConstIterator begin() const { return ConstIterator( this );          }
00058   ConstIterator end() const   { return ConstIterator( this, count() ); }
00059   
00060   Iterator insert( const Key& key, const Value& value, bool overwrite = true )
00061   { 
00062     if ( myData.find( key ) == myData.end() || overwrite )
00063     {
00064       if ( myData.find( key ) != myData.end() && overwrite )
00065         myKeys.removeAt( myKeys.indexOf( key ) );
00066       myKeys.append( key );
00067       myData[key] = value;
00068     }
00069     return Iterator( this, index( key ) );
00070   }
00071 
00072   Iterator replace( const Key& key, const Value& value )
00073   { 
00074     if ( myData.find( key ) == myData.end() )
00075       myKeys.append( key );
00076     myData[ key ] = value;
00077     return Iterator( this, index( key ) );
00078   }
00079 
00080   int           index( const Key& key ) const { return myKeys.indexOf( key );      }
00081   Iterator      at( const int index )         { return Iterator( this, index );      }
00082   ConstIterator at( const int index ) const   { return ConstIterator( this, index ); }
00083 
00084   Key& key( const int index )
00085   {
00086     if ( index < 0 || index >= (int)myKeys.count() ) 
00087       return dummyKey;
00088     return myKeys[index];
00089   }
00090 
00091   Value value( const int index )
00092   {
00093     if ( index < 0 || index >= (int)myKeys.count() ) 
00094       return dummyValue;
00095     return myData[ myKeys[index] ];
00096   }
00097 
00098   Value operator[]( const Key& key )
00099   {
00100     if ( myData.find( key ) == myData.end() )
00101       insert( key, Value() );
00102     return myData[ key ];
00103   }
00104 
00105   const Value operator[]( const Key& key ) const
00106   {
00107     if ( myData.find( key ) == myData.end() )
00108       return dummyValue;
00109     return myData[key];
00110   }
00111 
00112   void erase( Iterator it )     { remove( it );    }
00113   void erase( const Key& key )  { remove( key );   }
00114   void erase( const int index ) { remove( index ); }
00115   void remove( Iterator it )    { if ( it.myMap != this ) return; remove( it.myIndex ); }
00116   void remove( const Key& key ) { remove( index( key ) ); }
00117   void remove( const int index )
00118   {
00119     if ( index >= 0 && index < (int)myKeys.count() )
00120     {
00121       myData.remove( myKeys[index] );
00122       myKeys.removeAt( index );
00123     }
00124   }
00125 
00126 private:
00127   QList<Key>      myKeys;
00128   QMap<Key,Value> myData;
00129   Key             dummyKey;
00130   Value           dummyValue;
00131 
00132   friend class IMapIterator<Key,Value>;
00133   friend class IMapConstIterator<Key,Value>;
00134 };
00135 
00139 template <class Key, class Value> class IMapIterator
00140 {
00141 public:
00142   IMapIterator()                           : myMap( 0 ), myIndex( 0 )                                   { init(); }
00143   IMapIterator( const IMap<Key,Value>* m ) : myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( 0 ) { init(); }
00144   IMapIterator( const IMapIterator& i )    : myMap( i.myMap ), myIndex( i.myIndex )                     { init(); }
00145 
00146   bool operator==( const IMapIterator& i ) { return !operator!=( i );                                   }
00147   bool operator!=( const IMapIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
00148   
00149   operator bool() const { return myIndex >= 0; }
00150 
00151   const Key&   key() const  { return myMap->key( myIndex );   }
00152   Value&       value()       { return myMap->value( myIndex ); }
00153   const Value& value() const { return myMap->value( myIndex ); }
00154 
00155   Value& operator*() { return value(); }
00156 
00157   IMapIterator& operator++()      { myIndex++; init(); return *this;                     }
00158   IMapIterator  operator++( int ) { IMapIterator i = *this; myIndex++; init(); return i; }
00159   IMapIterator& operator--()      { myIndex--; init(); return *this;                     }
00160   IMapIterator  operator--( int ) { IMapIterator i = *this; myIndex--; init(); return i; }
00161 
00162 private:
00163   IMapIterator( const IMap<Key,Value>* m, const int index ) : myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( index ) { init(); }
00164   void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; }
00165 
00166 private:
00167   IMap<Key,Value>* myMap;
00168   int              myIndex;
00169 
00170   friend class IMap<Key, Value>;
00171   friend class IMapConstIterator<Key, Value>;
00172 };
00173 
00177 template <class Key, class Value> class IMapConstIterator
00178 {
00179 public:
00180   IMapConstIterator()                                    : myMap( 0 ), myIndex( 0 )                                    { init(); }
00181   IMapConstIterator( const IMap<Key,Value>* m )          : myMap( const_cast< IMap<Key,Value>* >( m )  ), myIndex( 0 ) { init(); }
00182   IMapConstIterator( const IMapConstIterator& i )        : myMap( i.myMap ), myIndex( i.myIndex )                      { init(); }
00183   IMapConstIterator( const IMapIterator<Key, Value>& i ) : myMap( i.myMap ), myIndex( i.myIndex )                      { init(); }
00184   
00185   bool operator==( const IMapConstIterator& i ) { return !operator!=( i );                                   }
00186   bool operator!=( const IMapConstIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
00187   
00188   operator bool() const { return myIndex >= 0; }
00189   
00190   const Key&   key() const  { return myMap->key( myIndex );   }
00191   const Value value() const { return myMap->value( myIndex ); }
00192   
00193   const Value operator*() const { return value(); }
00194 
00195   IMapConstIterator& operator++()      { myIndex++; init(); return *this;                          }
00196   IMapConstIterator  operator++( int ) { IMapConstIterator i = *this; myIndex++; init(); return i; }
00197   IMapConstIterator& operator--()      { myIndex--; init(); return *this;                          }
00198   IMapConstIterator  operator--( int ) { IMapConstIterator i = *this; myIndex--; init(); return i; }
00199   
00200 private:
00201   IMapConstIterator( const IMap<Key,Value>* m, const int index ): myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( index ) { init(); }
00202   void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; }
00203   
00204 private:
00205   IMap<Key,Value>* myMap;
00206   int              myIndex;
00207   
00208   friend class IMap<Key,Value>;
00209 };
00210 
00211 #endif // QTXMAP_H