Isis Developer Reference
CollectorMap.h
Go to the documentation of this file.
1#ifndef CollectorMap_h
2#define CollectorMap_h
8/* SPDX-License-Identifier: CC0-1.0 */
9#include <cctype>
10#include <QString>
11#include <map>
12#include <algorithm>
13#include <functional>
14#include <sstream>
15#include <iomanip>
16#include <exception>
17#include "IException.h"
18#include "IString.h"
19#include <gsl/gsl_math.h>
20
21
22namespace Isis {
23
30 template <typename K> struct SimpleCompare {
31
40 bool operator()(const K &v1, const K &v2) const {
41 return (v1 < v2);
42 }
43
44 };
45
53 template <typename K> struct NoCaseStringCompare {
54
64 bool operator()(const K &v1, const K &v2) const {
65 return (IString::DownCase(v1) < IString::DownCase(v2));
66 }
67
68 };
69
77 template <typename K> struct RobustFloatCompare {
78
87 bool operator()(const K &v1, const K &v2) const {
88 return (gsl_fcmp(v1, v2, -1.0E-6) < 0);
89 }
90
91 };
92
102 template <typename T> struct NoopRemoval {
103
104 // defines used for cross platform suppression of unused parameter warning. This will prevent
105 // distroy() from causing unused parameter warnings in clang. This is done for the purpose
106 // of porting ISIS to OSX 10.11
107 // #define MON_Internal_UnusedStringify(macro_arg_string_literal) #macro_arg_string_literal
108 // #define MONUnusedParameter(macro_arg_parameter) _Pragma(MON_Internal_UnusedStringify(unused(macro_arg_parameter)))
109
110 protected:
111
117 void destroy(T *element) {
118 // arbitrary cast type to suppress unused parameter warnings generated
119 // by clang when building on mac
120 (void)element;
121
122 return;
123 }
124
125 };
126
134 template <typename T> struct PointerRemoval {
135 protected:
136
142 void destroy(T *element) {
143 delete(*element);
144 return;
145 }
146
147 };
148
156 template <typename T> struct ArrayRemoval {
157 protected:
158
164 void destroy(T *element) {
165 delete [](*element);
166 return;
167 }
168
169 };
170
171
182 template <typename T> struct DefaultCopy {
183 protected:
184
192 const T &copy(const T &src) const {
193 return (src);
194 }
195
196 };
197
198
214 template <typename T> struct PointerCopy {
215 protected:
226 T copy(const T &src) const {
227 return (allocate(*(src)));
228 }
229
230 private:
238 template <typename P>
239 P *allocate(const P &obj) const {
240 return (new P(obj));
241 }
242 };
243
244
245
414 template < typename K, typename T,
415 template <class> class ComparePolicy = SimpleCompare,
416 template <class> class RemovalPolicy = NoopRemoval,
417 template <class> class CopyPolicy = DefaultCopy
418 >
419 class CollectorMap : public RemovalPolicy<T>, public CopyPolicy<T> {
420 public:
421 typedef T CollectorType;
423 typedef std::multimap<K, CollectorType, ComparePolicy<K> > CollectorList;
425 typedef typename CollectorList::iterator CollectorIter;
427 typedef typename CollectorList::const_iterator CollectorConstIter;
428
439
441 CollectorMap() : _keyPolicy(UniqueKeys) { }
442
452 CollectorMap(const KeyPolicy &keyPolicy) : _keyPolicy(keyPolicy) { }
453
459 virtual ~CollectorMap() {
460 selfDestruct();
461 }
462
473 _keyPolicy = cmap._keyPolicy;
475 for(cItr = cmap._list.begin() ; cItr != cmap._list.end() ; cItr++) {
476 _list.insert(std::make_pair(cItr->first, cmap.copy(cItr->second)));
477 }
478 }
479
496 if(&cmap != this) {
497 selfDestruct();
498 _keyPolicy = cmap._keyPolicy;
500 for(cItr = cmap._list.begin() ; cItr != cmap._list.end() ; cItr++) {
501 _list.insert(std::make_pair(cItr->first, cmap.copy(cItr->second)));
502 }
503 }
504 return (*this);
505 }
506
512 int size() const {
513 return (_list.size());
514 }
515
526 int count(const K &key) const {
527 return (_list.count(key));
528 }
529
540 void add(const K &key, const T &value) {
541 if(_keyPolicy == UniqueKeys) remove(key);
542 _list.insert(std::make_pair(key, value));
543 return;
544 }
545
551 bool exists(const K &key) const {
552 CollectorConstIter cItr = _list.find(key);
553 return (cItr != _list.end());
554 }
555
567 T &get(const K &key) {
568 CollectorIter cItr = _list.find(key);
569 if(cItr == _list.end()) {
570 QString mess = "Requested value does not exist!";
572 }
573 return (cItr->second);
574 }
575
583 const T &get(const K &key) const {
584 CollectorConstIter cItr = _list.find(key);
585 if(cItr == _list.end()) {
586 QString mess = "Requested value does not exist!";
588 }
589 return (cItr->second);
590 }
591
604 int index(const K &key) const {
605 CollectorConstIter cItr = _list.lower_bound(key);
606 if(cItr == _list.end()) {
607 return (-1);
608 }
609 else {
610 return (std::distance(_list.begin(), cItr));
611 }
612 }
613
624 T &getNth(int nth) {
625 CollectorIter cItr;
626 int i;
627 for(cItr = _list.begin(), i = 0 ; cItr != _list.end() ; ++cItr, i++) {
628 if(i == nth) break;
629 }
630
631 if(cItr == _list.end()) {
632 std::ostringstream mess;
633 mess << "Requested index (" << nth << ") out of range" << std::endl;
634 throw IException(IException::Programmer, mess.str(), _FILEINFO_);
635 }
636 return (cItr->second);
637 }
638
649 const T &getNth(int nth) const {
651 int i;
652 for(cItr = _list.begin(), i = 0 ; cItr != _list.end() ; ++cItr, i++) {
653 if(i == nth) break;
654 }
655 if(cItr == _list.end()) {
656 std::ostringstream mess;
657 mess << "Requested index (" << nth << ") out of range" << std::endl;
658 throw IException(IException::Programmer, mess.str(), _FILEINFO_);
659 }
660 return (cItr->second);
661 }
662
673 const K &key(int nth) const {
675 int i;
676 for(cItr = _list.begin(), i = 0 ; cItr != _list.end() ; ++cItr, i++) {
677 if(i == nth) break;
678 }
679 if(cItr == _list.end()) {
680 std::ostringstream mess;
681 mess << "Requested key index (" << nth << ") out of range" << std::endl;
682 throw IException(IException::Programmer, mess.str(), _FILEINFO_);
683 }
684 return (cItr->first);
685 }
686
694 int remove(const K &key) {
695 CollectorIter Itr1 = _list.lower_bound(key);
696 if(Itr1 == _list.end()) return (0);
697
698 CollectorIter Itr2 = _list.upper_bound(key);
699 while(Itr1 != Itr2) {
700 this->destroy(&Itr1->second);
701 ++Itr1;
702 }
703 return (_list.erase(key));
704 }
705
712 return _list.begin();
713 }
714
721 return _list.end();
722 }
723
730 return _list.begin();
731 }
732
740 return _list.end();
741 }
742
743 private:
744 KeyPolicy _keyPolicy;
745 CollectorList _list;
746
754 void selfDestruct() {
755 CollectorIter itr;
756 for(itr = _list.begin() ; itr != _list.end() ; itr++) {
757 this->destroy(&itr->second);
758 }
759 _list.clear();
760 }
761
762
763 };
764
765};
766#endif
#define _FILEINFO_
Macro for the filename and line number.
Definition IException.h:24
Collector/container for arbitrary items.
Definition CollectorMap.h:419
const K & key(int nth) const
Returns the nth key in the collection.
Definition CollectorMap.h:673
virtual ~CollectorMap()
Destructor handles removal of the elements within the collection.
Definition CollectorMap.h:459
CollectorMap & operator=(const CollectorMap &cmap)
Assignment operator for the CollectorMap class object.
Definition CollectorMap.h:495
int size() const
Returns the size of the collection.
Definition CollectorMap.h:512
std::multimap< K, CollectorType, ComparePolicy< K > > CollectorList
A multimap attacking a key to a CollectorType and a ComparePolicy<CollectorType>
Definition CollectorMap.h:423
CollectorList::const_iterator CollectorConstIter
CollectorList constant iterator type declaration.
Definition CollectorMap.h:427
int remove(const K &key)
Removes and entry from the list.
Definition CollectorMap.h:694
CollectorMap()
Constructor.
Definition CollectorMap.h:441
T & get(const K &key)
Returns the value associated with the name provided.
Definition CollectorMap.h:567
bool exists(const K &key) const
Checks the existance of a particular key in the list.
Definition CollectorMap.h:551
KeyPolicy
Enumerated selection of key behaviour.
Definition CollectorMap.h:436
@ UniqueKeys
Constrain keys to be unique.
Definition CollectorMap.h:436
@ DuplicateKeys
Allow duplication of keys.
Definition CollectorMap.h:437
CollectorMap(const KeyPolicy &keyPolicy)
Allows the user to choose if keys can be duplicated.
Definition CollectorMap.h:452
int index(const K &key) const
Returns the index of the first occuring element in the list.
Definition CollectorMap.h:604
CollectorIter end()
Returns the end of the list.
Definition CollectorMap.h:739
const T & get(const K &key) const
Const version returning the value associated with the given name.
Definition CollectorMap.h:583
CollectorIter begin()
Returns the start of the list for iterating purposes.
Definition CollectorMap.h:729
const T & getNth(int nth) const
Returns the nth value in the collection.
Definition CollectorMap.h:649
CollectorConstIter begin() const
Const iterator into list.
Definition CollectorMap.h:711
CollectorConstIter end() const
Const iterator to end of list.
Definition CollectorMap.h:720
CollectorMap(const CollectorMap &cmap)
Copy constructor invokes the copy policy as provided by the users.
Definition CollectorMap.h:472
T CollectorType
Data type.
Definition CollectorMap.h:421
void add(const K &key, const T &value)
Adds the element to the list.
Definition CollectorMap.h:540
int count(const K &key) const
Returns the number of keys found in the list.
Definition CollectorMap.h:526
T & getNth(int nth)
Returns the nth value in the collection.
Definition CollectorMap.h:624
CollectorList::iterator CollectorIter
CollectorList iterator type declaration.
Definition CollectorMap.h:425
Isis exception class.
Definition IException.h:91
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
IString DownCase()
Converts all upper case letters in the object IString into lower case characters.
Definition IString.cpp:644
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
Policy for deleting arrays that CollectorMap owns.
Definition CollectorMap.h:156
void destroy(T *element)
Destroys the array of CollectorMaps.
Definition CollectorMap.h:164
(Default) Policy for copying map elements
Definition CollectorMap.h:182
const T & copy(const T &src) const
Returns a copy of the input.
Definition CollectorMap.h:192
Provides a case insensitive string comparison.
Definition CollectorMap.h:53
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:64
Supplies a NOOP default for removal of a CollectorMap entry.
Definition CollectorMap.h:102
void destroy(T *element)
Destroys the CollectorMap entry.
Definition CollectorMap.h:117
Pointer to object policy for copying map elements.
Definition CollectorMap.h:214
T copy(const T &src) const
Allocate new object using copy construtor and new pointer.
Definition CollectorMap.h:226
Supplies a policy for deleting pointers that CollectorMap owns.
Definition CollectorMap.h:134
void destroy(T *element)
Destroys the CollectorMap pointer's CollectorMap.
Definition CollectorMap.h:142
Provides a robust comparison of double/float values.
Definition CollectorMap.h:77
bool operator()(const K &v1, const K &v2) const
Compares v1 and v2 as floating point values.
Definition CollectorMap.h:87
Provides a simple comparison between two values.
Definition CollectorMap.h:30
bool operator()(const K &v1, const K &v2) const
Returns true if v1 is less than v2.
Definition CollectorMap.h:40