RepastHPC  2.3.1
MultipleOccupancy.h
1 /*
2  * Repast for High Performance Computing (Repast HPC)
3  *
4  * Copyright (c) 2010 Argonne National Laboratory
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the Argonne National Laboratory nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * MultipleOccupancy.h
36  *
37  * Created on: Aug 11, 2010
38  * Author: nick
39  */
40 
41 #ifndef MULTIPLEOCCUPANCY_H_
42 #define MULTIPLEOCCUPANCY_H_
43 
44 #include <boost/unordered_map.hpp>
45 #include <boost/shared_ptr.hpp>
46 
47 #include "Point.h"
48 
49 namespace repast {
50 
51 
55 template<typename T>
56 struct ExtractPtrs: public std::unary_function<
57  typename boost::unordered_map<AgentId, boost::shared_ptr<T> >::value_type, T*> {
58  T* operator()(typename boost::unordered_map<AgentId, boost::shared_ptr<T> >::value_type& val) {
59  return val.second.get();
60  }
61 };
62 
71 template<typename T, typename GPType>
73 
74 private:
75  typedef typename boost::unordered_map<AgentId, boost::shared_ptr<T>, HashId> ValueType;
76  typedef typename ValueType::iterator ValueTypeIter;
77  typedef typename boost::unordered_map<Point<GPType> , ValueType*, HashGridPoint<GPType> > LocationMap;
78 
79  typedef typename LocationMap::iterator LocationMapIter;
80  typedef typename LocationMap::const_iterator LocationMapConstIter;
81 
82  LocationMap locations;
83 
84  ValueType* doGet(const Point<GPType>& location) const;
85 
86 public:
87 
88  virtual ~MultipleOccupancy();
89 
97  T* get(const Point<GPType>& location) const;
98 
105  void getAll(const Point<GPType>& location, std::vector<T*>& out) const;
106 
113  bool put(boost::shared_ptr<T>& agent, const Point<GPType>& location);
114 
121  void remove(boost::shared_ptr<T>& agent, const Point<GPType>& location);
122 
123 };
124 
125 template<typename T, typename GPType>
127  for (LocationMapIter iter = locations.begin(); iter != locations.end(); ++iter) {
128  delete iter->second;
129  }
130 }
131 
132 template<typename T, typename GPType>
133 typename MultipleOccupancy<T, GPType>::ValueType* MultipleOccupancy<T, GPType>::doGet(const Point<GPType>& location) const {
134  LocationMapConstIter iter = locations.find(location);
135  if (iter == locations.end())
136  return NULL;
137  return iter->second;
138 }
139 
140 template<typename T, typename GPType>
142  ValueType* ptrs = doGet(location);
143  if (ptrs == NULL)
144  return NULL;
145  return ptrs->begin()->second.get();
146 }
147 
148 template<typename T, typename GPType>
149 void MultipleOccupancy<T, GPType>::getAll(const Point<GPType>& location, std::vector<T*>& out) const {
150  ValueType* ptrs = doGet(location);
151  if (ptrs != NULL) {
152  int index = out.size();
153  out.resize(out.size() + ptrs->size(), 0);
154  ExtractPtrs<T> func;
155  std::transform(ptrs->begin(), ptrs->end(), out.begin() + index, func);
156  }
157 }
158 
159 template<typename T, typename GPType>
160 bool MultipleOccupancy<T, GPType>::put(boost::shared_ptr<T>& agent, const Point<GPType>& location) {
161  LocationMapIter iter = locations.find(location);
162  ValueType* vec;
163  if (iter == locations.end()) {
164  vec = new ValueType();
165  locations[location] = vec;
166  } else {
167  vec = iter->second;
168  }
169  vec->insert(std::make_pair(agent->getId(), agent));
170 
171  return true;
172 }
173 
174 template<typename T, typename GPType>
175 void MultipleOccupancy<T, GPType>::remove(boost::shared_ptr<T>& agent, const Point<GPType>& location) {
176  LocationMapIter iter = locations.find(location);
177  if (iter != locations.end()) {
178  ValueType* vec = iter->second;
179  ValueTypeIter agentIter = vec->find(agent->getId());
180 
181  if (agentIter != vec->end()) {
182  vec->erase(agentIter);
183  if (vec->size() == 0) {
184  delete vec;
185  locations.erase(iter);
186  }
187 
188  }
189 
190  }
191 }
192 
193 }
194 
195 #endif /* MULTIPLEOCCUPANCY_H_ */
repast::ExtractPtrs
Unary function that allows retrieving the occupants of locations.
Definition: MultipleOccupancy.h:56
repast::MultipleOccupancy::getAll
void getAll(const Point< GPType > &location, std::vector< T * > &out) const
Gets all the items found at the specified location.
Definition: MultipleOccupancy.h:149
repast::AgentId
Agent identity information.
Definition: AgentId.h:60
repast::MultipleOccupancy::get
T * get(const Point< GPType > &location) const
Gets the first object found at the specified location.
Definition: MultipleOccupancy.h:141
repast::MultipleOccupancy::remove
void remove(boost::shared_ptr< T > &agent, const Point< GPType > &location)
Removes the specified item from the specified location.
Definition: MultipleOccupancy.h:175
repast::HashGridPoint
Class that allows retrieval of hash value for Point objects.
Definition: Point.h:67
repast::MultipleOccupancy
Multiple Occupancy cell accessor for accessing the occupants of locations in a Grid.
Definition: MultipleOccupancy.h:72
repast::MultipleOccupancy::put
bool put(boost::shared_ptr< T > &agent, const Point< GPType > &location)
Puts the specified item at the specified location.
Definition: MultipleOccupancy.h:160
repast::HashId
operator() implementation that returns the hashcode of an AgentId.
Definition: AgentId.h:185
repast::Point< GPType >