LRU.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 = {};
  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){
  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. remove(node){
  75. let lruItem = this.items[node.id];
  76. if (lruItem) {
  77. if (this.elements === 1) {
  78. this.first = null;
  79. this.last = null;
  80. } else {
  81. if (!lruItem.previous) {
  82. this.first = lruItem.next;
  83. this.first.previous = null;
  84. }
  85. if (!lruItem.next) {
  86. this.last = lruItem.previous;
  87. this.last.next = null;
  88. }
  89. if (lruItem.previous && lruItem.next) {
  90. lruItem.previous.next = lruItem.next;
  91. lruItem.next.previous = lruItem.previous;
  92. }
  93. }
  94. delete this.items[node.id];
  95. this.elements--;
  96. this.numPoints -= node.numPoints;
  97. }
  98. }
  99. getLRUItem(){
  100. if (this.first === null) {
  101. return null;
  102. }
  103. let lru = this.first;
  104. return lru.node;
  105. }
  106. toString(){
  107. let string = '{ ';
  108. let curr = this.first;
  109. while (curr !== null) {
  110. string += curr.node.id;
  111. if (curr.next !== null) {
  112. string += ', ';
  113. }
  114. curr = curr.next;
  115. }
  116. string += '}';
  117. string += '(' + this.size() + ')';
  118. return string;
  119. }
  120. freeMemory(){
  121. if (this.elements <= 1) {
  122. return;
  123. }
  124. while (this.numPoints > Potree.pointLoadLimit) {
  125. let element = this.first;
  126. let node = element.node;
  127. this.disposeDescendants(node);
  128. }
  129. }
  130. disposeDescendants(node){
  131. let stack = [];
  132. stack.push(node);
  133. while (stack.length > 0) {
  134. let current = stack.pop();
  135. // console.log(current);
  136. current.dispose(); //真正删除geometry等
  137. this.remove(current);
  138. for (let key in current.children) {
  139. if (current.children.hasOwnProperty(key)) {
  140. let child = current.children[key];
  141. if (child.loaded) {
  142. stack.push(current.children[key]);
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. export {LRU, LRUItem};