Isis 3 Developer Reference
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 
430  template < typename K, typename T,
431  template <class> class ComparePolicy = SimpleCompare,
432  template <class> class RemovalPolicy = NoopRemoval,
433  template <class> class CopyPolicy = DefaultCopy
434  >
435  class CollectorMap : public RemovalPolicy<T>, public CopyPolicy<T> {
436  public:
437  typedef T CollectorType;
438 
439  typedef std::multimap<K, CollectorType, ComparePolicy<K> > CollectorList;
441  typedef typename CollectorList::iterator CollectorIter;
443  typedef typename CollectorList::const_iterator CollectorConstIter;
444 
454  };
455 
457  CollectorMap() : _keyPolicy(UniqueKeys) { }
458 
468  CollectorMap(const KeyPolicy &keyPolicy) : _keyPolicy(keyPolicy) { }
469 
475  virtual ~CollectorMap() {
476  selfDestruct();
477  }
478 
488  CollectorMap(const CollectorMap &cmap) {
489  _keyPolicy = cmap._keyPolicy;
490  CollectorConstIter cItr;
491  for(cItr = cmap._list.begin() ; cItr != cmap._list.end() ; cItr++) {
492  _list.insert(std::make_pair(cItr->first, cmap.copy(cItr->second)));
493  }
494  }
495 
512  if(&cmap != this) {
513  selfDestruct();
514  _keyPolicy = cmap._keyPolicy;
515  CollectorConstIter cItr;
516  for(cItr = cmap._list.begin() ; cItr != cmap._list.end() ; cItr++) {
517  _list.insert(std::make_pair(cItr->first, cmap.copy(cItr->second)));
518  }
519  }
520  return (*this);
521  }
522 
528  int size() const {
529  return (_list.size());
530  }
531 
542  int count(const K &key) const {
543  return (_list.count(key));
544  }
545 
556  void add(const K &key, const T &value) {
557  if(_keyPolicy == UniqueKeys) remove(key);
558  _list.insert(std::make_pair(key, value));
559  return;
560  }
561 
567  bool exists(const K &key) const {
568  CollectorConstIter cItr = _list.find(key);
569  return (cItr != _list.end());
570  }
571 
583  T &get(const K &key) {
584  CollectorIter cItr = _list.find(key);
585  if(cItr == _list.end()) {
586  QString mess = "Requested value does not exist!";
588  }
589  return (cItr->second);
590  }
591 
599  const T &get(const K &key) const {
600  CollectorConstIter cItr = _list.find(key);
601  if(cItr == _list.end()) {
602  QString mess = "Requested value does not exist!";
604  }
605  return (cItr->second);
606  }
607 
620  int index(const K &key) const {
621  CollectorConstIter cItr = _list.lower_bound(key);
622  if(cItr == _list.end()) {
623  return (-1);
624  }
625  else {
626  return (std::distance(_list.begin(), cItr));
627  }
628  }
629 
640  T &getNth(int nth) {
641  CollectorIter cItr;
642  int i;
643  for(cItr = _list.begin(), i = 0 ; cItr != _list.end() ; ++cItr, i++) {
644  if(i == nth) break;
645  }
646 
647  if(cItr == _list.end()) {
648  std::ostringstream mess;
649  mess << "Requested index (" << nth << ") out of range" << std::endl;
650  throw IException(IException::Programmer, mess.str(), _FILEINFO_);
651  }
652  return (cItr->second);
653  }
654 
665  const T &getNth(int nth) const {
666  CollectorConstIter cItr;
667  int i;
668  for(cItr = _list.begin(), i = 0 ; cItr != _list.end() ; ++cItr, i++) {
669  if(i == nth) break;
670  }
671  if(cItr == _list.end()) {
672  std::ostringstream mess;
673  mess << "Requested index (" << nth << ") out of range" << std::endl;
674  throw IException(IException::Programmer, mess.str(), _FILEINFO_);
675  }
676  return (cItr->second);
677  }
678 
689  const K &key(int nth) const {
690  CollectorConstIter cItr;
691  int i;
692  for(cItr = _list.begin(), i = 0 ; cItr != _list.end() ; ++cItr, i++) {
693  if(i == nth) break;
694  }
695  if(cItr == _list.end()) {
696  std::ostringstream mess;
697  mess << "Requested key index (" << nth << ") out of range" << std::endl;
698  throw IException(IException::Programmer, mess.str(), _FILEINFO_);
699  }
700  return (cItr->first);
701  }
702 
710  int remove(const K &key) {
711  CollectorIter Itr1 = _list.lower_bound(key);
712  if(Itr1 == _list.end()) return (0);
713 
714  CollectorIter Itr2 = _list.upper_bound(key);
715  while(Itr1 != Itr2) {
716  this->destroy(&Itr1->second);
717  ++Itr1;
718  }
719  return (_list.erase(key));
720  }
721 
728  return _list.begin();
729  }
730 
737  return _list.end();
738  }
739 
746  return _list.begin();
747  }
748 
756  return _list.end();
757  }
758 
759  private:
760  KeyPolicy _keyPolicy;
761  CollectorList _list;
762 
770  void selfDestruct() {
771  CollectorIter itr;
772  for(itr = _list.begin() ; itr != _list.end() ; itr++) {
773  this->destroy(&itr->second);
774  }
775  _list.clear();
776  }
777 
778 
779  };
780 
781 };
782 #endif
CollectorMap(const KeyPolicy &keyPolicy)
Allows the user to choose if keys can be duplicated.
Definition: CollectorMap.h:468
const T & copy(const T &src) const
Returns a copy of the input.
Definition: CollectorMap.h:208
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:620
Provides a case insensitive string comparison.
Definition: CollectorMap.h:69
T & getNth(int nth)
Returns the nth value in the collection.
Definition: CollectorMap.h:640
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
bool operator()(const K &v1, const K &v2) const
Compares v1 and v2 as floating point values.
Definition: CollectorMap.h:103
Supplies a NOOP default for removal of a CollectorMap entry.
Definition: CollectorMap.h:118
CollectorList::iterator CollectorIter
CollectorList iterator type declaration.
Definition: CollectorMap.h:441
void destroy(T *element)
Destroys the CollectorMap entry.
Definition: CollectorMap.h:133
Allow duplication of keys.
Definition: CollectorMap.h:453
CollectorList::const_iterator CollectorConstIter
CollectorList constant iterator type declaration.
Definition: CollectorMap.h:443
CollectorMap()
Constructor.
Definition: CollectorMap.h:457
int size() const
Returns the size of the collection.
Definition: CollectorMap.h:528
std::multimap< K, CollectorType, ComparePolicy< K > > CollectorList
A multimap attacking a key to a CollectorType and a ComparePolicy<CollectorType>
Definition: CollectorMap.h:439
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:475
(Default) Policy for copying map elements
Definition: CollectorMap.h:198
int count(const K &key) const
Returns the number of keys found in the list.
Definition: CollectorMap.h:542
const K & key(int nth) const
Returns the nth key in the collection.
Definition: CollectorMap.h:689
Supplies a policy for deleting pointers that CollectorMap owns.
Definition: CollectorMap.h:150
Constrain keys to be unique.
Definition: CollectorMap.h:452
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
bool exists(const K &key) const
Checks the existance of a particular key in the list.
Definition: CollectorMap.h:567
void destroy(T *element)
Destroys the array of CollectorMaps.
Definition: CollectorMap.h:180
#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:556
const T & getNth(int nth) const
Returns the nth value in the collection.
Definition: CollectorMap.h:665
CollectorMap(const CollectorMap &cmap)
Copy constructor invokes the copy policy as provided by the users.
Definition: CollectorMap.h:488
CollectorIter end()
Returns the end of the list.
Definition: CollectorMap.h:755
KeyPolicy
Enumerated selection of key behaviour.
Definition: CollectorMap.h:452
Collector/container for arbitrary items.
Definition: CollectorMap.h:435
const double E
Sets some basic constants for use in ISIS programming.
Definition: Constants.h:55
CollectorConstIter end() const
Const iterator to end of list.
Definition: CollectorMap.h:736
IString DownCase()
Converts all upper case letters in the object IString into lower case characters. ...
Definition: IString.cpp:659
CollectorMap & operator=(const CollectorMap &cmap)
Assignment operator for the CollectorMap class object.
Definition: CollectorMap.h:511
T CollectorType
Data type.
Definition: CollectorMap.h:437
Isis exception class.
Definition: IException.h:99
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
bool operator()(const K &v1, const K &v2) const
Returns true if v1 is less than v2.
Definition: CollectorMap.h:56
CollectorIter begin()
Returns the start of the list for iterating purposes.
Definition: CollectorMap.h:745
Provides a simple comparison between two values.
Definition: CollectorMap.h:46
Pointer to object policy for copying map elements.
Definition: CollectorMap.h:230
CollectorConstIter begin() const
Const iterator into list.
Definition: CollectorMap.h:727
T copy(const T &src) const
Allocate new object using copy construtor and new pointer.
Definition: CollectorMap.h:242