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

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