celauto.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #ifndef __CELAUTO_H
  2. #define __CELAUTO_H
  3. #include <map>
  4. #include <string>
  5. #include <memory>
  6. /*
  7. *
  8. */
  9. namespace celauto {
  10. typedef std::pair<unsigned int, unsigned int> pt;
  11. struct ca_element {
  12. tag_t tag;
  13. unsigned int age;
  14. unsigned int age_add;
  15. ca_element() : age(0), age_add(0) {}
  16. ca_element(tag_t t, unsigned int a) : tag(t), age(a), age_add(0) {}
  17. };
  18. struct CaMap {
  19. typedef std::map<pt, ca_element> camap_t;
  20. camap_t camap;
  21. std::unordered_map< pt, std::map<tag_t, size_t> > count_of_n;
  22. bool need_init_count;
  23. CaMap() : need_init_count(true) {}
  24. void init() {
  25. camap.clear();
  26. count_of_n.clear();
  27. }
  28. void clear() {
  29. init();
  30. }
  31. void seed(neighbors::Neighbors& neigh, const pt& xy, tag_t tag) {
  32. camap_t::iterator i = camap.find(xy);
  33. if (i != camap.end())
  34. return;
  35. camap[xy] = ca_element(tag, 0);
  36. const CelAuto& rul = celautos().get(tag);
  37. for (const auto& xy_ : neigh(xy)) {
  38. count_of_n[neigh.mk(xy_, xy)][rul.tag]++;
  39. }
  40. }
  41. template <typename FUNC>
  42. void clear(neighbors::Neighbors& neigh, const pt& xy, FUNC funcoff) {
  43. camap_t::iterator i = camap.find(xy);
  44. if (i == camap.end())
  45. return;
  46. const CelAuto& rul = celautos().get(i->second.tag);
  47. for (const auto& xy_ : neigh(xy)) {
  48. auto nxy = neigh.mk(xy_, xy);
  49. size_t qq = count_of_n[nxy][rul.tag];
  50. if (qq == 0) {
  51. throw std::runtime_error("Sanity error in camap::clear");
  52. }
  53. count_of_n[nxy][rul.tag]--;
  54. }
  55. funcoff(xy.first, xy.second, rul);
  56. camap.erase(i);
  57. }
  58. template <typename FUNC1, typename FUNC2, typename FUNC3>
  59. inline void step(neighbors::Neighbors& neigh, FUNC1 cell_good, FUNC2 funcon, FUNC3 funcoff,
  60. size_t MAX) {
  61. /// ///
  62. if (need_init_count) {
  63. for (const auto& i : camap) {
  64. const pt& xy = i.first;
  65. tag_t tag = i.second.tag;
  66. for (const auto& xy_ : neigh(xy)) {
  67. count_of_n[neigh.mk(xy_, xy)][tag]++;
  68. }
  69. }
  70. need_init_count = false;
  71. }
  72. /// ///
  73. std::map<pt, tag_t> for_remove;
  74. std::map<pt, tag_t> for_add;
  75. std::set<pt> no_more_count_of_n;
  76. for (camap_t::iterator i = camap.begin(); i != camap.end(); ++i) {
  77. const pt& xy = i->first;
  78. const CelAuto& rul = celautos().get(i->second.tag);
  79. const unsigned int& age = i->second.age;
  80. unsigned int& age_add = i->second.age_add;
  81. age_add = 0;
  82. // Check if we are dead.
  83. if (age > 0) {
  84. if (age < rul.age - 1) {
  85. age_add = 1;
  86. } else {
  87. for_remove[xy] = rul.tag;
  88. }
  89. } else {
  90. // Check if we survive
  91. unsigned int n = count_of_n[xy][rul.tag];
  92. if ((rul.survive & (1 << n)) == 0) {
  93. age_add = 1;
  94. }
  95. }
  96. }
  97. size_t n_cells = camap.size() - for_remove.size();
  98. for (const auto& i : count_of_n) {
  99. const pt& xy = i.first;
  100. if (camap.count(xy) != 0)
  101. continue;
  102. bool need_delete = true;
  103. for (const auto& j : i.second) {
  104. size_t n = j.second;
  105. if (n == 0)
  106. continue;
  107. need_delete = false;
  108. const CelAuto& rul = celautos().get(j.first);
  109. if ((rul.born & (1 << n)) != 0 &&
  110. cell_good(xy.first, xy.second, rul) &&
  111. n_cells + for_add.size() < MAX) {
  112. for_add[xy] = rul.tag;
  113. break;
  114. }
  115. }
  116. if (need_delete) {
  117. no_more_count_of_n.insert(xy);
  118. }
  119. }
  120. for (const auto& i : no_more_count_of_n) {
  121. count_of_n.erase(i);
  122. }
  123. for (const auto& i : for_add) {
  124. const pt& xy = i.first;
  125. const CelAuto& rul = celautos().get(i.second);
  126. camap[xy] = ca_element(rul.tag, 0);
  127. for (const auto& xy_ : neigh(xy)) {
  128. count_of_n[neigh.mk(xy_, xy)][rul.tag]++;
  129. }
  130. funcon(xy.first, xy.second, rul);
  131. }
  132. // Leave remains of dead cells.
  133. for (const auto& i : for_remove) {
  134. const pt& xy = i.first;
  135. const CelAuto& rul = celautos().get(i.second);
  136. camap.erase(xy);
  137. funcoff(xy.first, xy.second, rul);
  138. }
  139. for (camap_t::iterator i = camap.begin(); i != camap.end(); ++i) {
  140. const pt& xy = i->first;
  141. ca_element& cae = i->second;
  142. if (cae.age == 0 && cae.age_add > 0) {
  143. for (const auto& xy_ : neigh(xy)) {
  144. auto nxy = neigh.mk(xy_, xy);
  145. size_t qq = count_of_n[nxy][cae.tag];
  146. if (qq == 0) {
  147. throw std::runtime_error("Sanity error in camap");
  148. }
  149. count_of_n[nxy][cae.tag]--;
  150. }
  151. }
  152. cae.age += cae.age_add;
  153. }
  154. }
  155. };
  156. }
  157. namespace serialize {
  158. template <>
  159. struct reader<celauto::CaMap> {
  160. void read(Source& s, celauto::CaMap& t) {
  161. size_t sz;
  162. serialize::read(s, sz);
  163. for (size_t i = 0; i < sz; ++i) {
  164. celauto::pt key;
  165. celauto::ca_element ca;
  166. serialize::read(s, key);
  167. serialize::read(s, ca.tag);
  168. serialize::read(s, ca.age);
  169. serialize::read(s, ca.age_add);
  170. t.camap[key] = ca;
  171. }
  172. t.need_init_count = true;
  173. }
  174. };
  175. template <>
  176. struct writer<celauto::CaMap> {
  177. void write(Sink& s, const celauto::CaMap& m) {
  178. serialize::write(s, m.camap.size());
  179. for (const auto& t : m.camap) {
  180. serialize::write(s, t.first);
  181. serialize::write(s, t.second.tag);
  182. serialize::write(s, t.second.age);
  183. serialize::write(s, t.second.age_add);
  184. }
  185. }
  186. };
  187. }
  188. #endif