Skip to content

Commit 75effe4

Browse files
committed
Remove trailing references from ManagedArray and Heap
1 parent d01584a commit 75effe4

File tree

4 files changed

+105
-3
lines changed

4 files changed

+105
-3
lines changed

Source/Core/Heap.js

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ function Heap(options) {
2424
this._maximumLength = undefined;
2525
}
2626

27+
function removeTrailingReferences(heap, newLength) {
28+
var originalLength = heap._length;
29+
if (newLength < originalLength) {
30+
var array = heap._array;
31+
for (var i = newLength; i < originalLength; ++i) {
32+
array[i] = undefined;
33+
}
34+
}
35+
}
36+
2737
Object.defineProperties(Heap.prototype, {
2838
/**
2939
* Gets the length of the heap.
@@ -65,6 +75,7 @@ Object.defineProperties(Heap.prototype, {
6575
return this._maximumLength;
6676
},
6777
set: function (value) {
78+
removeTrailingReferences(this, value);
6879
this._maximumLength = value;
6980
if (this._length > value && value > 0) {
7081
this._length = value;
@@ -211,6 +222,7 @@ Heap.prototype.pop = function (index) {
211222
var root = array[index];
212223
swap(array, index, --this._length);
213224
this.heapify(index);
225+
array[this._length] = undefined; // Remove trailing reference
214226
return root;
215227
};
216228

Source/Core/ManagedArray.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ function ManagedArray(length) {
1616
this._length = length;
1717
}
1818

19+
function removeTrailingReferences(managedArray, newLength) {
20+
var array = managedArray._array;
21+
var originalLength = managedArray._length;
22+
if (newLength < originalLength) {
23+
for (var i = newLength; i < originalLength; ++i) {
24+
array[i] = undefined;
25+
}
26+
}
27+
}
28+
1929
Object.defineProperties(ManagedArray.prototype, {
2030
/**
2131
* Gets or sets the length of the array.
@@ -29,6 +39,7 @@ Object.defineProperties(ManagedArray.prototype, {
2939
return this._length;
3040
},
3141
set: function (length) {
42+
removeTrailingReferences(this, length);
3243
this._length = length;
3344
if (length > this._array.length) {
3445
this._array.length = length;
@@ -74,7 +85,7 @@ ManagedArray.prototype.set = function (index, element) {
7485
Check.typeOf.number("index", index);
7586
//>>includeEnd('debug');
7687

77-
if (index >= this.length) {
88+
if (index >= this._length) {
7889
this.length = index + 1;
7990
}
8091
this._array[index] = element;
@@ -105,7 +116,12 @@ ManagedArray.prototype.push = function (element) {
105116
* @returns {*} The last element in the array.
106117
*/
107118
ManagedArray.prototype.pop = function () {
108-
return this._array[--this.length];
119+
if (this._length === 0) {
120+
return undefined;
121+
}
122+
var element = this._array[this._length - 1];
123+
--this.length;
124+
return element;
109125
};
110126

111127
/**
@@ -142,7 +158,7 @@ ManagedArray.prototype.resize = function (length) {
142158
* @param {Number} [length] The length.
143159
*/
144160
ManagedArray.prototype.trim = function (length) {
145-
length = defaultValue(length, this.length);
161+
length = defaultValue(length, this._length);
146162
this._array.length = length;
147163
};
148164
export default ManagedArray;

Specs/Core/HeapSpec.js

+37
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import { Heap } from "../../Source/Cesium.js";
33
describe("Core/Heap", function () {
44
var length = 100;
55

6+
function expectTrailingReferenceToBeRemoved(heap) {
7+
var array = heap._array;
8+
var length = heap._length;
9+
var reservedLength = array.length;
10+
for (var i = length; i < reservedLength; ++i) {
11+
expect(array[i]).toBeUndefined();
12+
}
13+
}
14+
615
function checkHeap(heap, comparator) {
716
var array = heap.internalArray;
817
var pass = true;
@@ -90,6 +99,34 @@ describe("Core/Heap", function () {
9099
expect(pass).toBe(true);
91100
});
92101

102+
it("pop removes trailing references", function () {
103+
var heap = new Heap({
104+
comparator: comparator,
105+
});
106+
107+
for (var i = 0; i < 10; ++i) {
108+
heap.insert(Math.random());
109+
}
110+
111+
heap.pop();
112+
heap.pop();
113+
114+
expectTrailingReferenceToBeRemoved(heap);
115+
});
116+
117+
it("setting maximum length less than current length removes trailing references", function () {
118+
var heap = new Heap({
119+
comparator: comparator,
120+
});
121+
122+
for (var i = 0; i < 10; ++i) {
123+
heap.insert(Math.random());
124+
}
125+
126+
heap.maximumLength = 5;
127+
expectTrailingReferenceToBeRemoved(heap);
128+
});
129+
93130
it("insert returns the removed element when maximumLength is set", function () {
94131
var heap = new Heap({
95132
comparator: comparator,

Specs/Core/ManagedArraySpec.js

+37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { ManagedArray } from "../../Source/Cesium.js";
22

33
describe("Core/ManagedArray", function () {
4+
function expectTrailingReferenceToBeRemoved(managedArray) {
5+
var array = managedArray._array;
6+
var length = managedArray._length;
7+
var reservedLength = array.length;
8+
for (var i = length; i < reservedLength; ++i) {
9+
expect(array[i]).toBeUndefined();
10+
}
11+
}
12+
413
it("constructor has expected default values", function () {
514
var array = new ManagedArray();
615
expect(array.length).toEqual(0);
@@ -90,6 +99,24 @@ describe("Core/ManagedArray", function () {
9099
}
91100
});
92101

102+
it("pop removes trailing references", function () {
103+
var length = 10;
104+
var array = new ManagedArray(length);
105+
array.set(0, Math.random());
106+
array.set(1, Math.random());
107+
array.set(2, Math.random());
108+
array.pop();
109+
array.pop();
110+
expectTrailingReferenceToBeRemoved(array);
111+
});
112+
113+
it("pop returns undefined if array is empty", function () {
114+
var array = new ManagedArray();
115+
array.push(1);
116+
expect(array.pop()).toBe(1);
117+
expect(array.pop()).toBeUndefined();
118+
});
119+
93120
it("reserve throws if length is less than 0", function () {
94121
var array = new ManagedArray();
95122
expect(function () {
@@ -130,6 +157,16 @@ describe("Core/ManagedArray", function () {
130157
expect(array.length).toEqual(5);
131158
});
132159

160+
it("resize removes trailing references", function () {
161+
var length = 10;
162+
var array = new ManagedArray(length);
163+
array.set(0, Math.random());
164+
array.set(1, Math.random());
165+
array.set(2, Math.random());
166+
array.resize(1);
167+
expectTrailingReferenceToBeRemoved(array);
168+
});
169+
133170
it("trim", function () {
134171
var array = new ManagedArray(2);
135172
array.reserve(10);

0 commit comments

Comments
 (0)