MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Element_Consumer.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Element_Consumer.h Basic data consuming 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_CONSUMER_H
29 #define ELEMENT_CONSUMER_H
30 
31 #include "Element.h"
32 #include "EventWindow.h"
33 #include "ElementTable.h"
34 #include "Element_Data.h"
35 #include "Element_Empty.h"
36 #include "Element_Emitter.h" /* For DATA_MAXVAL, DATA_MINVAL */
38 #include "itype.h"
39 #include "Util.h"
40 #include "Tile.h"
41 
42 namespace MFM
43 {
44  template <class CC>
46  {
47  // Extract short names for parameter types
48  typedef typename CC::ATOM_TYPE T;
49  typedef typename CC::PARAM_CONFIG P;
50  enum { R = P::EVENT_WINDOW_RADIUS };
51  enum { W = P::TILE_WIDTH };
52 
53  public:
54  // Element Data Slot names
55  enum {
56  DATUMS_CONSUMED_SLOT,
57  TOTAL_BUCKET_ERROR_SLOT,
58  DATA_SLOT_COUNT
59  };
60 
61  static Element_Consumer THE_INSTANCE;
62 
63  virtual T BuildDefaultAtom() const {
64  T defaultAtom(this->GetType(), 0, 0, AbstractElement_Reprovert<CC>::STATE_BITS);
65  this->SetGap(defaultAtom,1); // Pack consumers adjacent
66  return defaultAtom;
67  }
68 
70  : AbstractElement_Reprovert<CC>(MFM_UUID_FOR("Consumer",1))
71  {
73  Element<CC>::SetName("Consumer");
74  }
75 
76  u64 GetAndResetDatumsConsumed(Tile<CC> & t) const
77  {
78  ElementTable<CC> & et = t.GetElementTable();
79  u64 * datap = et.GetDataIfRegistered(this->GetType(), DATA_SLOT_COUNT);
80  if (!datap)
81  return 0;
82 
83  u64 ret = datap[DATUMS_CONSUMED_SLOT];
84  datap[DATUMS_CONSUMED_SLOT] = 0;
85  return ret;
86  }
87 
88  u64 GetAndResetBucketError(Tile<CC> & t) const
89  {
90  ElementTable<CC> & et = t.GetElementTable();
91  u64 * datap = et.GetDataIfRegistered(this->GetType(), DATA_SLOT_COUNT);
92  if (!datap)
93  return 0;
94 
95  u64 ret = datap[TOTAL_BUCKET_ERROR_SLOT];
96  datap[TOTAL_BUCKET_ERROR_SLOT] = 0;
97  return ret;
98  }
99 
100  virtual u32 PercentMovable(const T& you,
101  const T& me, const SPoint& offset) const
102  {
103  return 0;
104  }
105 
106  virtual u32 DefaultPhysicsColor() const
107  {
108  return 0xff202030;
109  }
110 
111  virtual u32 DefaultLowlightColor() const
112  {
113  return 0xff101018;
114  }
115 
116  virtual const char* GetDescription() const
117  {
118  return "This vertically-reproducing Element consumes DATA atoms and holds "
119  "information on its position and the DATA consumed.";
120  }
121 
122  virtual void Behavior(EventWindow<CC>& window) const
123  {
124  // Random & random = window.GetRandom();
125  this->ReproduceVertically(window);
126 
127  T self = window.GetCenterAtom();
128 
129  // Find nearest-on-right consumable, if any
130  for (u32 i = 1; i < R; ++i)
131  {
132  SPoint consPt(i,0);
133  if(window.GetRelativeAtom(consPt).GetType() == Element_Data<CC>::TYPE())
134  {
135  // Use the expanded info maintained by reprovert to
136  // dynamically compute the bucket size. That info can be
137  // temporarily disrupted e.g. by DReg, so going this route
138  // does increase the overall sorting error a bit. But it's so
139  // damn robust. Turn off an output tile and it starts sorting
140 
141  u32 above = this->GetAbove(self,0);
142  u32 below = this->GetBelow(self,0);
143  u32 range = above + below + 1;
144  const u32 bucketSize = DATA_MAXVAL / range;
145 
146  u32 val = Element_Data<CC>::THE_INSTANCE.GetDatum(window.GetRelativeAtom(consPt), 0);
147 
148  u32 minBucketVal = bucketSize * above;
149  u32 maxBucketVal = bucketSize * (above + 1);
150  u32 midpoint = (maxBucketVal+minBucketVal)/2;
151 
152  u32 diff;
153  if (val < midpoint) diff = midpoint - val;
154  else diff = val - midpoint;
155 
156  u32 bucketsOff = diff / bucketSize;
157 
158  Tile<CC> & tile = window.GetTile();
159  ElementTable<CC> & et = tile.GetElementTable();
160 
161  u64 * datap = et.GetDataAndRegister(this->GetType(), DATA_SLOT_COUNT);
162  ++datap[DATUMS_CONSUMED_SLOT]; // Count datums consumed
163  datap[TOTAL_BUCKET_ERROR_SLOT] += bucketsOff; // Count total bucket error
164 
165  /*
166  printf("[%d:%d:%d/bs %d>%d<%d=%d]Export!: %d %ld %ld %f\n",
167  below,
168  range,
169  above,
170  bucketSize,
171  minBucketVal,
172  maxBucketVal,
173  bucketsOff,
174  val,
175  datap[DATUMS_CONSUMED_SLOT], datap[TOTAL_BUCKET_ERROR_SLOT],
176  ((double)datap[TOTAL_BUCKET_ERROR_SLOT])/datap[DATUMS_CONSUMED_SLOT]);
177  */
179  break;
180  }
181  }
182  }
183  };
184 
185  template <class CC>
186  Element_Consumer<CC> Element_Consumer<CC>::THE_INSTANCE;
187 }
188 
189 #endif /* ELEMENT_CONSUMER_H */
u32 GetType() const
Definition: Element.h:290
Definition: Element_Empty.h:41
Definition: Element_Data.h:47
virtual u32 DefaultPhysicsColor() const
Definition: Element_Consumer.h:106
virtual u32 DefaultLowlightColor() const
Definition: Element_Consumer.h:111
Tile< CC > & GetTile()
Definition: EventWindow.h:132
void SetName(const char *name)
Definition: Element.h:209
void SetAtomicSymbol(const char *symbol)
Definition: Element.h:193
const T & GetRelativeAtom(const SPoint &offset) const
Definition: EventWindow.tcc:26
Definition: Element_Consumer.h:45
const T & GetCenterAtom() const
Definition: EventWindow.h:209
virtual void Behavior(EventWindow< CC > &window) const
Definition: Element_Consumer.h:122
virtual const T & GetDefaultAtom() const
Definition: Element.h:382
Definition: AbstractElement_Reprovert.h:42
virtual u32 PercentMovable(const T &you, const T &me, const SPoint &offset) const
Definition: Element_Consumer.h:100
virtual T BuildDefaultAtom() const
Definition: Element_Consumer.h:63
virtual const char * GetDescription() const
Definition: Element_Consumer.h:116
Definition: EventWindow.h:41
Definition: ElementTable.h:46
ElementTable< CC > & GetElementTable()
Definition: Tile.h:599
Definition: ElementTable.h:43
bool SetRelativeAtom(const SPoint &offset, const T &atom)
Definition: EventWindow.tcc:15