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_HISTORY_H 7 #define POLYGRAPH__XSTD_HISTORY_H 8 9 #include "xstd/Array.h" 10 #include "xstd/gadgets.h" 11 12 // History log: a circular buffer; new entries overwrite old ones 13 // all external coordinates are relative to "now" or last entry 14 // for simplicity, assume that at most MAX_INT items are inserted 15 template <class Item> 16 class History { 17 public: theLog(aCapacity)18 History(int aCapacity = 0): theLog(aCapacity), theOffset(0) {} 19 capacity(int aCapacity)20 void capacity(int aCapacity) { Assert(!theOffset); theLog.stretch(aCapacity); } 21 depth()22 int depth() const { return Min(theOffset, theLog.capacity()); } at(int off)23 Item at(int off) const { return theLog[(theOffset - off) % theLog.capacity()]; } 24 Item operator [](int off) const { return at(off); } 25 insert(Item i)26 void insert(Item i) { theLog[++theOffset % theLog.capacity()] = i; } 27 28 protected: 29 Array<Item> theLog; 30 int theOffset; // beginning of a buffer 31 }; 32 33 #endif 34