1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
26 template <class Key, class Value> class IMap;
27 template <class Key, class Value> class IMapIterator;
28 template <class Key, class Value> class IMapConstIterator;
31 \brief Indexed map template class.
33 template <class Key, class Value> class IMap
36 typedef IMapIterator<Key,Value> Iterator;
37 typedef IMapConstIterator<Key,Value> ConstIterator;
41 IMap( const IMap& m ) : myKeys( m.myKeys ), myData( m.myData ) {}
42 IMap& operator=( const IMap& m ) { myKeys = m.myKeys; myData = m.myData; return *this; }
44 int count() const { return myData.count(); }
45 int size() const { return myData.count(); }
46 bool empty() const { return myData.empty(); }
47 bool isEmpty() const { return myData.empty(); }
49 void clear() { myKeys.clear(); myData.clear(); }
51 QList<Key> keys() const { return myKeys; }
52 QList<Value> values() const { QList<Value> l; for ( int i = 0; i < count(); i++ ) l.append( value( i ) ); return l; }
53 bool contains ( const Key& key ) const { return myData.contains( key ); }
55 Iterator begin() { return Iterator( this ); }
56 Iterator end() { return Iterator( this, count() ); }
57 ConstIterator begin() const { return ConstIterator( this ); }
58 ConstIterator end() const { return ConstIterator( this, count() ); }
60 Iterator insert( const Key& key, const Value& value, bool overwrite = true )
62 if ( myData.find( key ) == myData.end() || overwrite )
64 if ( myData.find( key ) != myData.end() && overwrite )
65 myKeys.removeAt( myKeys.indexOf( key ) );
69 return Iterator( this, index( key ) );
72 Iterator replace( const Key& key, const Value& value )
74 if ( myData.find( key ) == myData.end() )
76 myData[ key ] = value;
77 return Iterator( this, index( key ) );
80 int index( const Key& key ) const { return myKeys.indexOf( key ); }
81 Iterator at( const int index ) { return Iterator( this, index ); }
82 ConstIterator at( const int index ) const { return ConstIterator( this, index ); }
84 Key& key( const int index )
86 if ( index < 0 || index >= (int)myKeys.count() )
91 Value value( const int index )
93 if ( index < 0 || index >= (int)myKeys.count() )
95 return myData[ myKeys[index] ];
98 Value operator[]( const Key& key )
100 if ( myData.find( key ) == myData.end() )
101 insert( key, Value() );
102 return myData[ key ];
105 const Value operator[]( const Key& key ) const
107 if ( myData.find( key ) == myData.end() )
112 void erase( Iterator it ) { remove( it ); }
113 void erase( const Key& key ) { remove( key ); }
114 void erase( const int index ) { remove( index ); }
115 void remove( Iterator it ) { if ( it.myMap != this ) return; remove( it.myIndex ); }
116 void remove( const Key& key ) { remove( index( key ) ); }
117 void remove( const int index )
119 if ( index >= 0 && index < (int)myKeys.count() )
121 myData.remove( myKeys[index] );
122 myKeys.removeAt( index );
128 QMap<Key,Value> myData;
132 friend class IMapIterator<Key,Value>;
133 friend class IMapConstIterator<Key,Value>;
137 \brief Indexed map iterator template class.
139 template <class Key, class Value> class IMapIterator
142 IMapIterator() : myMap( 0 ), myIndex( 0 ) { init(); }
143 IMapIterator( const IMap<Key,Value>* m ) : myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( 0 ) { init(); }
144 IMapIterator( const IMapIterator& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); }
146 bool operator==( const IMapIterator& i ) { return !operator!=( i ); }
147 bool operator!=( const IMapIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
149 operator bool() const { return myIndex >= 0; }
151 const Key& key() const { return myMap->key( myIndex ); }
152 Value& value() { return myMap->value( myIndex ); }
153 const Value& value() const { return myMap->value( myIndex ); }
155 Value& operator*() { return value(); }
157 IMapIterator& operator++() { myIndex++; init(); return *this; }
158 IMapIterator operator++( int ) { IMapIterator i = *this; myIndex++; init(); return i; }
159 IMapIterator& operator--() { myIndex--; init(); return *this; }
160 IMapIterator operator--( int ) { IMapIterator i = *this; myIndex--; init(); return i; }
163 IMapIterator( const IMap<Key,Value>* m, const int index ) : myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( index ) { init(); }
164 void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; }
167 IMap<Key,Value>* myMap;
170 friend class IMap<Key, Value>;
171 friend class IMapConstIterator<Key, Value>;
175 \brief Indexed map const iterator template class.
177 template <class Key, class Value> class IMapConstIterator
180 IMapConstIterator() : myMap( 0 ), myIndex( 0 ) { init(); }
181 IMapConstIterator( const IMap<Key,Value>* m ) : myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( 0 ) { init(); }
182 IMapConstIterator( const IMapConstIterator& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); }
183 IMapConstIterator( const IMapIterator<Key, Value>& i ) : myMap( i.myMap ), myIndex( i.myIndex ) { init(); }
185 bool operator==( const IMapConstIterator& i ) { return !operator!=( i ); }
186 bool operator!=( const IMapConstIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
188 operator bool() const { return myIndex >= 0; }
190 const Key& key() const { return myMap->key( myIndex ); }
191 const Value value() const { return myMap->value( myIndex ); }
193 const Value operator*() const { return value(); }
195 IMapConstIterator& operator++() { myIndex++; init(); return *this; }
196 IMapConstIterator operator++( int ) { IMapConstIterator i = *this; myIndex++; init(); return i; }
197 IMapConstIterator& operator--() { myIndex--; init(); return *this; }
198 IMapConstIterator operator--( int ) { IMapConstIterator i = *this; myIndex--; init(); return i; }
201 IMapConstIterator( const IMap<Key,Value>* m, const int index ): myMap( const_cast< IMap<Key,Value>* >( m ) ), myIndex( index ) { init(); }
202 void init() { if ( !myMap || myIndex >= myMap->count() ) myIndex = -1; }
205 IMap<Key,Value>* myMap;
208 friend class IMap<Key,Value>;