HElib  1.0
Implementing Homomorphic Encryption
 All Classes Files Functions Variables Friends Pages
IndexMap.h
Go to the documentation of this file.
1 /* Copyright (C) 2012,2013 IBM Corp.
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License along
13  * with this program; if not, write to the Free Software Foundation, Inc.,
14  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 #ifndef _IndexMap
17 #define _IndexMap
18 
23 #include "IndexSet.h"
24 #include <tr1/unordered_map>
25 #include <iostream>
26 #include <cassert>
27 #include "cloned_ptr.h"
28 
29 using namespace std;
30 
32 template < class T > class IndexMapInit {
33 public:
35  virtual void init(T&) = 0;
36 
38  virtual IndexMapInit<T> * clone() const = 0;
39  virtual ~IndexMapInit() {} // ensure that derived destructor is called
40 };
41 
42 
47 template < class T > class IndexMap {
48 
49  tr1::unordered_map<long, T> map;
50  IndexSet indexSet;
51  cloned_ptr< IndexMapInit<T> > init;
52 
53 public:
54 
56  IndexMap();
57 
66  explicit IndexMap(IndexMapInit<T> *_init) : init(_init) { }
67 
69  const IndexSet& getIndexSet() const { return indexSet; }
70 
73  T& operator[] (long j) {
74  assert(indexSet.contains(j));
75  return map[j];
76  }
77  const T& operator[] (long j) const {
78  assert(indexSet.contains(j));
79  // unordered_map does not support a const [] operator,
80  // so we have to artificially strip away the const-ness here
81  tr1::unordered_map<long, T> & map1 =
82  const_cast< tr1::unordered_map<long, T> & > (map);
83  return map1[j];
84  }
85 
89  void insert(long j) {
90  if (!indexSet.contains(j)) {
91  indexSet.insert(j);
92  if (!init.null()) init->init(map[j]);
93  }
94  }
95  void insert(const IndexSet& s) {
96  for (long i = s.first(); i <= s.last(); i = s.next(i))
97  insert(i);
98  }
99 
101  void remove(long j) { indexSet.remove(j); map.erase(j); }
102  void remove(const IndexSet& s) {
103  for (long i = s.first(); i <= s.last(); i = s.next(i))
104  map.erase(i);
105  indexSet.remove(s);
106  }
107 
108  void clear() {
109  map.clear();
110  indexSet.clear();
111  }
112 
113 
114 };
115 
117 template <class T>
118 bool operator==(const IndexMap<T>& map1, const IndexMap<T>& map2) {
119  if (map1.getIndexSet() != map2.getIndexSet()) return false;
120  const IndexSet& s = map1.getIndexSet();
121  for (long i = s.first(); i <= s.last(); i = s.next(i)) {
122  if (map1[i] == map2[i]) continue;
123  return false;
124  }
125  return true;
126 }
127 
128 template <class T>
129 bool operator!=(const IndexMap<T>& map1, const IndexMap<T>& map2) {
130  return !(map1 == map2);
131 }
132 
133 
134 
135 #endif