Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

mapc.h

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 1999  Carsten Winkelholz
00003 
00004 Address:  FGAN Forschungsgesellschaft fr Angewandte Naturwissenschaften e. V.
00005       Neuenahrer Str. 20
00006       D - 53343 Wachtberg
00007       
00008 Email:    winkelholz@fgan.de
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 */
00024 
00025 
00026 #ifndef MAPC_H
00027 #define MAPC_H
00028 
00029 
00030 
00031 #include <assert.h>
00032 #include "idmapc.h"
00033 
00034 #include "../oawconfig.h"
00035 
00036 OAW_BEGIN_NAMESPACE
00037 
00038 template <class Key, class Value>
00039 class MapC
00040 {
00041 public:
00042   MapC(){}
00043   MapC(const MapC<Key,Value>& a){
00044     m_keys = a.m_keys;
00045     m_values = a.m_values;
00046     m_occupied = a.m_occupied;
00047     m_freeIndexes = a.m_freeIndexes;
00048     m_blanc = a.m_blanc;
00049   }
00050 
00051   void SetBlanc(const Value& t){m_blanc = t;}
00052 
00053   void set(Key k, const Value& v){
00054     long ix = lookupIndex(k);
00055     if(ix<=0){
00056       if(m_freeIndexes.size()==0){
00057         m_values.push_back(v);
00058         m_keys.push_back(k);
00059         m_occupied.push_back(1);
00060       }else{
00061         m_freeIndexes.pop_back(ix);
00062         m_values[ix]=v;
00063         m_keys[ix]=k;
00064         m_occupied[ix]=1;
00065       }
00066     }else{
00067       m_values[ix]=v;
00068     }
00069   }
00070 
00071   inline const Value& get(const Key& k) const{
00072     long ix = lookupIndex(k);
00073     if(ix<0){return m_blanc;}
00074     return m_values.get(ix);
00075   }
00076 
00077   void RemoveKey(const Key& key){
00078     long ix = lookupIndex(i);
00079     if(ix<0){return;}
00080     m_occupied[ix]=0;
00081     m_freeIndexes.push_back(ix);
00082   }
00083 
00084   void Empty(){
00085     m_values.Empty();
00086     m_keys.Empty();
00087   }
00088 
00089   const MapC<Key,Value>& operator = (const MapC<Key,Value>& a){
00090     m_keys = a.m_keys;
00091     m_values = a.m_values;
00092     m_occupied = a.m_occupied;
00093     m_freeIndexes = a.m_freeIndexes;
00094     m_blanc = a.m_blanc;
00095     return *this;
00096   }
00097 
00098   inline long size() const {return m_keys.size();}
00099 
00100 protected:
00101 
00102   long lookupIndex(const Key& k) const{
00103     long i;
00104     for(i=0;i<m_keys.size();i++){
00105       if(m_occupied.get(i) && m_keys.get(i)==k){
00106         return i;
00107       }
00108     }
00109     return -1;
00110   }
00111 
00112 protected:
00113   ArrayC<Key> m_keys;
00114   ArrayC<Value> m_values;
00115   Value m_blanc;
00116 
00117   ArrayC<long> m_freeIndexes;
00118   ArrayC<short> m_occupied;
00119 };
00120 
00121 OAW_END_NAMESPACE
00122 
00123 
00124 #endif
00125 
00126 
00127 
00128 
00129 
00130 

Generated on Tue Jul 29 14:24:15 2003 for Open ActiveWrl by doxygen1.3-rc2