1 2 /* Web Polygraph http://www.web-polygraph.org/ 3 * Copyright 2003-2011 The Measurement Factory 4 * Licensed under the Apache License, Version 2.0 */ 5 6 #ifndef POLYGRAPH__XSTD_FUTURE_H 7 #define POLYGRAPH__XSTD_FUTURE_H 8 9 #include "xstd/Array.h" 10 11 // "Future plan": a circular buffer; new entries are inserted 12 // at random positions relative to "now"; "now" or "next step" 13 // is at position "zero" 14 // for simplicity, assume that at most MAX_INT items are inserted 15 // over the life time of an object 16 template <class Item> 17 class Future { 18 public: theLog(aCapacity)19 Future(int aCapacity = 0): theLog(aCapacity), theOffset(0) {} 20 capacity(int aCapacity)21 void capacity(int aCapacity) { Assert(!theOffset); theLog.stretch(aCapacity); } capacity()22 int capacity() const { return theLog.capacity(); } 23 24 const Item &operator [](int off) const { return theLog[(theOffset + off) % theLog.capacity()]; } 25 Item &operator [](int off) { return theLog[(theOffset + off) % theLog.capacity()]; } shift(Item i)26 void shift(Item i) { theLog[theOffset++ % theLog.capacity()] = i; } 27 28 protected: 29 Array<Item> theLog; 30 int theOffset; // "now" or the start of a plan 31 }; 32 33 #endif 34