The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
multimapwdef.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * multimapwdef.h - template multimapwithdefault: a template which extends
4  * multipmap with easy access operators to
5  * obtain entries which are not repeated, plus a few
6  * nice convenience methods
7  *
8  * $Id: multimapwdef.h 3786 2020-08-30 11:35:14Z scribe $
9  *
10  * Copyright 2002-2013 CrossWire Bible Society (http://www.crosswire.org)
11  * CrossWire Bible Society
12  * P. O. Box 2528
13  * Tempe, AZ 85280-2528
14  *
15  * This program is free software; you can redistribute it and/or modify it
16  * under the terms of the GNU General Public License as published by the
17  * Free Software Foundation version 2.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * General Public License for more details.
23  *
24  */
25 
26 #ifndef MULTIMAPWDEF_H
27 #define MULTIMAPWDEF_H
28 
29 #include <map>
30 
32 
33 // multmap that still lets you use [] to reference FIRST
34 // entry of a key if multiples exist
35 template <class Key, class T, class Compare>
36 class multimapwithdefault : public std::multimap<Key, T, Compare> {
37 public:
38  typedef std::pair<const Key, T> value_type;
39  T& getWithDefault(const Key& k, const T& defaultValue) {
40  if (find(k) == this->end()) {
41  insert(value_type(k, defaultValue));
42  }
43  return (*(find(k))).second;
44  }
45 
46  T& operator[](const Key& k) {
47  if (this->find(k) == this->end()) {
48  this->insert(value_type(k, T()));
49  }
50  return (*(this->find(k))).second;
51  }
52  bool has(const Key& k, const T &val) const {
53  typename std::multimap<Key, T, Compare>::const_iterator start = this->lower_bound(k);
54  typename std::multimap<Key, T, Compare>::const_iterator end = this->upper_bound(k);
55  for (; start!=end; start++) {
56  if (start->second == val)
57  return true;
58  }
59  return false;
60  }
61 };
62 
64 #endif
std::pair< const Key, T > value_type
Definition: multimapwdef.h:38
#define SWORD_NAMESPACE_START
Definition: defs.h:39
T & getWithDefault(const Key &k, const T &defaultValue)
Definition: multimapwdef.h:39
T & operator[](const Key &k)
Definition: multimapwdef.h:46
bool has(const Key &k, const T &val) const
Definition: multimapwdef.h:52
#define SWORD_NAMESPACE_END
Definition: defs.h:40