Isis 3.0
Home
CollectorMap.h
Go to the documentation of this file.
1 #ifndef CollectorMap_h
2 #define CollectorMap_h
3 
25 #include <cctype>
26 #include <QString>
27 #include <map>
28 #include <algorithm>
29 #include <functional>
30 #include <sstream>
31 #include <iomanip>
32 #include <exception>
33 #include "IException.h"
34 #include "IString.h"
35 #include <gsl/gsl_math.h>
36 
37 
38 namespace Isis {
39 
46  template <typename K> struct SimpleCompare {
47 
56  bool operator()(const K &v1, const K &v2) const {
57  return (v1 < v2);
58  }
59 
60  };
61 
69  template <typename K> struct NoCaseStringCompare {
70 
80  bool operator()(const K &v1, const K &v2) const {
81  return (IString::DownCase(v1) < IString::DownCase(v2));
82  }
83 
84  };
85 
93  template <typename K> struct RobustFloatCompare {
94 
103  bool operator()(const K &v1, const K &v2) const {
104  return (gsl_fcmp(v1, v2, -1.0E-6) < 0);
105  }
106 
107  };
108 
118  template <typename T> struct NoopRemoval {
119 
120  // defines used for cross platform suppression of unused parameter warning. This will prevent
121  // distroy() from causing unused parameter warnings in clang. This is done for the purpose
122  // of porting ISIS to OSX 10.11
123  // #define MON_Internal_UnusedStringify(macro_arg_string_literal) #macro_arg_string_literal
124  // #define MONUnusedParameter(macro_arg_parameter) _Pragma(MON_Internal_UnusedStringify(unused(macro_arg_parameter)))
125 
126  protected:
127 
133  void destroy(T *element) {
134  // arbitrary cast type to suppress unused parameter warnings generated
135  // by clang when building on mac
136  (void)element;
137 
138  return;
139  }
140 
141  };
142 
150  template <typename T> struct PointerRemoval {
151  protected:
152 
158  void destroy(T *element) {
159  delete(*element);
160  return;
161  }
162 
163  };
164 
172  template <typename T> struct ArrayRemoval {
173  protected:
174 
180  void destroy(T *element) {
181  delete [](*element);
182  return;
183  }
184 
185  };
186 
187 
198  template <typename T> struct DefaultCopy {
199  protected:
200 
208  const T &copy(const T &src) const {
209  return (src);
210  }
211 
212  };
213 
214 
230  template <typename T> struct PointerCopy {
231  protected:
242  T copy(const T &src) const {
243  return (allocate(*(src)));
244  }
245 
246  private:
254  template <typename P>
255  P *allocate(const P &obj) const {
256  return (new P(obj));
257  }
258  };
259 
260 
261 
428  template < typename K, typename T,
429  template <class> class ComparePolicy = SimpleCompare,
430  template <class> class RemovalPolicy = NoopRemoval,
431  template <class> class CopyPolicy = DefaultCopy
432  >
433  class CollectorMap : public RemovalPolicy<T>, public CopyPolicy<T> {
434  public:
435  typedef T CollectorType;
436  typedef std::multimap<K, CollectorType, ComparePolicy<K> > CollectorList;
439  typedef typename CollectorList::iterator CollectorIter;
441  typedef typename CollectorList::const_iterator CollectorConstIter;
442 
452  };
453 
455  CollectorMap() : _keyPolicy(UniqueKeys) { }
456 
466  CollectorMap(const KeyPolicy &keyPolicy) : _keyPolicy(keyPolicy) { }
467 
473  virtual ~CollectorMap() {
474  selfDestruct();
475  }
476 
486  CollectorMap(const CollectorMap &cmap) {
487  _keyPolicy = cmap._keyPolicy;
488  CollectorConstIter cItr;
489  for(cItr = cmap._list.begin() ; cItr != cmap._list.end() ; cItr++) {
490  _list.insert(std::make_pair(cItr->first, cmap.copy(cItr->second)));
491  }
492  }
493 
508  if(&cmap != this) {
509  selfDestruct();
510  _keyPolicy = cmap._keyPolicy;
511  CollectorConstIter cItr;
512  for(cItr = cmap._list.begin() ; cItr != cmap._list.end() ; cItr++) {
513  _list.insert(std::make_pair(cItr->first, cmap.copy(cItr->second)));
514  }
515  }
516  return (*this);
517  }
518 
524  int size() const {
525  return (_list.size());
526  }
527 
538  int count(const K &key) const {
539  return (_list.count(key));
540  }
541 
552  void add(const K &key, const T &value) {
553  if(_keyPolicy == UniqueKeys) remove(key);
554  _list.insert(std::make_pair(key, value));
555  return;
556  }
557 
563  bool exists(const K &key) const {
564  CollectorConstIter cItr = _list.find(key);
565  return (cItr != _list.end());
566  }
567 
579  T &get(const K &key) {
580  CollectorIter cItr = _list.find(key);
581  if(cItr == _list.end()) {
582  QString mess = "Requested value does not exist!";
584  }
585  return (cItr->second);
586  }
587 
593  const T &get(const K &key) const {
594  CollectorConstIter cItr = _list.find(key);
595  if(cItr == _list.end()) {
596  QString mess = "Requested value does not exist!";
598  }
599  return (cItr->second);
600  }
601 
614  int index(const K &key) const {
615  CollectorConstIter cItr = _list.lower_bound(key);
616  if(cItr == _list.end()) {
617  return (-1);
618  }
619  else {
620  return (std::distance(_list.begin(), cItr));
621  }
622  }
623 
634  T &getNth(int nth) {
635  CollectorIter cItr;
636  int i;
637  for(cItr = _list.begin(), i = 0 ; cItr != _list.end() ; ++cItr, i++) {
638  if(i == nth) break;
639  }
640 
641  if(cItr == _list.end()) {
642  std::ostringstream mess;
643  mess << "Requested index (" << nth << ") out of range" << std::endl;
644  throw IException(IException::Programmer, mess.str(), _FILEINFO_);
645  }
646  return (cItr->second);
647  }
648 
659  const T &getNth(int nth) const {
660  CollectorConstIter cItr;
661  int i;
662  for(cItr = _list.begin(), i = 0 ; cItr != _list.end() ; ++cItr, i++) {
663  if(i == nth) break;
664  }
665  if(cItr == _list.end()) {
666  std::ostringstream mess;
667  mess << "Requested index (" << nth << ") out of range" << std::endl;
668  throw IException(IException::Programmer, mess.str(), _FILEINFO_);
669  }
670  return (cItr->second);
671  }
672 
683  const K &key(int nth) const {
684  CollectorConstIter cItr;
685  int i;
686  for(cItr = _list.begin(), i = 0 ; cItr != _list.end() ; ++cItr, i++) {
687  if(i == nth) break;
688  }
689  if(cItr == _list.end()) {
690  std::ostringstream mess;
691  mess << "Requested key index (" << nth << ") out of range" << std::endl;
692  throw IException(IException::Programmer, mess.str(), _FILEINFO_);
693  }
694  return (cItr->first);
695  }
696 
704  int remove(const K &key) {
705  CollectorIter Itr1 = _list.lower_bound(key);
706  if(Itr1 == _list.end()) return (0);
707 
708  CollectorIter Itr2 = _list.upper_bound(key);
709  while(Itr1 != Itr2) {
710  this->destroy(&Itr1->second);
711  ++Itr1;
712  }
713  return (_list.erase(key));
714  }
715 
722  return _list.begin();
723  }
724 
731  return _list.end();
732  }
733 
740  return _list.begin();
741  }
742 
750  return _list.end();
751  }
752 
753  private:
754  KeyPolicy _keyPolicy;
755  CollectorList _list;
756 
764  void selfDestruct() {
765  CollectorIter itr;
766  for(itr = _list.begin() ; itr != _list.end() ; itr++) {
767  this->destroy(&itr->second);
768  }
769  _list.clear();
770  }
771 
772 
773  };
774 
775 };
776 #endif
CollectorMap(const KeyPolicy &keyPolicy)
Allows the user to choose if keys can be duplicated.
Definition: CollectorMap.h:466
const K & key(int nth) const
Returns the nth key in the collection.
Definition: CollectorMap.h:683
bool exists(const K &key) const
Checks the existance of a particular key in the list.
Definition: CollectorMap.h:563
const double E(2.7182818284590452354)
Sets some basic constants for use in ISIS programming.
Policy for deleting arrays that CollectorMap owns.
Definition: CollectorMap.h:172
int index(const K &key) const
Returns the index of the first occuring element in the list.
Definition: CollectorMap.h:614
Provides a case insensitive string comparison.
Definition: CollectorMap.h:69
T & getNth(int nth)
Returns the nth value in the collection.
Definition: CollectorMap.h:634
CollectorConstIter begin() const
Const iterator into list.
Definition: CollectorMap.h:721
void destroy(T *element)
Destroys the CollectorMap pointer&#39;s CollectorMap.
Definition: CollectorMap.h:158
Provides a robust comparison of double/float values.
Definition: CollectorMap.h:93
Supplies a NOOP default for removal of a CollectorMap entry.
Definition: CollectorMap.h:118
CollectorList::iterator CollectorIter
CollectorList iterator type declaration.
Definition: CollectorMap.h:439
void destroy(T *element)
Destroys the CollectorMap entry.
Definition: CollectorMap.h:133
Allow duplication of keys.
Definition: CollectorMap.h:451
CollectorList::const_iterator CollectorConstIter
CollectorList constant iterator type declaration.
Definition: CollectorMap.h:441
CollectorMap()
Constructor.
Definition: CollectorMap.h:455
std::multimap< K, CollectorType, ComparePolicy< K > > CollectorList
Definition: CollectorMap.h:437
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:154
virtual ~CollectorMap()
Destructor handles removal of the elements within the collection.
Definition: CollectorMap.h:473
const T & getNth(int nth) const
Returns the nth value in the collection.
Definition: CollectorMap.h:659
(Default) Policy for copying map elements
Definition: CollectorMap.h:198
Supplies a policy for deleting pointers that CollectorMap owns.
Definition: CollectorMap.h:150
Constrain keys to be unique.
Definition: CollectorMap.h:450
void destroy(T *element)
Destroys the array of CollectorMaps.
Definition: CollectorMap.h:180
int count(const K &key) const
Returns the number of keys found in the list.
Definition: CollectorMap.h:538
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:38
void add(const K &key, const T &value)
Adds the element to the list.
Definition: CollectorMap.h:552
CollectorMap(const CollectorMap &cmap)
Copy constructor invokes the copy policy as provided by the users.
Definition: CollectorMap.h:486
CollectorIter end()
Returns the end of the list.
Definition: CollectorMap.h:749
bool operator()(const K &v1, const K &v2) const
Returns true if v1 is less than v2.
Definition: CollectorMap.h:56
KeyPolicy
Enumerated selection of key behaviour.
Definition: CollectorMap.h:450
Collector/container for arbitrary items.
Definition: CollectorMap.h:433
IString DownCase()
Converts all upper case letters in the object IString into lower case characters. ...
Definition: IString.cpp:659
CollectorConstIter end() const
Const iterator to end of list.
Definition: CollectorMap.h:730
const T & copy(const T &src) const
Returns a copy of the input.
Definition: CollectorMap.h:208
CollectorMap & operator=(const CollectorMap &cmap)
Assignment operator for the CollectorMap class object.
Definition: CollectorMap.h:507
T CollectorType
Data type A multimap attacking a key t...
Definition: CollectorMap.h:435
int size() const
Returns the size of the collection.
Definition: CollectorMap.h:524
Isis exception class.
Definition: IException.h:99
bool operator()(const K &v1, const K &v2) const
Compares v1 and v2 as floating point values.
Definition: CollectorMap.h:103
CollectorIter begin()
Returns the start of the list for iterating purposes.
Definition: CollectorMap.h:739
Provides a simple comparison between two values.
Definition: CollectorMap.h:46
bool operator()(const K &v1, const K &v2) const
Compares v1 and v2 as case insensitive strings, and returns true of v1 is less than v2 (as those stri...
Definition: CollectorMap.h:80
Pointer to object policy for copying map elements.
Definition: CollectorMap.h:230
T copy(const T &src) const
Allocate new object using copy construtor and new pointer.
Definition: CollectorMap.h:242