RepastHPC  2.3.1
agent_set_functions.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  * agent_set_functions.h
36  *
37  * Created on: Aug 12, 2010
38  * Author: nick
39  */
40 
41 #ifndef AGENT_SET_FUNCTIONS_H_
42 #define AGENT_SET_FUNCTIONS_H_
43 
44 #include <boost/iterator/filter_iterator.hpp>
45 #include <vector>
46 
47 #include "RelogoAgent.h"
48 #include "AgentSet.h"
49 
50 namespace repast {
51 namespace relogo {
52 
56 template<typename TargetType>
57 struct Caster2: public std::unary_function<RelogoAgent, TargetType*> {
58 
59  TargetType* operator()(const RelogoAgent* agent) const {
60  RelogoAgent* agentRef = const_cast<RelogoAgent*> (agent);
61  return static_cast<TargetType*> (agentRef);
62  }
63 };
64 
69 template<typename T>
71  IsAgentType<T> isAgentType;
72  boost::unordered_set<AgentId, HashId > set;
73 
74  IsAgentTypeNoDup(int typeId) :
75  isAgentType(typeId) {
76  }
77 
78  bool operator()(const T* agent) {
79  int count = set.count(agent->getId());
80  if (count == 0) {
81  set.insert(agent->getId());
82  return isAgentType(agent);
83  }
84  return false;
85  }
86 };
87 
88 
89 
90 typedef boost::filter_iterator<IsAgentType<RelogoAgent> , std::vector<RelogoAgent*>::const_iterator>
91  const_bytype_iterator;
92 typedef boost::filter_iterator<IsAgentTypeNoDup<RelogoAgent> , std::vector<RelogoAgent*>::const_iterator>
93  const_bytype_nodup_iterator;
94 typedef boost::filter_iterator<IsAgentType<RelogoAgent> , std::vector<RelogoAgent*>::iterator> bytype_iterator;
95 
104 template<typename AgentType>
105 void filterVecToSet(std::vector<RelogoAgent*>& in, AgentSet<AgentType>& out, int typeId) {
106  //const_bytype_iterator typeFilterBegin(IsAgentType<RelogoAgent> (typeId), in.begin(), in.end());
107  //const_bytype_iterator typeFilterEnd(IsAgentType<RelogoAgent> (typeId), in.end(), in.end());
108  //boost::transform_iterator<Caster2<AgentType> , const_bytype_iterator> begin(typeFilterBegin);
109  //boost::transform_iterator<Caster2<AgentType> , const_bytype_iterator> end(typeFilterEnd);
110  for (int i = 0, n = in.size(); i < n; ++i) {
111  RelogoAgent* agent = in[i];
112  if (agent->getId().agentType() == typeId) {
113  out.add(static_cast<AgentType*> (const_cast<RelogoAgent*>(agent)));
114  }
115  }
116  //out.addAll(begin, end);
117 }
118 
119 
129 template<typename AgentType>
130 void filterVecToSetNoDuplicates(std::vector<RelogoAgent*>& in, AgentSet<AgentType>& out, int typeId) {
131  const_bytype_nodup_iterator typeFilterBegin(IsAgentTypeNoDup<RelogoAgent> (typeId), in.begin(), in.end());
132  const_bytype_nodup_iterator typeFilterEnd(IsAgentTypeNoDup<RelogoAgent> (typeId), in.end(), in.end());
133  boost::transform_iterator<Caster2<AgentType> , const_bytype_nodup_iterator> begin(typeFilterBegin);
134  boost::transform_iterator<Caster2<AgentType> , const_bytype_nodup_iterator> end(typeFilterEnd);
135  out.addAll(begin, end);
136 }
137 
138 }
139 }
140 
141 #endif /* AGENT_SET_FUNCTIONS_H_ */
repast::relogo::RelogoAgent
Base agent for Relogo.
Definition: RelogoAgent.h:60
repast::relogo::AgentSet
Specialized indexable collection class for agents.
Definition: AgentSet.h:82
repast::IsAgentType
Struct that allows filtering by Agent Type.
Definition: AgentId.h:232
repast::relogo::RelogoAgent::getId
virtual repast::AgentId & getId()
Gets the id of this RelogoAgent.
Definition: RelogoAgent.h:93
repast::AgentId::agentType
int agentType() const
Gets the agent type component of this AgentId.
Definition: AgentId.h:124
repast::relogo::IsAgentTypeNoDup
Used to filter by agent type but ensure that only the first encountered instance of agent is consider...
Definition: agent_set_functions.h:70
repast::relogo::AgentSet::add
void add(T *agent)
Adds an agent to this AgentSet.
Definition: AgentSet.h:406
repast::relogo::Caster2
Unary function used in the transform_iterator that allows.
Definition: agent_set_functions.h:57