MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Element_Sorter.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Element_Sorter.h DHS Specific data sorting element
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
28 #ifndef ELEMENT_SORTER_H
29 #define ELEMENT_SORTER_H
30 
31 #include "Element.h"
32 #include "EventWindow.h"
33 #include "ElementTable.h"
34 #include "Element_Res.h" /* For Element_Res::TYPE */
35 #include "Element_Data.h" /* For Element_Data::TYPE */
36 #include "ColorMap.h"
37 #include "itype.h"
38 #include "P1Atom.h"
39 #include "WindowScanner.h"
40 
41 namespace MFM
42 {
43 
44 #define SORTER_VERSION 1
45 
46  template <class CC>
47  class Element_Sorter : public Element<CC>
48  {
49  // Extract short names for parameter types
50  typedef typename CC::ATOM_TYPE T;
51  typedef typename CC::PARAM_CONFIG P;
52  enum { R = P::EVENT_WINDOW_RADIUS };
53 
54  public:
55  static Element_Sorter THE_INSTANCE;
56  static const u32 TYPE()
57  {
58  return THE_INSTANCE.GetType();
59  }
60 
61  static const u32 STATE_THRESHOLD_IDX = 0; // First bit in state
62  static const u32 STATE_THRESHOLD_LEN = 32;
63  static const u32 STATE_BITS = STATE_THRESHOLD_LEN;
64 
65  static const SPoint m_southeastSubWindow[4];
66  static const SPoint m_northeastSubWindow[4];
67  static const SPoint m_southwestSubWindow[4];
68  static const SPoint m_northwestSubWindow[4];
69 
70  inline u32 GetDataType() const
71  {
73  }
74 
75  inline u32 GetEmptyType() const
76  {
78  }
79 
80  Element_Sorter() :
81  Element<CC>(MFM_UUID_FOR("Sorter", SORTER_VERSION))
82  {
84  Element<CC>::SetName("Sorter");
85  }
86 
87  u32 GetThreshold(const T &atom, u32 badType) const
88  {
89  if (!Atom<CC>::IsType(atom,TYPE()))
90  {
91  return badType;
92  }
93  return atom.GetStateField(STATE_THRESHOLD_IDX,STATE_THRESHOLD_LEN);
94  }
95 
96  bool SetThreshold(T &atom, u32 value) const
97  {
98  if (!Atom<CC>::IsType(atom,TYPE()))
99  {
100  return false;
101  }
102  atom.SetStateField(STATE_THRESHOLD_IDX,STATE_THRESHOLD_LEN,value);
103  return true;
104  }
105 
106  virtual const T & GetDefaultAtom() const
107  {
108  static T defaultAtom(TYPE(),0,0,STATE_BITS);
109  return defaultAtom;
110  }
111 
112  virtual u32 PercentMovable(const T& you,
113  const T& me, const SPoint& offset) const
114  {
115  return 100;
116  }
117 
118  virtual u32 DefaultPhysicsColor() const
119  {
120  return 0xffff0000;
121  }
122 
123  virtual u32 DefaultLowlightColor() const
124  {
125  return 0xff7f0000;
126  }
127 
128  virtual const char* GetDescription() const
129  {
130  return "A sorting \"demon\", which sorts DATA atoms based on their values and "
131  "a value held within this Sorter as well. See the \"Demon Hoard Sort\" "
132  "algorithm for details.";
133  }
134 
135  virtual void AppendDescription(const T* atom, OString64& desc) const
136  {
137  u32 threshold = GetThreshold(*atom, 0);
138 
139  if(threshold)
140  {
141  desc.Printf("Threshold: %d", threshold);
142  }
143  else
144  {
145  desc.Printf("Threshold: INVALID");
146  }
147  }
148 
149  virtual u32 LocalPhysicsColor(const T & atom, u32 selector) const
150  {
151  switch (selector)
152  {
153  case 1:
154  return ColorMap_CubeHelixRev::THE_INSTANCE.
155  GetInterpolatedColor(GetThreshold(atom,0),DATA_MINVAL,DATA_MAXVAL,0xffff0000);
156 
157  default:
158  return Element<CC>::PhysicsColor();
159  }
160  }
161 
162  virtual void Behavior(EventWindow<CC>& window) const
163  {
164  Random & random = window.GetRandom();
165  SPoint reproducePt;
166  T self = window.GetCenterAtom();
167  WindowScanner<CC> scanner(window);
169  reproducePt) > 0)
170  {
171  window.SetRelativeAtom(reproducePt, self);
172  }
173 
174  bool movingUp = random.CreateBool();
175 
176  SPoint src, dst;
177  bool foundPts;
178 
179  for(s32 i = 0; i < 2; i++)
180  {
181  foundPts = false;
182  if(movingUp)
183  {
184  if(scanner.FindRandomAtomsInSubWindows(2,
185  &src, GetDataType(), m_southeastSubWindow, 4,
186  &dst, GetEmptyType(), m_northwestSubWindow, 4))
187  {
188  foundPts = true;
189  }
190  }
191  else
192  {
193  if(scanner.FindRandomAtomsInSubWindows(2,
194  &src, GetDataType(), m_northeastSubWindow, 4,
195  &dst, GetEmptyType(), m_southwestSubWindow, 4))
196  {
197  foundPts = true;
198  }
199  }
200 
201  if(foundPts)
202  {
203  u32 threshold = GetThreshold(window.GetCenterAtom(),0);
204  u32 datum = Element_Data<CC>::THE_INSTANCE.GetDatum(window.GetRelativeAtom(src),0);
205  bool cmp = (movingUp && (datum < threshold)) || (!movingUp && (datum > threshold));
206 
207  if(cmp)
208  {
209  SetThreshold(self, datum);
210  window.SetCenterAtom(self);
211  window.SwapAtoms(src, dst);
212  break;
213  }
214  }
215 
216  movingUp = !movingUp;
217 
218  }
219  this->Diffuse(window);
220  }
221  };
222 
223  template <class CC>
224  Element_Sorter<CC> Element_Sorter<CC>::THE_INSTANCE;
225 
226  template <class CC>
227  const SPoint Element_Sorter<CC>::m_southeastSubWindow[4] =
228  {
229  SPoint(1,1),SPoint(1,2),SPoint(2,1),SPoint(2,2)
230  };
231 
232  template <class CC>
233  const SPoint Element_Sorter<CC>::m_northeastSubWindow[4] =
234  {
235  SPoint(1,-1),SPoint(1,-2),SPoint(2,-1),SPoint(2,-2)
236  };
237 
238  template <class CC>
239  const SPoint Element_Sorter<CC>::m_northwestSubWindow[4] =
240  {
241  SPoint(-1,-1),SPoint(-1,-2),SPoint(-2,-1),SPoint(-2,-2)
242  };
243 
244  template <class CC>
245  const SPoint Element_Sorter<CC>::m_southwestSubWindow[4] =
246  {
247  SPoint(-1,1),SPoint(-1,2),SPoint(-2,1),SPoint(-2,2)
248  };
249 }
250 #endif /* ELEMENT_SORTER_H */
void Diffuse(EventWindow< CC > &window) const
Definition: Element.tcc:73
u32 GetType() const
Definition: Element.h:290
Definition: Element_Empty.h:41
Definition: Element_Data.h:47
Definition: Random.h:45
virtual void Behavior(EventWindow< CC > &window) const
Definition: Element_Sorter.h:162
virtual u32 DefaultLowlightColor() const
Definition: Element_Sorter.h:123
Definition: Atom.h:71
void SetCenterAtom(const T &atom)
Definition: EventWindow.h:220
void SetName(const char *name)
Definition: Element.h:209
void SetAtomicSymbol(const char *symbol)
Definition: Element.h:193
virtual u32 LocalPhysicsColor(const T &atom, u32 selector) const
Definition: Element_Sorter.h:149
virtual const char * GetDescription() const
Definition: Element_Sorter.h:128
Random & GetRandom()
Definition: EventWindow.h:122
const T & GetRelativeAtom(const SPoint &offset) const
Definition: EventWindow.tcc:26
const T & GetCenterAtom() const
Definition: EventWindow.h:209
virtual const T & GetDefaultAtom() const
Definition: Element_Sorter.h:106
virtual u32 DefaultPhysicsColor() const
Definition: Element_Sorter.h:118
u32 FindRandomInVonNeumann(const u32 type, SPoint &outPoint) const
Definition: WindowScanner.tcc:294
Definition: Element_Sorter.h:47
Definition: Element_Res.h:48
Definition: ElementTable.h:43
virtual u32 PhysicsColor() const
Definition: Element.h:398
virtual u32 PercentMovable(const T &you, const T &me, const SPoint &offset) const
Definition: Element_Sorter.h:112
Definition: WindowScanner.h:40
bool SetRelativeAtom(const SPoint &offset, const T &atom)
Definition: EventWindow.tcc:15
virtual void AppendDescription(const T *atom, OString64 &desc) const
Definition: Element_Sorter.h:135
bool CreateBool()
Definition: Random.h:86
Definition: Atom.h:43
void SwapAtoms(const SPoint &locA, const SPoint &locB)
Definition: EventWindow.tcc:40