Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

AssociativeArray.h

Go to the documentation of this file.
00001 
00002 //
00003 // This file is part of the MADELINE 2 program 
00004 // written by Edward H. Trager, Ritu Khanna and Adrian Marrs
00005 // Copyright (c) 2005 by the
00006 // Regents of the University of Michigan.
00007 // All Rights Reserved.
00008 // 
00009 // The latest version of this program is available from:
00010 // 
00011 //   http://eyegene.ophthy.med.umich.edu/madeline/
00012 //   
00013 // Released under the GNU General Public License.
00014 // A copy of the GPL is included in the distribution
00015 // package of this software, or see:
00016 // 
00017 //   http://www.gnu.org/copyleft/
00018 //   
00019 // ... for licensing details.
00020 // 
00022 //
00023 // 2005.07.17.RK.ET
00024 //
00025 #ifndef ASSOCIATIVEARRAY_INCLUDED
00026 #define ASSOCIATIVEARRAY_INCLUDED
00027 
00028 #include "Variable.h"
00029 #include <map>
00030 
00031 
00032 struct compare {
00033         
00034         bool operator() (Data* a, Data* b) const {
00035                 return *a < *b;
00036         }
00037         
00038 };
00039 
00041 
00045 class AssociativeArray : public Variable{
00046 private:
00047         std::map<Data*,Data*,compare> _map; 
00048         short _keyType;                     
00050         bool _keyIsValid(Data*,const std::string key);
00051 public:
00052         //
00053         // Constructors/Destructors:
00054         //
00055         AssociativeArray(const std::string & name,const short keyType,const short valueType) : Variable(name,valueType), _keyType(keyType){ }
00056         
00057         ~AssociativeArray(){ 
00058                 std::map<Data*,Data*,compare>::const_iterator p;
00059                 p=_map.begin();
00060                 while(p != _map.end()){
00061                         delete (p->first);
00062                         delete (p->second);
00063                         p++;
00064                 }
00065                 _map.clear();
00066         }
00067         
00068         // Copy Constructor:
00069         AssociativeArray(const AssociativeArray& s) : Variable(s._name,s._valueType),_keyType(s._keyType) {
00070                 std::map<Data*,Data*,compare>::const_iterator p;
00071                 p = s._map.begin();
00072                 while(p != s._map.end()){
00073                         _map.insert(std::pair<Data*,Data*>(p->first->clone(),p->second->clone()));
00074                         p++;
00075                 }
00076         }
00077         //
00078         // Assignment Operator:
00079         //
00080         AssociativeArray& operator= (const AssociativeArray& s);
00081         //
00082         //  Methods required by Variable virtual base class:
00083         //
00084         const std::string get( void ) const;
00085         const std::string getValueType() const;
00086         const std::string getVariableType() const { return "AssociativeArray"; }
00087         //
00088         // Additional methods not present in the virtual base class:
00089         //
00090         void insert(const short keyType,const std::string key,const short valueType,const std::string value);
00091         const std::string getKeyType() const;
00092         //
00093         // Friend functions:
00094         //
00095         friend bool operator==(const AssociativeArray& a, const AssociativeArray& b);
00096 };
00097 
00098         std::ostream& operator<< (std::ostream& out, const AssociativeArray& var);
00099 
00100 #endif

Generated on Fri Nov 18 16:24:39 2011 for MADELINE by  doxygen 1.4.4