Empirical
World_iterator.h
Go to the documentation of this file.
1 
15 #ifndef EMP_EVO_WORLD_ITER_H
16 #define EMP_EVO_WORLD_ITER_H
17 
18 #include "../base/Ptr.h"
19 
20 namespace emp {
21 
22  template <typename WORLD>
24  private:
25  using world_t = WORLD;
26  using org_t = typename world_t::org_t;
28 
29  Ptr<world_t> world_ptr;
30  size_t pos;
31 
32  // WorldSize() is a shortcut to get the size of the pointed-to world object.
33  size_t WorldSize() { emp_assert(world_ptr); return world_ptr->size(); }
34 
35  // OrgPtr() is a shortcut to retrieve an organism from the pointed-to world object.
36  Ptr<org_t> OrgPtr() { emp_assert(world_ptr); return world_ptr->pop[pos]; }
37 
38  // The MakeValid() function moves an iterator to t next non-null position (or the end)
39  void MakeValid() {
40  while (pos < WorldSize() && OrgPtr() == nullptr) ++pos;
41  }
42 
43  public:
46  World_iterator(world_t * _w, size_t _p=0) : world_ptr(_w), pos(_p) { MakeValid(); }
47 
49  World_iterator(const World_iterator & _in) : world_ptr(_in.world_ptr), pos(_in.pos) { MakeValid(); }
50 
52  this_t & operator=(const World_iterator & _in) {
53  world_ptr = _in.world_ptr;
54  pos = _in.pos;
55  MakeValid();
56  return *this;
57  }
58 
61  ++pos;
62  MakeValid();
63  return *this;
64  }
65 
68  --pos;
69  while (pos < WorldSize() && OrgPtr() == nullptr) --pos;
70  return *this;
71  }
72 
74  bool operator==(const this_t& rhs) const { return pos == rhs.pos; }
75 
77  bool operator!=(const this_t& rhs) const { return pos != rhs.pos; }
78 
80  bool operator< (const this_t& rhs) const { return pos < rhs.pos; }
81 
83  bool operator<=(const this_t& rhs) const { return pos <= rhs.pos; }
84 
86  bool operator> (const this_t& rhs) const { return pos > rhs.pos; }
87 
89  bool operator>=(const this_t& rhs) const { return pos >= rhs.pos; }
90 
92  org_t & operator*() { MakeValid(); return *(OrgPtr()); }
93 
95  const org_t & operator*() const { MakeValid(); return *(OrgPtr()); }
96 
98  operator bool() const { MakeValid(); return pos < WorldSize(); }
99 
101  this_t begin() { return this_t(world_ptr, 0); }
102 
104  const this_t begin() const { return this_t(world_ptr, 0); }
105 
107  this_t end() { return this_t(world_ptr, WorldSize()); }
108 
110  const this_t end() const { return this_t(world_ptr, WorldSize()); }
111  };
112 
113 }
114 
115 #endif
this_t & operator--()
Backup iterator to the previos non-empty cell in the world.
Definition: World_iterator.h:67
bool operator!=(const this_t &rhs) const
Compare two iterators to determine if they point to different positions in the world.
Definition: World_iterator.h:77
const org_t & operator*() const
Return a const reference to the organism pointed to by this iterator.
Definition: World_iterator.h:95
World_iterator(const World_iterator &_in)
Create an iterator pointing to the same position as another iterator.
Definition: World_iterator.h:49
bool operator>(const this_t &rhs) const
Determine if this iterator points to a position in the world AFTER another iterator.
Definition: World_iterator.h:86
Definition: World_iterator.h:23
this_t & operator++()
Advance iterator to the next non-empty cell in the world.
Definition: World_iterator.h:60
org_t & operator*()
Return a reference to the organism pointed to by this iterator.
Definition: World_iterator.h:92
bool operator<(const this_t &rhs) const
Determine if this iterator points to a position in the world BEFORE another iterator.
Definition: World_iterator.h:80
World_iterator(world_t *_w, size_t _p=0)
Definition: World_iterator.h:46
bool operator>=(const this_t &rhs) const
Determine if this iterator points to a position in the world AFTER or the SAME AS another iterator...
Definition: World_iterator.h:89
this_t end()
Return an iterator pointing to just past the end of the world.
Definition: World_iterator.h:107
const this_t begin() const
Return a const iterator pointing to the first occupied cell in the world.
Definition: World_iterator.h:104
this_t begin()
Return an iterator pointing to the first occupied cell in the world.
Definition: World_iterator.h:101
bool operator<=(const this_t &rhs) const
Determine if this iterator points to a position in the world BEFORE or the SAME AS another iterator...
Definition: World_iterator.h:83
If we are in emscripten, make sure to include the header.
Definition: array.h:37
const this_t end() const
Return a const iterator pointing to just past the end of the world.
Definition: World_iterator.h:110
#define emp_assert(...)
Definition: assert.h:199
this_t & operator=(const World_iterator &_in)
Assign this iterator to the position of another iterator.
Definition: World_iterator.h:52
bool operator==(const this_t &rhs) const
Compare two iterators to determine if they point to identical positions in the world.
Definition: World_iterator.h:74