LRU.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. class LRUItem{
  2. constructor(node){
  3. this.previous = null;
  4. this.next = null;
  5. this.node = node;
  6. }
  7. }
  8. /**
  9. *
  10. * @class A doubly-linked-list of the least recently used elements.
  11. */
  12. class LRU{
  13. constructor(){ //类似链表存储
  14. // the least recently used item
  15. this.first = null;
  16. // the most recently used item
  17. this.last = null;
  18. // a list of all items in the lru list
  19. this.items = {}; //按node的id存储。(id为0的就是root,name='r')
  20. this.elements = 0;
  21. this.numPoints = 0;
  22. }
  23. size(){
  24. return this.elements;
  25. }
  26. contains(node){
  27. return this.items[node.id] == null;
  28. }
  29. touch(node){//链接node,并且永远放在最后. (每次updatePointClouds都要刷新一次链表)
  30. if (!node.loaded) {
  31. return;
  32. }
  33. let item;
  34. if (this.items[node.id] == null) {
  35. // add to list
  36. item = new LRUItem(node);
  37. item.previous = this.last;
  38. this.last = item;
  39. if (item.previous !== null) {
  40. item.previous.next = item;
  41. }
  42. this.items[node.id] = item;
  43. this.elements++;
  44. if (this.first === null) {
  45. this.first = item;
  46. }
  47. this.numPoints += node.numPoints;
  48. } else {
  49. // update in list
  50. item = this.items[node.id];
  51. if (item.previous === null) {
  52. // handle touch on first element
  53. if (item.next !== null) {
  54. this.first = item.next;
  55. this.first.previous = null;
  56. item.previous = this.last;
  57. item.next = null;
  58. this.last = item;
  59. item.previous.next = item;
  60. }
  61. } else if (item.next === null) {
  62. // handle touch on last element
  63. } else {//从原来的位置挑出放最后
  64. // handle touch on any other element
  65. item.previous.next = item.next;
  66. item.next.previous = item.previous;
  67. item.previous = this.last;
  68. item.next = null;
  69. this.last = item;
  70. item.previous.next = item;
  71. }
  72. }
  73. }
  74. //因为需要显示的都放末尾,所以不显示的部分都在前面,删除时从头删除。(但并不代表开头的一定是不显示的,所以如果第一个仍是显示的,它很可能是root,也就是name='r'的nodeGeo,删除它也就会删除全部)
  75. remove(node){
  76. let lruItem = this.items[node.id];
  77. if (lruItem) {
  78. if (this.elements === 1) {
  79. this.first = null;
  80. this.last = null;
  81. } else {
  82. if (!lruItem.previous) {
  83. this.first = lruItem.next;
  84. this.first.previous = null;
  85. }
  86. if (!lruItem.next) {
  87. this.last = lruItem.previous;
  88. this.last.next = null;
  89. }
  90. if (lruItem.previous && lruItem.next) {
  91. lruItem.previous.next = lruItem.next;
  92. lruItem.next.previous = lruItem.previous;
  93. }
  94. }
  95. delete this.items[node.id];
  96. this.elements--;
  97. this.numPoints -= node.numPoints;
  98. }
  99. }
  100. getLRUItem(){
  101. if (this.first === null) {
  102. return null;
  103. }
  104. let lru = this.first;
  105. return lru.node;
  106. }
  107. toString(){
  108. let string = '{ ';
  109. let curr = this.first;
  110. while (curr !== null) {
  111. string += curr.node.id;
  112. if (curr.next !== null) {
  113. string += ', ';
  114. }
  115. curr = curr.next;
  116. }
  117. string += '}';
  118. string += '(' + this.size() + ')';
  119. return string;
  120. }
  121. freeMemory(){
  122. if (this.elements <= 1) {
  123. return;
  124. }
  125. while (this.numPoints > Potree.pointLoadLimit) {
  126. let element = this.first;
  127. let node = element.node;
  128. this.disposeDescendants(node);
  129. }
  130. }
  131. disposeDescendants(node){
  132. let stack = [];
  133. stack.push(node);
  134. while (stack.length > 0) {
  135. let current = stack.pop();
  136. // console.log(current);
  137. current.dispose(); //真正删除geometry等
  138. this.remove(current);
  139. for (let key in current.children) {
  140. if (current.children.hasOwnProperty(key)) {
  141. let child = current.children[key];
  142. if (child.loaded) {
  143. stack.push(current.children[key]);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. export {LRU, LRUItem};