Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / Qtx / QtxMap.h
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : QtxMap.h
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24 //
25 #ifndef QTXMAP_H
26 #define QTXMAP_H
27
28 template <class Key, class Value> class IMap;
29 template <class Key, class Value> class IMapIterator;
30 template <class Key, class Value> class IMapConstIterator;
31
32 /*!
33   \brief Indexed map template class.
34 */
35 template <class Key, class Value> class IMap
36 {
37 public:
38   typedef IMapIterator<Key,Value>      Iterator;
39   typedef IMapConstIterator<Key,Value> ConstIterator;
40
41 public:
42   IMap() {}
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; }
45   
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(); }
50   
51   void clear() { myKeys.clear(); myData.clear(); }
52   
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 ); }
56   
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() ); }
61   
62   Iterator insert( const Key& key, const Value& value, bool overwrite = true )
63   { 
64     if ( myData.find( key ) == myData.end() || overwrite )
65     {
66       if ( myData.find( key ) != myData.end() && overwrite )
67         myKeys.removeAt( myKeys.indexOf( key ) );
68       myKeys.append( key );
69       myData[key] = value;
70     }
71     return Iterator( this, index( key ) );
72   }
73
74   Iterator replace( const Key& key, const Value& value )
75   { 
76     if ( myData.find( key ) == myData.end() )
77       myKeys.append( key );
78     myData[ key ] = value;
79     return Iterator( this, index( key ) );
80   }
81
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 ); }
85
86   Key& key( const int index )
87   {
88     if ( index < 0 || index >= (int)myKeys.count() ) 
89       return dummyKey;
90     return myKeys[index];
91   }
92
93   Value value( const int index )
94   {
95     if ( index < 0 || index >= (int)myKeys.count() ) 
96       return dummyValue;
97     return myData[ myKeys[index] ];
98   }
99
100   Value operator[]( const Key& key )
101   {
102     if ( myData.find( key ) == myData.end() )
103       insert( key, Value() );
104     return myData[ key ];
105   }
106
107   const Value operator[]( const Key& key ) const
108   {
109     if ( myData.find( key ) == myData.end() )
110       return dummyValue;
111     return myData[key];
112   }
113
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 )
120   {
121     if ( index >= 0 && index < (int)myKeys.count() )
122     {
123       myData.remove( myKeys[index] );
124       myKeys.removeAt( index );
125     }
126   }
127
128 private:
129   QList<Key>      myKeys;
130   QMap<Key,Value> myData;
131   Key             dummyKey;
132   Value           dummyValue;
133
134   friend class IMapIterator<Key,Value>;
135   friend class IMapConstIterator<Key,Value>;
136 };
137
138 /*!
139   \brief Indexed map iterator template class.
140 */
141 template <class Key, class Value> class IMapIterator
142 {
143 public:
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(); }
147
148   bool operator==( const IMapIterator& i ) { return !operator!=( i );                                   }
149   bool operator!=( const IMapIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
150   
151   operator bool() const { return myIndex >= 0; }
152
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 ); }
156
157   Value& operator*() { return value(); }
158
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; }
163
164 private:
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; }
167
168 private:
169   IMap<Key,Value>* myMap;
170   int              myIndex;
171
172   friend class IMap<Key, Value>;
173   friend class IMapConstIterator<Key, Value>;
174 };
175
176 /*!
177   \brief Indexed map const iterator template class.
178 */
179 template <class Key, class Value> class IMapConstIterator
180 {
181 public:
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(); }
186   
187   bool operator==( const IMapConstIterator& i ) { return !operator!=( i );                                   }
188   bool operator!=( const IMapConstIterator& i ) { return !myMap || myMap != i.myMap || myIndex != i.myIndex; }
189   
190   operator bool() const { return myIndex >= 0; }
191   
192   const Key&   key() const  { return myMap->key( myIndex );   }
193   const Value value() const { return myMap->value( myIndex ); }
194   
195   const Value operator*() const { return value(); }
196
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; }
201   
202 private:
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; }
205   
206 private:
207   IMap<Key,Value>* myMap;
208   int              myIndex;
209   
210   friend class IMap<Key,Value>;
211 };
212
213 #endif // QTXMAP_H