47 #include <boost/unordered_map.hpp>
48 #include <boost/smart_ptr.hpp>
49 #include <boost/iterator/transform_iterator.hpp>
50 #include <boost/iterator/filter_iterator.hpp>
51 #include <boost/function.hpp>
54 #include "AgentRequest.h"
56 #include "ValueLayer.h"
57 #include "Projection.h"
58 #include "RepastErrors.h"
67 struct SecondElement:
public std::unary_function<typename boost::unordered_map<AgentId, boost::shared_ptr<T> >::value_type, boost::shared_ptr<T> > {
68 boost::shared_ptr<T> operator()(
const typename boost::unordered_map<
AgentId, boost::shared_ptr<T> >::value_type& value)
const {
69 const boost::shared_ptr<T>& ptr = value.second;
86 typedef typename std::vector<Projection<T>*>::iterator ProjPtrIter;
87 typedef typename boost::unordered_map<AgentId, boost::shared_ptr<T>,
HashId> AgentMap;
89 typedef typename AgentMap::iterator AgentMapIterator;
90 typedef typename AgentMap::const_iterator AgentMapConstIterator;
93 std::map<std::string, BaseValueLayer*> valueLayers;
96 std::vector<Projection<T> *> projections;
100 typedef typename boost::transform_iterator<SecondElement<T> ,
typename AgentMap::const_iterator> const_iterator;
101 typedef typename boost::filter_iterator<IsAgentType<T> ,
typename Context<T>::const_iterator> const_bytype_iterator;
182 return const_iterator(agents.begin());
192 const_iterator
end()
const {
193 return const_iterator(agents.end());
231 return agents.size();
252 template<
typename ValueType,
typename Borders>
266 template<
typename ValueType,
typename Borders>
292 template<
typename filterStruct>
293 boost::filter_iterator<filterStruct, typename Context<T>::const_iterator>
filteredBegin(
const filterStruct& fStruct);
318 template<
typename filterStruct>
319 boost::filter_iterator<filterStruct, typename Context<T>::const_iterator>
filteredEnd(
const filterStruct& fStruct);
347 template<
typename filterStruct>
348 boost::filter_iterator<filterStruct, typename Context<T>::const_bytype_iterator>
byTypeFilteredBegin(
const int type,
const filterStruct& fStruct);
375 template<
typename filterStruct>
376 boost::filter_iterator<filterStruct, typename Context<T>::const_bytype_iterator>
byTypeFilteredEnd(
const int type,
const filterStruct& fStruct);
388 void selectAgents(std::set<T*>& selectedAgents,
bool remove =
false);
400 void selectAgents(std::vector<T*>& selectedAgents,
bool remove =
false);
417 void selectAgents(
int count, std::set<T*>& selectedAgents,
bool remove =
false);
434 void selectAgents(
int count, std::vector<T*>& selectedAgents,
bool remove =
false);
454 void selectAgents(std::set<T*>& selectedAgents,
int type,
bool remove =
false,
int popSize = -1);
474 void selectAgents(std::vector<T*>& selectedAgents,
int type,
bool remove =
false,
int popSize = -1);
499 void selectAgents(
int count, std::set<T*>& selectedAgents,
int type,
bool remove =
false,
int popSize = -1);
524 void selectAgents(
int count, std::vector<T*>& selectedAgents,
int type,
bool remove =
false,
int popSize = -1);
547 template<
typename filterStruct>
548 void selectAgents(std::set<T*>& selectedAgents, filterStruct& filter,
bool remove =
false,
int popSize = -1);
571 template<
typename filterStruct>
572 void selectAgents(std::vector<T*>& selectedAgents, filterStruct& filter,
bool remove =
false,
int popSize = -1);
600 template<
typename filterStruct>
601 void selectAgents(
int count, std::set<T*>& selectedAgents, filterStruct& filter,
bool remove =
false,
int popSize = -1);
629 template<
typename filterStruct>
630 void selectAgents(
int count, std::vector<T*>& selectedAgents, filterStruct& filter,
bool remove =
false,
int popSize = -1);
654 template<
typename filterStruct>
655 void selectAgents(std::set<T*>& selectedAgents,
int type, filterStruct& filter,
bool remove =
false,
int popSize = -1);
679 template<
typename filterStruct>
680 void selectAgents(std::vector<T*>& selectedAgents,
int type, filterStruct& filter,
bool remove =
false,
int popSize = -1);
709 template<
typename filterStruct>
710 void selectAgents(
int count, std::set<T*>& selectedAgents,
int type, filterStruct& filter,
bool remove =
false,
int popSize = -1);
739 template<
typename filterStruct>
740 void selectAgents(
int count, std::vector<T*>& selectedAgents,
int type, filterStruct& filter,
bool remove =
false,
int popSize = -1);
802 bool secondaryInfo =
false, std::set<AgentId>* secondaryIds = 0,
int destProc = -1);
810 void setProjectionInfo(std::map<std::string, std::vector<repast::ProjectionInfoPacket*> >& projInfo);
812 void cleanProjectionInfo(std::set<AgentId>& agentsToKeep);
819 Context<T>::Context() {
824 agents.erase(agents.begin(), agents.end());
825 for (ProjPtrIter iter = projections.begin(); iter != projections.end(); ++iter) {
831 for (std::map<std::string, BaseValueLayer*>::iterator iter = valueLayers.begin(); iter != valueLayers.end(); ++iter) {
840 std::string name = projection->
name();
841 for (ProjPtrIter iter = projections.begin(); iter != projections.end(); ++iter){
842 if(name.compare((*iter)->name()) == 0)
throw Repast_Error_9(name);
844 for (const_iterator iter = begin(); iter != end(); ++iter) {
845 projection->addAgent(*iter);
847 projections.push_back(projection);
852 for (ProjPtrIter iter = projections.begin(); iter != projections.end(); ++iter) {
854 if (proj->
name() == name)
862 AgentMapIterator iter = agents.find(
id);
863 if (iter == agents.end())
return 0;
864 return iter->second.get();
869 for (
int i = 0; i < count; i++) {
873 const_iterator iter = begin();
874 for (
int j = 0, n = rnd.
next(); j < n; iter++, j++)
876 T* agent = iter->get();
877 if (find(agents.begin(), agents.end(), agent) == agents.end()) {
878 agents.push_back(agent);
887 const AgentId&
id = agent->getId();
888 typename AgentMap::iterator findIter = agents.find(
id);
889 if (findIter != agents.end())
return &*(findIter->second);
891 boost::shared_ptr<T> ptr(agent);
894 for (ProjPtrIter iter = projections.begin(); iter != projections.end(); ++iter) {
904 removeAgent(agent->getId());
909 const AgentMapIterator iter = agents.find(
id);
910 if (iter != agents.end()) {
911 boost::shared_ptr<T>& ptr = iter->second;
912 for (ProjPtrIter pIter = projections.begin(); pIter != projections.end(); ++pIter) {
914 proj->removeAgent(ptr.get());
922 return agents.find(
id) != agents.end();
927 valueLayers[layer->
name()] = layer;
931 template<
typename ValueType,
typename Borders>
934 std::map<std::string, BaseValueLayer*>::iterator iter = valueLayers.find(valueLayerName);
935 if (iter == valueLayers.end())
941 template<
typename ValueType,
typename Borders>
944 std::map<std::string, BaseValueLayer*>::iterator iter = valueLayers.find(valueLayerName);
945 if (iter == valueLayers.end())
954 template<
typename filterStruct>
960 template<
typename filterStruct>
961 boost::filter_iterator<filterStruct, typename Context<T>::const_iterator>
Context<T>::filteredEnd(
const filterStruct& fStruct){
967 template<
typename filterStruct>
973 template<
typename filterStruct>
974 boost::filter_iterator<filterStruct, typename Context<T>::const_bytype_iterator>
Context<T>::byTypeFilteredEnd(
const int type,
const filterStruct& fStruct){
989 selectNElementsAtRandom(begin(), size(), size(), selectedAgents, remove);
994 selectNElementsInRandomOrder(begin(), size(), size(), selectedAgents, remove);
999 selectNElementsAtRandom(begin(), size(), count, selectedAgents, remove);
1002 template<
typename T>
1004 selectNElementsInRandomOrder(begin(), end(), count, selectedAgents, remove);
1007 template<
typename T>
1009 if(popSize <= -1) selectNElementsAtRandom(byTypeBegin(type), byTypeEnd(type), size(), selectedAgents, remove);
1010 else selectNElementsAtRandom(byTypeBegin(type), popSize, size(), selectedAgents, remove);
1013 template<
typename T>
1015 if(popSize <= -1) selectNElementsInRandomOrder(byTypeBegin(type), byTypeEnd(type), size(), selectedAgents, remove);
1016 else selectNElementsInRandomOrder(byTypeBegin(type), popSize, size(), selectedAgents, remove);
1019 template<
typename T>
1021 if(popSize <= -1) selectNElementsAtRandom(byTypeBegin(type), byTypeEnd(type), count, selectedAgents, remove);
1022 else selectNElementsAtRandom(byTypeBegin(type), popSize, count, selectedAgents, remove);
1025 template<
typename T>
1027 if(popSize <= -1) selectNElementsInRandomOrder(byTypeBegin(type), byTypeEnd(type), count, selectedAgents, remove);
1028 else selectNElementsInRandomOrder(byTypeBegin(type), popSize, count, selectedAgents, remove);
1031 template<
typename T>
1032 template<
typename filterStruct>
1034 if(popSize <= -1) selectNElementsAtRandom(filteredBegin<filterStruct>(filter), filteredEnd<filterStruct>(filter), size(), selectedAgents, remove);
1035 else selectNElementsAtRandom(filteredBegin<filterStruct>(filter), popSize, size(), selectedAgents, remove);
1038 template<
typename T>
1039 template<
typename filterStruct>
1041 if(popSize <= -1) selectNElementsInRandomOrder(filteredBegin(filter), filteredEnd(filter), size(), selectedAgents, remove);
1042 else selectNElementsInRandomOrder(filteredBegin(filter), popSize, size(), selectedAgents, remove);
1045 template<
typename T>
1046 template<
typename filterStruct>
1048 if(popSize <= -1) selectNElementsAtRandom(filteredBegin<filterStruct>(filter), filteredEnd<filterStruct>(filter), count, selectedAgents, remove);
1049 else selectNElementsAtRandom(filteredBegin<filterStruct>(filter), popSize, count, selectedAgents, remove);
1052 template<
typename T>
1053 template<
typename filterStruct>
1055 if(popSize <= -1) selectNElementsInRandomOrder(filteredBegin(filter), filteredEnd(filter), count, selectedAgents, remove);
1056 else selectNElementsInRandomOrder(filteredBegin(filter), popSize, count, selectedAgents, remove);
1059 template<
typename T>
1060 template<
typename filterStruct>
1062 if(popSize <= -1) selectNElementsAtRandom(byTypeFilteredBegin(type, filter), byTypeFilteredEnd(type, filter), size(), selectedAgents, remove);
1063 else selectNElementsAtRandom(byTypeFilteredBegin(type, filter), popSize, size(), selectedAgents, remove);
1066 template<
typename T>
1067 template<
typename filterStruct>
1069 if(popSize <= -1) selectNElementsInRandomOrder(byTypeFilteredBegin(type, filter), byTypeFilteredEnd(type, filter), size(), selectedAgents, remove);
1070 else selectNElementsInRandomOrder(byTypeFilteredBegin(type, filter), popSize, size(), selectedAgents, remove);
1073 template<
typename T>
1074 template<
typename filterStruct>
1076 if(popSize <= -1) selectNElementsAtRandom(byTypeFilteredBegin(type, filter), byTypeFilteredEnd(type, filter), count, selectedAgents, remove);
1077 else selectNElementsAtRandom(byTypeFilteredBegin(type, filter), popSize, count, selectedAgents, remove);
1080 template<
typename T>
1081 template<
typename filterStruct>
1083 if(popSize <= -1) selectNElementsInRandomOrder(byTypeFilteredBegin(type, filter), byTypeFilteredEnd(type, filter), count, selectedAgents, remove);
1084 else selectNElementsInRandomOrder(byTypeFilteredBegin(type, filter), popSize, count, selectedAgents, remove);
1090 template<
typename T>
1092 bool secondaryInfo, std::set<AgentId>* secondaryIds,
int destProc){
1094 for(
typename std::vector<
Projection<T> *>::iterator iter = projections.begin(), iterEnd = projections.end(); iter != iterEnd; iter++){
1095 std::string projName = (*iter)->name();
1096 map[projName] = std::vector<repast::ProjectionInfoPacket*>();
1097 (*iter)->getProjectionInfo(ids, map[projName], secondaryInfo, secondaryIds, destProc);
1101 template<
typename T>
1103 for(std::map<std::string, std::vector<repast::ProjectionInfoPacket*> >::iterator iter = projInfo.begin(), iterEnd = projInfo.end(); iter != iterEnd; iter++)
1104 getProjection(iter->first)->updateProjectionInfo(iter->second,
this);
1107 template<
typename T>
1109 for(
typename std::vector<
Projection<T> *>::iterator iter = projections.begin(), iterEnd = projections.end(); iter != iterEnd; iter++){
1110 (*iter)->cleanProjectionInfo(agentsToKeep);