Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

snf_saccades.cpp 3.9KB

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