You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

snf_saccades.hpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // snf_saccades.hpp
  2. // Saccades engine adaptation for MessageSniffer
  3. // Copyright 2014 MicroNeil Research Corporation (www.microneil.com)
  4. // Licensed to ARM Research Labs for use in Message Sniffer.
  5. #ifndef _MN_SACCADES_ENGINE
  6. #define _MN_SACCADES_ENGINE
  7. #include <set>
  8. #include <vector>
  9. namespace SNFMulti {
  10. class saccades_engine;
  11. class saccade {
  12. friend class saccades_engine;
  13. private:
  14. const static unsigned int stretchSize = 8;
  15. const static unsigned int stretchMark = stretchSize / 2;
  16. const static unsigned int stretchMask = ((~0U) ^ (stretchSize - 1));
  17. unsigned int stretchLeft(unsigned int s) {
  18. s = (stretchMark > s) ? s : (s - (stretchMark));
  19. return (s & stretchMask);
  20. }
  21. unsigned int stretchRight(unsigned int f) {
  22. f = (start > f) ? start : f;
  23. return ((f + stretchSize + stretchMark) & stretchMask);
  24. }
  25. saccade() : start(0), finish(0) {}
  26. static saccade unstretched(unsigned int s, unsigned int f) {
  27. saccade u;
  28. u.start = s;
  29. u.finish = f;
  30. return u;
  31. }
  32. public:
  33. unsigned int start;
  34. unsigned int finish;
  35. saccade(unsigned int s, unsigned int f) :
  36. start(stretchLeft(s)),
  37. finish(stretchRight(f)) {}
  38. bool operator<(const saccade& r) const {
  39. if(start < r.start) return true;
  40. if(start > r.start) return false;
  41. return (finish < r.finish);
  42. }
  43. bool operator==(const saccade& r) const {
  44. return (
  45. start == r.start &&
  46. finish == r.finish
  47. );
  48. }
  49. };
  50. class saccade_marker {
  51. public:
  52. const saccade theSaccade;
  53. unsigned int theSlot;
  54. saccade_marker(saccade key, unsigned int slot) :
  55. theSaccade(key),
  56. theSlot(slot) {}
  57. bool operator<(const saccade_marker& r) const {
  58. return (theSaccade < r.theSaccade);
  59. }
  60. };
  61. struct saccade_engram {
  62. static const int NoneMore = -1;
  63. int nextFresh;
  64. int nextStale;
  65. saccade theSaccade;
  66. saccade_engram() :
  67. nextFresh(NoneMore),
  68. nextStale(NoneMore),
  69. theSaccade(0,0) {}
  70. saccade_engram(saccade s) :
  71. nextFresh(NoneMore),
  72. nextStale(NoneMore),
  73. theSaccade(s) {}
  74. };
  75. class saccades_engine {
  76. private:
  77. std::vector<saccade_engram> engrams;
  78. std::set<saccade_marker> markers;
  79. const unsigned int capacity;
  80. int mostFresh;
  81. int mostStale;
  82. void evoke(saccade s);
  83. void disconnectEngram(int theSlot);
  84. void connectFreshestEngram(int theSlot);
  85. void freshenEngram(int theSlot);
  86. void makeEngram(saccade s);
  87. void recycleEngram(saccade s);
  88. public:
  89. saccades_engine(unsigned int c) :
  90. capacity(c),
  91. mostFresh(saccade_engram::NoneMore),
  92. mostStale(saccade_engram::NoneMore) {
  93. engrams.reserve(capacity);
  94. }
  95. std::vector<saccade> recall();
  96. void learn(std::vector<saccade>& experiences);
  97. };
  98. } // namespace SNFMulti
  99. #endif