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.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // snf_saccades.cpp
  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. #include <set>
  6. #include <vector>
  7. #include "SNFMulti/snf_saccades.hpp"
  8. using namespace std;
  9. namespace SNFMulti {
  10. bool doesOverlap(unsigned int workingStart, unsigned int testStart, unsigned int workingFinish) {
  11. return(
  12. testStart >= workingStart &&
  13. testStart <= workingFinish
  14. );
  15. }
  16. vector<saccade> saccades_engine::recall() {
  17. vector<saccade> recollection;
  18. int markersSize = markers.size();
  19. if(0 < markersSize) {
  20. set<saccade_marker>::iterator i;
  21. recollection.reserve(markers.size());
  22. bool isFirstPass = true;
  23. unsigned int workingStart = 0;
  24. unsigned int workingFinish = 0;
  25. for(i = markers.begin(); i != markers.end(); i++) {
  26. const saccade& theSaccade = (*i).theSaccade;
  27. if(isFirstPass) {
  28. workingStart = theSaccade.start;
  29. workingFinish = theSaccade.finish;
  30. isFirstPass = false;
  31. }
  32. else {
  33. if(doesOverlap(workingStart, theSaccade.start, workingFinish)) {
  34. workingFinish = theSaccade.finish;
  35. }
  36. else {
  37. recollection.push_back(saccade::unstretched(workingStart, workingFinish));
  38. workingStart = theSaccade.start;
  39. workingFinish = theSaccade.finish;
  40. }
  41. }
  42. }
  43. recollection.push_back(saccade::unstretched(workingStart, workingFinish));
  44. }
  45. return recollection;
  46. }
  47. void saccades_engine::disconnectEngram(int theSlot) {
  48. saccade_engram& current = engrams.at(theSlot);
  49. saccade_engram& above = engrams.at(current.nextFresh);
  50. above.nextStale = current.nextStale;
  51. if(mostStale == theSlot) {
  52. mostStale = current.nextFresh;
  53. }
  54. else {
  55. saccade_engram& below = engrams.at(current.nextStale);
  56. below.nextFresh = current.nextFresh;
  57. }
  58. current.nextFresh = saccade_engram::NoneMore;
  59. current.nextStale = saccade_engram::NoneMore;
  60. }
  61. void saccades_engine::connectFreshestEngram(int theSlot) {
  62. engrams.at(theSlot).nextStale = mostFresh;
  63. if(mostFresh != saccade_engram::NoneMore) {
  64. engrams.at(mostFresh).nextFresh = theSlot;
  65. }
  66. mostFresh = theSlot;
  67. if(mostStale == saccade_engram::NoneMore) {
  68. mostStale = theSlot;
  69. }
  70. }
  71. void saccades_engine::freshenEngram(int theSlot) {
  72. if(mostFresh == theSlot) return;
  73. disconnectEngram(theSlot);
  74. connectFreshestEngram(theSlot);
  75. }
  76. void saccades_engine::makeEngram(saccade s) {
  77. int newSlot = engrams.size();
  78. engrams.push_back(saccade_engram(s));
  79. connectFreshestEngram(newSlot);
  80. markers.insert(saccade_marker(s, newSlot));
  81. }
  82. void saccades_engine::recycleEngram(saccade s) {
  83. int recycleSlot = mostStale;
  84. saccade_marker dropMarker(engrams.at(recycleSlot).theSaccade, recycleSlot);
  85. markers.erase(dropMarker);
  86. engrams.at(recycleSlot).theSaccade = s;
  87. saccade_marker insertMarker(s, recycleSlot);
  88. markers.insert(insertMarker);
  89. freshenEngram(recycleSlot);
  90. }
  91. void saccades_engine::evoke(saccade s) {
  92. bool stillGrowing = (capacity > engrams.size());
  93. set<saccade_marker>::iterator i;
  94. saccade_marker testMarker(s,0);
  95. i = markers.find(testMarker);
  96. bool isRemembered = (i != markers.end());
  97. if(isRemembered) {
  98. freshenEngram((*i).theSlot);
  99. }
  100. else {
  101. if(stillGrowing) {
  102. makeEngram(s);
  103. }
  104. else {
  105. recycleEngram(s);
  106. }
  107. }
  108. }
  109. void saccades_engine::learn(vector<saccade>& experiences) {
  110. for(unsigned int i = 0; i < experiences.size(); i++) {
  111. evoke(experiences[i]);
  112. }
  113. }
  114. }