Skip to content

Commit a6b787f

Browse files
committed
Revert "make copy customizable with optional capacity"
This reverts commit ea3bf1d.
1 parent ea3bf1d commit a6b787f

File tree

2 files changed

+6
-51
lines changed

2 files changed

+6
-51
lines changed

src/lib/provable/dynamic-array.ts

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,6 @@ class DynamicArrayBase<ProvableValue = any, Value = any> {
417417
* Shifts all elements of the array to the right by n positions, increasing
418418
* the length by n, which must result in less than or equal to the capacity.
419419
* The new elements on the left are set to NULL values.
420-
*
421-
* **Warning**: This does not extend the capacity of the array, so if the
422-
* array cannot be extended with `n` elements, it will fail. Consider copying
423-
* the array into a new one that has larger capacity first.
424420
*
425421
* @param n
426422
*/
@@ -440,25 +436,13 @@ class DynamicArrayBase<ProvableValue = any, Value = any> {
440436
}
441437

442438
/**
443-
* Copies the current dynamic array, returning a new instance with the same
444-
* values and length. Optionally, one can specify a different `capacity` to
445-
* set the new array's capacity. If it is larger than the current capacity,
446-
* the copy can be done without failing. If it is smaller, the current length
447-
* must be less than or equal to the new capacity.
448-
*
449-
* @returns a new DynamicArray instance with the same values as the current.
450-
*
439+
* @returns a new DynamicArray instance with the same values as the current
451440
*/
452-
copy(capacity?: number): DynamicArray<ProvableValue, Value> {
453-
const newCapacity = capacity ?? this.capacity;
454-
455-
if (newCapacity < this.capacity) {
456-
this.length.assertLessThanOrEqual(new Field(newCapacity));
457-
}
458-
459-
const CopiedArray = DynamicArray(this.innerType, { capacity: newCapacity });
460-
461-
return new CopiedArray(this.array, this.length);
441+
copy(): this {
442+
let newArr = new (<any>this.constructor)();
443+
newArr.array = this.array.slice();
444+
newArr.length = this.length;
445+
return newArr;
462446
}
463447

464448
/**

src/lib/provable/test/dynamic-array.unit-test.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,6 @@ import { Provable } from '../provable.js';
3838
assert(fromArrayLength.get(new Field(1)).value.equals(new Field(2)));
3939
assert(fromArrayLength.get(new Field(2)).value.equals(new Field(3)));
4040
fromArrayLength.getOption(new Field(3)).assertNone();
41-
42-
// Copy into new dynamic arrays of different capacities
43-
let copySame = fromArray.copy();
44-
assert(copySame.length.equals(fromArray.length));
45-
assert(copySame.capacity === fromArray.capacity);
46-
assert(copySame.get(new Field(0)).value.equals(fromArray.get(new Field(1)).value));
47-
assert(copySame.get(new Field(1)).value.equals(fromArray.get(new Field(2)).value));
48-
assert(copySame.get(new Field(2)).value.equals(fromArray.get(new Field(3)).value));
49-
for (let i = 3; i < 8; i++) {
50-
copySame.getOption(new Field(i)).assertNone();
51-
}
52-
53-
let copyLonger = fromArray.copy(16);
54-
assert(copyLonger.length.equals(fromArray.length));
55-
assert(copyLonger.capacity === 16);
56-
assert(copyLonger.get(new Field(0)).value.equals(fromArray.get(new Field(1)).value));
57-
assert(copyLonger.get(new Field(1)).value.equals(fromArray.get(new Field(2)).value));
58-
assert(copyLonger.get(new Field(2)).value.equals(fromArray.get(new Field(3)).value));
59-
for (let i = 3; i < 16; i++) {
60-
copyLonger.getOption(new Field(i)).assertNone();
61-
}
62-
63-
let copyShorter = fromArray.copy(4);
64-
assert(copyShorter.length.equals(fromArray.length));
65-
assert(copyShorter.capacity === 4);
66-
assert(copyShorter.get(new Field(0)).value.equals(fromArray.get(new Field(1)).value));
67-
assert(copyShorter.get(new Field(1)).value.equals(fromArray.get(new Field(2)).value));
68-
assert(copyShorter.get(new Field(2)).value.equals(fromArray.get(new Field(3)).value));
69-
copyShorter.getOption(new Field(3)).assertNone();
7041

7142
// Initialize an empty dynamic array
7243
let bytes = new Bytestring();

0 commit comments

Comments
 (0)