libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
peano_curve.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #pragma once
20 
21 #include "ttypes.hpp"
22 
23 #include <cassert>
24 #include <list>
25 
26 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE {
27 
28  class PeanoCurve {
29  private:
30  enum elements {
31  green_right_curve=0,
32  red_right_curve,
33  green_left_curve,
34  red_left_curve,
35  green_straight,
36  red_straight
37  };
38 
39  public:
40  enum directions {
41  north=0,
44  west
45  };
46 
47  struct curve_element {
48  elements elem;
50 
51  curve_element(const elements e,const directions d) noexcept(true)
52  : elem(e),dir(d) {
53  }
54  };
55 
57 
58  PeanoCurve(void) {
59  ZeroCurve();
60  }
61  explicit PeanoCurve(const curve &c,const unsigned long d=0)
62  : peano(c),depth(d) {
63  }
64  ~PeanoCurve(void) {
65  }
66 
67  void Create(const unsigned long d) {
68  if (depth>d) {
69  ZeroCurve();
70  }
71  if (depth<d) {
72  for (unsigned long i=depth;i<d;++i) {
73  curve::iterator element(peano.end());
74  do {
75  ReplaceElement(--element);
76  } while (element!=peano.begin());
77  }
78  depth=d;
79  }
80  assert(!peano.empty());
81  }
82 
83  unsigned long Depth(void) const noexcept(true) {
84  return depth;
85  }
86 
87  const curve &Curve(void) const noexcept(true) {
88  return peano;
89  }
90 
91  private:
92  typedef curve_element replace_element[4];
93  typedef replace_element dir_replacements[6];
94 
95  static const dir_replacements replacements[];
96  curve peano;
97  unsigned long depth;
98 
99  void ZeroCurve(void) {
100  peano.clear();
101  peano.push_back(curve_element(red_right_curve,east));
102  peano.push_back(curve_element(green_right_curve,south));
103  peano.push_back(curve_element(red_right_curve,west));
104  peano.push_back(curve_element(green_right_curve,north));
105  depth=0;
106  }
107 
108  void ReplaceElement(curve::iterator &element) {
109  const dir_replacements &replacement_curves=replacements[element->dir];
110  const replace_element &replacement_curve=replacement_curves[element->elem];
111  *element=replacement_curve[3];
112  element=peano.insert(element,replacement_curve[2]);
113  element=peano.insert(element,replacement_curve[1]);
114  element=peano.insert(element,replacement_curve[0]);
115  }
116 
117  };
118 
119 } }