casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SimOrdMap.h
Go to the documentation of this file.
00001 //# SimOrdMap.h: Simple map with ordered keys
00002 //# Copyright (C) 1993,1994,1995,1996,1999,2000
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: SimOrdMap.h 21090 2011-06-01 10:01:28Z gervandiepen $
00027 
00028 #ifndef CASA_SIMORDMAP_H
00029 #define CASA_SIMORDMAP_H
00030 
00031 #include <casa/aips.h>
00032 #include <casa/Containers/OrderedPair.h>
00033 #include <casa/Containers/Block.h>
00034 #include <casa/BasicSL/String.h>
00035 
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 //# Define a macro to cast kvblk[i] to OrderedPair<K,V>*.
00039 //# This is needed because the compiler outlines the inline functions pair.
00040 #define KVBLKpair(INX) ((OrderedPair<K,V>*)(kvblk[INX]))
00041 
00042 // <category lib=aips sect="Containers">
00043 // <summary>Simple map with keys ordered</summary>
00044 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
00045 // </reviewed>
00046 
00047 // SimpleOrderedMap<key,value> is a template class.
00048 // It is similar to OrderedMap<key,value>, but lacks its
00049 // sophisticated iterator capability. Instead iteration can be
00050 // done using the getKey and getVal function with a simple index.
00051 // The function ndefined() gives the number of key,value pairs in the map.
00052 // 
00053 // It uses a Block to store an array of pointers to the keys and
00054 // the associated values.
00055 // The keys and values themselves are stored on the heap.
00056 // The keys are kept in order to allow a binary search through
00057 // the keys for rapid access.
00058 //
00059 // This is one (simple) implementation of an ordered map.
00060 // It is not suitable for large arrays of keys, since the overhead
00061 // of keeping the keys in order would get too big.
00062 //
00063 // Exceptions are raised when new[] is failing or when the next()
00064 // getKey() or getValue() function is failing.
00065 //
00066 // The AipsIO >> and << operators are defined in <aips/SimOrdMapIO.h>.
00067 
00068 
00069 template<class K, class V> class SimpleOrderedMap
00070 {
00071 public:
00072 
00073     // Creates a map with the specified default value, "value", and the
00074     // internal block size.
00075     SimpleOrderedMap (const V& defaultValue, uInt size);
00076 
00077     // Creates a map with the specified default value, "value".
00078     explicit SimpleOrderedMap (const V& defaultValue);
00079 
00080     // Creates a map from another one; use copy semantics.
00081     SimpleOrderedMap (const SimpleOrderedMap<K,V>&);
00082 
00083     // Removes a map.
00084     ~SimpleOrderedMap ();
00085 
00086     // Assigns this SimpleOrderedMap to another with copy semantics.
00087     SimpleOrderedMap<K,V>& operator= (const SimpleOrderedMap<K,V>&);
00088 
00089     // Defines a mapping (ie. create a key value mapping)
00090     V &define (const K&, const V&);
00091 
00092     // This is the mapping function which maps keys to values. If the
00093     // map from the key to a value is not defined, a mapping will be
00094     // defined from the key to the default value (which is set from
00095     // the constructor. The "isDefined()" member function can be used
00096     // to check to see if a mapping is defined before using the
00097     // "operator()()".
00098     //
00099     // <note> With a constant map in the case where the key is not
00100     //        defined, the mapping between key and default value is 
00101     //        not created, but rather an exception is thrown.
00102     // </note>
00103     //+grp
00104     V &operator()(const K &ky);
00105     // <thrown>
00106     //   <li> indexError<K>
00107     // </thrown>
00108     const V &operator()(const K &ky) const;
00109     //-grp
00110 
00111     // Returns the default value for the Map.
00112     //+grp
00113     V &defaultVal()             {return DefaultVal;}
00114     const V &defaultVal() const {return DefaultVal;}
00115     //-grp
00116 
00117     // These functions check to see if a mapping is defined between
00118     // the specified key and some value. If one is, a pointer to
00119     // the value is returned, otherwise 0 is returned.
00120     //+grp
00121     V *isDefined(const K&);
00122     const V *isDefined(const K& k) const
00123         { return ((SimpleOrderedMap<K,V>*)this)->isDefined(k); }
00124     //-grp
00125 
00126     // Get the number of elements in the map.
00127     uInt ndefined() const { return nrused; }
00128 
00129     // Get the i-th key in the map.
00130     // It can be used to iterate through the keys as:
00131     // <code>
00132     //   for (uInt i=0; i<map.ndefined(); i++) {
00133     //       cout << map.getKey(i) << " " << map.getVal(i) << endl;
00134     //   }
00135     // </code>
00136     // Index checking is only done if Block is doing it.
00137     const K& getKey (uInt inx) const
00138         { return KVBLKpair(inx)->x(); }
00139 
00140     // Get the i-th value in the map.
00141     // It can be used to iterate through the keys as:
00142     // <code>
00143     //   for (uInt i=0; i<map.ndefined(); i++) {
00144     //       cout << map.getKey(i) << " " << map.getVal(i) << endl;
00145     //   }
00146     // </code>
00147     // Index checking is only done if Block is doing it.
00148     //+grp
00149     const V& getVal (uInt inx) const
00150         { return KVBLKpair(inx)->y(); }
00151     V& getVal (uInt inx)
00152         { return KVBLKpair(inx)->y(); }
00153     //-grp
00154 
00155 
00156     // Rename a key.
00157     // If the new key already exists, the existing key will be removed.
00158     // <thrown>
00159     //   <li> indexError<K>
00160     // </thrown>
00161     void rename (const K& newkey, const K& oldkey);
00162 
00163     // Undefines a mapping (ie. remove a key value mapping).
00164     // <thrown>
00165     //   <li> indexError<K>
00166     // </thrown>
00167     void remove (const K&);
00168 
00169     // Clear the entire map (ie. remove all mappings).
00170     void clear ();
00171 
00172     // Get the total size of the block in use.
00173     uInt ntot() const  { return kvblk.nelements(); }
00174 
00175     // Get or set the Block allocation increment.
00176     //+grp
00177     uInt incr() const { return nrincr; }
00178     uInt incr(uInt nri) { return (nrincr = nri); }
00179     //-grp
00180 
00181     // Check the internal state.
00182     // <thrown>
00183     //   <li> AipsError
00184     // </thrown>
00185     Bool ok() const;
00186 
00187     // Version for major change (used by SimOrdMapIO).
00188     // enum did not work properly with cfront 3.0.1), so replaced
00189     // by a static inline function. Users won't normally use this.
00190     //*display 8
00191     static uInt Version() {return 1;}
00192 
00193 protected:
00194     // The blocks to hold the keys and values
00195     // and the total, used and increment size of these blocks.
00196     Block<void*> kvblk;
00197     uInt         nrused;
00198     uInt         nrincr;
00199     V            DefaultVal;
00200 
00201     // Binary search for the key.
00202     uInt findKey (const K&, Bool&) const;
00203 
00204     // Copy from another Block of OrderedPair's.
00205     void copyBlock (const SimpleOrderedMap<K,V>&);
00206 };
00207 
00208 
00209 } //# NAMESPACE CASA - END
00210 
00211 #ifndef CASACORE_NO_AUTO_TEMPLATES
00212 #include <casa/Containers/SimOrdMap.tcc>
00213 #endif //# CASACORE_NO_AUTO_TEMPLATES
00214 #endif