From ceef6e44f1fd43dd55f73e0f4c3074eb886ca6f1 Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Tue, 22 Jul 2025 19:19:37 +0300 Subject: [PATCH] fix(front, back) -> undefined exception; feat(resize) --- Makefile | 2 ++ package-lock.json | 4 ++-- package.json | 2 +- src/List.js | 15 +++++++++++-- src/containers/Sequence.js | 13 +++++++++--- src/iterators/Forward.js | 4 ++-- src/utils/concept.js | 2 +- src/utils/mixin.js | 2 +- test/List.test.js | 17 +++++++++++++-- types/List.d.ts | 9 ++------ types/containers/Sequence.d.ts | 39 ++++++++++++---------------------- 11 files changed, 62 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index 07e3536..2da070b 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +.PHONY: build test + build: npx esbuild src/index.js --bundle --minify --outfile=dist/index.js --format=esm npx rollup -c diff --git a/package-lock.json b/package-lock.json index 51dd093..b746dce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sek1ro/iterables", - "version": "0.1.3", + "version": "0.1.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sek1ro/iterables", - "version": "0.1.3", + "version": "0.1.4", "license": "LGPL-3.0-only", "devDependencies": { "rollup": "^4.45.1", diff --git a/package.json b/package.json index 52790d7..01ffc17 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sek1ro/iterables", - "version": "0.1.3", + "version": "0.1.4", "description": "A data structures library based on iterators, inspired by libstdc++", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/List.js b/src/List.js index 5f03b6d..30bf7dc 100644 --- a/src/List.js +++ b/src/List.js @@ -35,6 +35,16 @@ export class List { return this._end.clone() } + resize(size, value) { + if (this._size > size) + while (this._size !== size) + this.popBack() + + else + while (this._size !== size) + this.pushBack(value) + } + _shiftRight(dest, generator) { let prev, res, new_item, item = dest.deref() @@ -75,7 +85,7 @@ export class List { } } -List.from = function (obj) { +List.from = function(obj) { const list = new List() for (const value of obj) { list.pushBack(value) @@ -98,6 +108,7 @@ export class ListItem { } export class ListIterator { + constructor(item) { this._item = item } @@ -130,4 +141,4 @@ export class ListIterator { } } -mixinClasses(ListIterator, BidirectionalIterator) \ No newline at end of file +mixinClasses(ListIterator, BidirectionalIterator) diff --git a/src/containers/Sequence.js b/src/containers/Sequence.js index 1f90b9c..d77079b 100644 --- a/src/containers/Sequence.js +++ b/src/containers/Sequence.js @@ -1,5 +1,5 @@ import { BidirectionalIterator } from "../iterators/Bidirectional" -import { satisfiesConcept } from "../utils/concept" +import { getCloneFunc, satisfiesConcept } from "../utils/concept" export class SequenceContainer { clone() { @@ -16,6 +16,9 @@ export class SequenceContainer { } get front() { + if (this.empty()) + throw new Error('front element is undefined') + return this.begin().value } @@ -24,6 +27,9 @@ export class SequenceContainer { } get back() { + if (this.empty()) + throw new Error('back element is undefined') + return this.end().dec().value } @@ -151,10 +157,11 @@ export class SequenceContainer { } } -SequenceContainer.is = function (obj, ...args) { +SequenceContainer.is = function(obj, ...args) { const pure_virtual = [ 'move', 'size', + 'resize', 'begin', 'end', '_shiftRight', @@ -179,4 +186,4 @@ SequenceContainer.is = function (obj, ...args) { 'toString', ] return satisfiesConcept(obj, pure_virtual, virtual, ...args) -} \ No newline at end of file +} diff --git a/src/iterators/Forward.js b/src/iterators/Forward.js index 7fad264..c6726ef 100644 --- a/src/iterators/Forward.js +++ b/src/iterators/Forward.js @@ -6,7 +6,7 @@ export class ForwardIterator { } get value() { - return this.deref()?.value + return this.deref().value } set value(value) { @@ -32,4 +32,4 @@ ForwardIterator.is = function (obj, ...args) { 'neq', ] return satisfiesConcept(obj, pure_virtual, virtual, ...args) -} \ No newline at end of file +} diff --git a/src/utils/concept.js b/src/utils/concept.js index 63f8ec4..cead87f 100644 --- a/src/utils/concept.js +++ b/src/utils/concept.js @@ -31,4 +31,4 @@ export function satisfiesConcept(obj, pure_virtual, virtual, debug) { } } return true -} \ No newline at end of file +} diff --git a/src/utils/mixin.js b/src/utils/mixin.js index 153ceb8..ebe375b 100644 --- a/src/utils/mixin.js +++ b/src/utils/mixin.js @@ -4,4 +4,4 @@ export function mixinClasses(dest, ...sources) { delete properties.constructor Object.defineProperties(dest.prototype, properties) } -} \ No newline at end of file +} diff --git a/test/List.test.js b/test/List.test.js index b022b15..2b4dedf 100644 --- a/test/List.test.js +++ b/test/List.test.js @@ -49,7 +49,7 @@ test('move, empty', () => { test('constructor, assign', () => { expect(new List().size()).toEqual(0) - expect(new List().back).toEqual(undefined) + expect(() => new List().back).toThrow() expect(listEquArray(new List(null), [null])).toBeTruthy() expect(listEquArray(new List(null, 2), [null, null])).toBeTruthy() { @@ -60,6 +60,19 @@ test('constructor, assign', () => { } }) +test('resize', () => { + { + const list = List.from([1, 2, 3]) + list.resize(1) + expect(listEquArray(list, [1])).toBeTruthy() + } + { + const list = List.from([1]) + list.resize(3, 2) + expect(listEquArray(list, [1, 2, 2])).toBeTruthy() + } +}) + test('insert, clone', () => { { const list = List.from([1, 2]) @@ -135,4 +148,4 @@ test('pushBack, pushFront, popBack, popFront, back, front', () => { test('toJSON, toString', () => { expect(List.from([1, 2]).toJSON()).toEqual('[ 1, 2 ]') expect(List.from([0n]).toString()).toEqual('[ 0 ]') -}) \ No newline at end of file +}) diff --git a/types/List.d.ts b/types/List.d.ts index 058fe23..0cb8deb 100644 --- a/types/List.d.ts +++ b/types/List.d.ts @@ -2,22 +2,17 @@ import { SequenceContainer } from "./containers/Sequence"; import { BidirectionalIterator } from "./iterators/Bidirectional"; import { Item } from "./iterators/Forward"; -export class List extends SequenceContainer { +export class List extends SequenceContainer> { /** * empty list */ constructor(); - /** - * list with single element - * @param value which will be inserted - */ - constructor(value: T); /** * list with n similar elements * @param value which will be inserted * @param n count of how many values will be inserted */ - constructor(value: T, n: number); + constructor(value: T, n?: number); /** * copies range of another list * @param begin of another SequenceContainer diff --git a/types/containers/Sequence.d.ts b/types/containers/Sequence.d.ts index d845938..c2f1349 100644 --- a/types/containers/Sequence.d.ts +++ b/types/containers/Sequence.d.ts @@ -1,13 +1,13 @@ -import { BidirectionalIterator } from "../iterators/Bidirectional"; +import { ForwardIterator } from "../iterators/Forward"; -export class SequenceContainer { - move(rhs: SequenceContainer): this; +export class SequenceContainer> { + move(rhs: SequenceContainer): this; size(): number; - begin(): BidirectionalIterator; - end(): BidirectionalIterator; + begin(): It; + end(): It; clone(): this; - copy(rhs: SequenceContainer): this; + copy(rhs: SequenceContainer): this; empty(): boolean; /** * @returns direct access to front item's value @@ -26,7 +26,7 @@ export class SequenceContainer { * @param end of another SequenceContainer * @returns iterator right after end iterator */ - insert(dest: BidirectionalIterator, begin: BidirectionalIterator, end: BidirectionalIterator): BidirectionalIterator; + insert(dest: It, begin: It, end: It): It; /** * inserts [begin, end) at dest * @param dest where to insert @@ -34,52 +34,39 @@ export class SequenceContainer { * @param n count of how many values will be inserted * @returns iterator right after last inserted value */ - insert(dest: BidirectionalIterator, value: T, n: number): BidirectionalIterator; - /** - * inserts [begin, end) at dest - * @param dest where to insert - * @param value which will be inserted - * @returns iterator right after inserted value - */ - insert(dest: BidirectionalIterator, value: T): BidirectionalIterator; + insert(dest: It, value: T, n?: number): It; /** * same as list.clear(); list.insert(list.begin(), ...) * @param begin of another SequenceContainer * @param end of another SequenceContainer * @returns end iterator */ - assign(begin: BidirectionalIterator, end: BidirectionalIterator): BidirectionalIterator; + assign(begin: It, end: It): It; /** * same as list.clear(); list.insert(list.begin(), ...) * @param value which will be inserted * @param n count of how many values will be inserted * @returns iterator right after last inserted value */ - assign(value: T, n: number): BidirectionalIterator; - /** - * same as list.clear(); list.insert(list.begin(), ...) - * @param value which will be inserted - * @returns iterator right after inserted value - */ - assign(value: T): BidirectionalIterator; + assign(value: T, n?: number): It; /** * erases [begin, end) * @param begin of erased range * @param end of erased range * @returns iterator right after erased range */ - erase(begin: BidirectionalIterator, end: BidirectionalIterator): BidirectionalIterator; + erase(begin: It, end: It): It; /** * erases single item * @param begin which points to erased item * @returns iterator right after erased value */ - erase(begin: BidirectionalIterator): BidirectionalIterator; + erase(item: It): It; /** * same as list.erase(list.begin(), list.end()) * @returns end iterator */ - clear(): BidirectionalIterator; + clear(): It; pushFront(value: T): void; pushBack(value: T): void; popFront(): void;