1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
28 template <class Key, class Value> class IMap;
29 template <class Key, class Value> class IMapIterator;
30 template <class Key, class Value> class IMapConstIterator;
33 \brief Indexed map template class.
35 template <class Key, class Value> class IMap
38 typedef IMapIterator<Key,Value> Iterator;
39 typedef IMapConstIterator<Key,Value> ConstIterator;
43 IMap( const IMap& m ) : myKeys( m.myKeys ), myData( m.myData ) {}
44 IMap& operator=( const IMap& m ) { myKeys = m.myKeys; myData = m.myData; return *this; }
46 int count() const { return myData.count(); }
47 int size() const { return myData.count(); }
48 bool empty() const { return myData.empty(); }
49 bool isEmpty() const { return myData.empty(); }
51 void clear() { myKeys.clear(); myData.clear(); }
53 QList<Key> keys() const { return myKeys; }
54 QList<Value> values() const { QList<Value> l; for ( int i = 0; i < count(); i++ ) l.append( value( i ) ); return l; }
55 bool contains ( const Key& key ) const { return myData.contains( key ); }
57 Iterator begin() { return Iterator( this ); }
58 Iterator end() { return Iterator( this, count() ); }
59 ConstIterator begin() const { return ConstIterator( this ); }
60 ConstIterator end() const { return ConstIterator( this, count() ); }
62 Iterator insert( const Key& key, const Value& value, bool overwrite = true )
64 if ( myData.find( key ) == myData.end() || overwrite )
66 if ( myData.find( key ) != myData.end() && overwrite )
67 myKeys.removeAt( myKeys.indexOf( key ) );
71 return Iterator( this, index( key ) );
74 Iterator replace( const Key& key, const Value& value )
76 if ( myData.find( key ) == myData.end() )
78 myData[ key ] = value;
79 return Iterator( this, index( key ) );
82 int index( const Key& key ) const { return myKeys.indexOf( key ); }
83 Iterator at( const int index ) { return Iterator( this, index ); }
84 ConstIterator at( const int index ) const { return ConstIterator( this, index ); }
86 Key& key( const int index )
88 if ( index < 0 || index >= (int)myKeys.count() )
93 Value value( const int index )
95 if ( index < 0 || index >= (int)myKeys.count() )
97 return myData[ myKeys[index] ];
100 Value operator[]( const Key& key )
102 if ( myData.find( key ) == myData.end() )
103 insert( key, Value() );
104 return myData[ key ];
107 const Value operator[]( const Key& key ) const
109 if ( myData.find( key ) == myData.end() )
114 void erase( Iterator it ) { remove( it ); }
115 void erase( const Key& key ) { remove( key ); }
116 void erase( const int index ) { remove( index ); }
117 void remove( Iterator it ) { if ( it.myMap != this ) return; remove( it.myIndex ); }
118 void remove( const Key& key ) { remove( index( key ) ); }
119 void remove( const int index )
121 if ( index >= 0 && index < (int)myKeys.count() )
123 myData.remove( myKeys[index] );
124 myKeys.removeAt( index );
130 QMap<Key,Value> myData;
134 friend class IMapIterator<Key,Value>;
135 friend class IMapConstIterator<Key,Value>;
139 \brief Indexed map iterator template class.
141 template <class Key, class Value> class IMapIterator
144 IMapIterator() : myMap( 0 ), myIndex( 0 ) { init(); }
145 IMapIterator( const IMap<Key,Value>* m ) : myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( 0 ) { init(); }
146 IMapIterator( const IMapIterator& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); }
148 bool operator==( const IMapIterator& i ) { return !operator!=( i ); }
149 bool operator!=( const IMapIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
151 operator bool() const { return myIndex >= 0; }
153 const Key& key() const { return myMap->key( myIndex ); }
154 Value& value() { return myMap->value( myIndex ); }
155 const Value& value() const { return myMap->value( myIndex ); }
157 Value& operator*() { return value(); }
159 IMapIterator& operator++() { myIndex++; init(); return *this; }
160 IMapIterator operator++( int ) { IMapIterator i = *this; myIndex++; init(); return i; }
161 IMapIterator& operator--() { myIndex--; init(); return *this; }
162 IMapIterator operator--( int ) { IMapIterator i = *this; myIndex--; init(); return i; }
165 IMapIterator( const IMap<Key,Value>* m, const int index ) : myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( index ) { init(); }
166 void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; }
169 IMap<Key,Value>* myMap;
172 friend class IMap<Key, Value>;
173 friend class IMapConstIterator<Key, Value>;
177 \brief Indexed map const iterator template class.
179 template <class Key, class Value> class IMapConstIterator
182 IMapConstIterator() : myMap( 0 ), myIndex( 0 ) { init(); }
183 IMapConstIterator( const IMap<Key,Value>* m ) : myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( 0 ) { init(); }
184 IMapConstIterator( const IMapConstIterator& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); }
185 IMapConstIterator( const IMapIterator<Key, Value>& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); }
187 bool operator==( const IMapConstIterator& i ) { return !operator!=( i ); }
188 bool operator!=( const IMapConstIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
190 operator bool() const { return myIndex >= 0; }
192 const Key& key() const { return myMap->key( myIndex ); }
193 const Value value() const { return myMap->value( myIndex ); }
195 const Value operator*() const { return value(); }
197 IMapConstIterator& operator++() { myIndex++; init(); return *this; }
198 IMapConstIterator operator++( int ) { IMapConstIterator i = *this; myIndex++; init(); return i; }
199 IMapConstIterator& operator--() { myIndex--; init(); return *this; }
200 IMapConstIterator operator--( int ) { IMapConstIterator i = *this; myIndex--; init(); return i; }
203 IMapConstIterator( const IMap<Key,Value>* m, const int index ): myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( index ) { init(); }
204 void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; }
207 IMap<Key,Value>* myMap;
210 friend class IMap<Key,Value>;