Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlGraphics/NaryQuickMap.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2011, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #ifndef NaryQuickSet_INCLUDE_ONCE
00033 #define NaryQuickSet_INCLUDE_ONCE
00034 
00035 #include <vlCore/Object.hpp>
00036 
00037 namespace vl
00038 {
00042   template<typename KeyType, typename ValueType, int MaxMapType>
00043   class NaryQuickMap: public Object
00044   {
00045   public:
00046     NaryQuickMap()
00047     {
00048       reset();
00049     }
00050 
00051     void reset()
00052     {
00053       mMapSize = 0;
00054       memset(mKeyToValue, 0, sizeof(mKeyToValue));
00055       memset(mValueToKey, 0, sizeof(mValueToKey));
00056     }
00057 
00058     void clear()
00059     {
00060       // no need to touch anything else.. you clever VL!
00061       mMapSize = 0;
00062     }
00063 
00064     int size() const { return mMapSize; }
00065 
00066     ValueType* begin() { return mValues; }
00067     ValueType* end() { return mValues + mMapSize; }
00068     const ValueType* begin() const { return mValues; }
00069     const ValueType* end() const { return mValues + mMapSize; }
00070 
00071     void append(KeyType key)
00072     {
00073       append(key, key);
00074     }
00075 
00076     // use this when you know that 'key' is not already in the set.
00077     void append(KeyType key, ValueType value)
00078     {
00079       // sovrascrivi 'key' in ogni caso
00080       int pos = mMapSize++;
00081       mKeyToValue[key] = pos;
00082       mValueToKey[pos] = key;
00083       mValues[pos] = value;
00084     }
00085 
00086     void insert(KeyType key)
00087     {
00088       insert(key, key);
00089     }
00090 
00091     void insert(KeyType key, const ValueType& value)
00092     {
00093       VL_CHECK(key < MaxMapType)
00094       int pos = find(key);
00095       if (pos == -1)
00096       {
00097         pos = mMapSize++;
00098         VL_CHECK(pos < MaxMapType)
00099         mKeyToValue[key] = pos;
00100         mValueToKey[pos] = key;
00101       }
00102       mValues[pos] = value;
00103     }
00104 
00105     void erase(KeyType key)
00106     {
00107       int pos = find(key);
00108       if (pos != -1)
00109       {
00110         // move the last object to the one being erased
00111         if (mMapSize>1)
00112         {
00113           // move value
00114           mValues[pos] = mValues[mMapSize-1];
00115           // move Enum
00116           mValueToKey[pos] = mValueToKey[mMapSize-1];
00117           // mark moved KeyType to point to the new pos
00118           mKeyToValue[mValueToKey[pos]] = pos;
00119         }
00120         mMapSize--;
00121         VL_CHECK(mMapSize >= 0)
00122       }
00123     }
00124 
00125     int find(KeyType key) const
00126     {
00127       int pos = mKeyToValue[key];
00128       VL_CHECK(pos >= 0)
00129       VL_CHECK(pos < MaxMapType)
00130       if (pos < mMapSize)
00131       {
00132         KeyType e = mValueToKey[pos];
00133         VL_CHECK(e >= 0)
00134         VL_CHECK(e < MaxMapType)
00135         if (e == key)
00136           return pos;
00137         else
00138           return -1;
00139       }
00140       else
00141         return -1;
00142     }
00143 
00144     bool hasKey(KeyType key) const
00145     {
00146       return find(key) != -1;
00147     }
00148 
00149     const ValueType& valueFromKey(KeyType key) const
00150     {
00151         VL_CHECK(key >= 0)
00152         VL_CHECK(key < MaxMapType)
00153         VL_CHECK(mKeyToValue[key] >= 0)
00154         VL_CHECK(mKeyToValue[key] < MaxMapType)
00155         return mValues[mKeyToValue[key]];
00156     }
00157 
00158     const ValueType& valueFromIndex(int i) const
00159     {
00160       return mValues[i];
00161     }
00162 
00163     KeyType key(int i) const
00164     {
00165       return mValueToKey[i];
00166     }
00167 
00168   protected:
00169     // Note:
00170     // In the case of EEnable we don't really need this 
00171     // (we would need only a set, not a map) but 
00172     // for the moment we use this for simplicity.
00173     ValueType mValues[MaxMapType]; 
00174     // given an index (< mMapSize) of a value returns it's KeyType (== index in mKeyToValue)
00175     KeyType mValueToKey[MaxMapType];
00176     // the number of elements in mValues and mValueToKey
00177     int mMapSize;
00178     // given a KeyType gives where in mValues and mValueToKey the value is.
00179     int mKeyToValue[MaxMapType]; 
00180   };
00181 
00182 }
00183 
00184 #endif

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:38.
Permission is granted to use this page to write and publish articles regarding Visualization Library.