nowjs-core
Version:
NowCanDo Javascript Core [nowjs-core] is a library written by TypeScript code maintains under Apache 2.0 licence
90 lines (89 loc) • 2.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../linq/index");
const index_2 = require("./index");
class SortedSet {
constructor(comparator, enumerable) {
this.comparator = comparator;
this.arr = [];
if (enumerable && enumerable[Symbol.iterator]) {
for (const x of enumerable) {
this.add(x);
}
}
}
[Symbol.iterator]() {
return this.arr[Symbol.iterator]();
}
isEmpty() {
return this.size === 0;
}
toSet() {
return new Set(this);
}
add(value) {
if (this.size < 1) {
this.arr.push(value);
}
else {
this.arr.push(value);
this.arr = this.arr.sort(this.comparator);
}
return this;
}
clear() {
this.arr.splice(0, this.arr.length);
}
get(index) {
return this.arr[index];
}
delete(value) {
const index = this.arr.indexOf(value);
if (index >= 0) {
this.arr.splice(index, 1);
this.arr = this.arr.sort(this.comparator);
return true;
}
return false;
}
forEach(callbackfn, thisArg) {
this.arr.forEach((item, index, col) => {
callbackfn(item, index, this);
});
}
has(value) {
return this.arr.includes(value);
}
get size() {
return this.arr.length;
}
linq() {
return new index_1.Enumerable(this);
}
plinq() {
return new index_1.ParallelEnumerable(this);
}
indexOf(item) {
return this.arr.indexOf(item);
}
lastIndexOf(item) {
return this.arr.lastIndexOf(item);
}
toArray() {
const arr = [];
for (const item of this) {
arr.push(item);
}
return arr;
}
toCollection() {
return new index_2.Collection(this);
}
toList() {
return new index_2.List(this);
}
clone() {
return new SortedSet(this.comparator, this);
}
}
exports.SortedSet = SortedSet;