yzt
2023-05-26 de4278af2fd46705a40bac58ec01122db6b7f3d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { Heap } from "../../Source/Cesium.js";
 
describe("Core/Heap", function () {
  var length = 100;
 
  function expectTrailingReferenceToBeRemoved(heap) {
    var array = heap._array;
    var length = heap._length;
    var reservedLength = array.length;
    for (var i = length; i < reservedLength; ++i) {
      expect(array[i]).toBeUndefined();
    }
  }
 
  function checkHeap(heap, comparator) {
    var array = heap.internalArray;
    var pass = true;
    var length = heap.length;
    for (var i = 0; i < length; ++i) {
      var left = 2 * (i + 1) - 1;
      var right = 2 * (i + 1);
      if (left < heap.length) {
        pass = pass && comparator(array[i], array[left]) <= 0;
      }
      if (right < heap.length) {
        pass = pass && comparator(array[i], array[right]) <= 0;
      }
    }
    return pass;
  }
 
  // min heap
  function comparator(a, b) {
    return a - b;
  }
 
  it("maintains heap property on insert", function () {
    var heap = new Heap({
      comparator: comparator,
    });
    var pass = true;
    for (var i = 0; i < length; ++i) {
      heap.insert(Math.random());
      pass = pass && checkHeap(heap, comparator);
    }
 
    expect(pass).toBe(true);
  });
 
  it("maintains heap property on pop", function () {
    var heap = new Heap({
      comparator: comparator,
    });
    var i;
    for (i = 0; i < length; ++i) {
      heap.insert(Math.random());
    }
    var pass = true;
    for (i = 0; i < length; ++i) {
      heap.pop();
      pass = pass && checkHeap(heap, comparator);
    }
    expect(pass).toBe(true);
  });
 
  it("limited by maximum length", function () {
    var heap = new Heap({
      comparator: comparator,
    });
    heap.maximumLength = length / 2;
    var pass = true;
    for (var i = 0; i < length; ++i) {
      heap.insert(Math.random());
      pass = pass && checkHeap(heap, comparator);
    }
    expect(pass).toBe(true);
    expect(heap.length).toBeLessThanOrEqualTo(heap.maximumLength);
    // allowed one extra slot for swapping
    expect(heap.internalArray.length).toBeLessThanOrEqualTo(
      heap.maximumLength + 1
    );
  });
 
  it("pops in sorted order", function () {
    var heap = new Heap({
      comparator: comparator,
    });
    var i;
    for (i = 0; i < length; ++i) {
      heap.insert(Math.random());
    }
    var curr = heap.pop();
    var pass = true;
    for (i = 0; i < length - 1; ++i) {
      var next = heap.pop();
      pass = pass && comparator(curr, next) <= 0;
      curr = next;
    }
    expect(pass).toBe(true);
  });
 
  it("pop removes trailing references", function () {
    var heap = new Heap({
      comparator: comparator,
    });
 
    for (var i = 0; i < 10; ++i) {
      heap.insert(Math.random());
    }
 
    heap.pop();
    heap.pop();
 
    expectTrailingReferenceToBeRemoved(heap);
  });
 
  it("setting maximum length less than current length removes trailing references", function () {
    var heap = new Heap({
      comparator: comparator,
    });
 
    for (var i = 0; i < 10; ++i) {
      heap.insert(Math.random());
    }
 
    heap.maximumLength = 5;
    expectTrailingReferenceToBeRemoved(heap);
  });
 
  it("insert returns the removed element when maximumLength is set", function () {
    var heap = new Heap({
      comparator: comparator,
    });
    heap.maximumLength = length;
 
    var i;
    var max = 0.0;
    var min = 1.0;
    var values = new Array(length);
    for (i = 0; i < length; ++i) {
      var value = Math.random();
      max = Math.max(max, value);
      min = Math.min(min, value);
      values[i] = value;
    }
 
    // Push 99 values
    for (i = 0; i < length - 1; ++i) {
      heap.insert(values[i]);
    }
 
    // Push 100th, nothing is removed so it returns undefined
    var removed = heap.insert(values[length - 1]);
    expect(removed).toBeUndefined();
 
    // Insert value, an element is removed
    removed = heap.insert(max - 0.1);
    expect(removed).toBeDefined();
 
    // If this value is the least priority it will be returned
    removed = heap.insert(max + 0.1);
    expect(removed).toBe(max + 0.1);
  });
 
  it("resort", function () {
    function comparator(a, b) {
      return a.distance - b.distance;
    }
 
    var i;
    var heap = new Heap({
      comparator: comparator,
    });
    for (i = 0; i < length; ++i) {
      heap.insert({
        distance: i / (length - 1),
        id: i,
      });
    }
 
    // Check that elements are initially sorted
    var element;
    var elements = [];
    var currentId = 0;
    while (heap.length > 0) {
      element = heap.pop();
      elements.push(element);
      expect(element.id).toBeGreaterThanOrEqualTo(currentId);
      currentId = element.id;
    }
 
    // Add back into heap
    for (i = 0; i < length; ++i) {
      heap.insert(elements[i]);
    }
 
    // Invert priority
    for (i = 0; i < length; ++i) {
      elements[i].distance = 1.0 - elements[i].distance;
    }
 
    // Resort and check the the elements are popped in the opposite order now
    heap.resort();
    while (heap.length > 0) {
      element = heap.pop();
      expect(element.id).toBeLessThanOrEqualTo(currentId);
      currentId = element.id;
    }
  });
 
  it("maximumLength setter throws if length is less than 0", function () {
    var heap = new Heap({
      comparator: comparator,
    });
    expect(function () {
      heap.maximumLength = -1;
    }).toThrowDeveloperError();
  });
});