Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.

Commit 5654f03

Browse files
committed
Added has and eq
1 parent 196546e commit 5654f03

File tree

2 files changed

+79
-21
lines changed

2 files changed

+79
-21
lines changed

src/sorted-array.ts

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
* @copyright © 2017 Matus Gura
44
*/
55

6+
interface IEqualElements {
7+
first: number,
8+
length: number
9+
}
10+
611
export class SortedArray<T> {
712
protected items: T[];
813
protected key: string;
@@ -43,29 +48,15 @@ export class SortedArray<T> {
4348
return this;
4449
}
4550

46-
public removeByValue(value: string | number | boolean | T): SortedArray<T> {
47-
const element = this.prepareComparableValue(value);
51+
public removeByValue(value: any): SortedArray<T> {
52+
const found: IEqualElements = this.getEqual(value);
4853

49-
let firstOccurrence: number = null;
50-
let numberOfEqualValues = 0;
51-
let index = this.greaterThan(element, true);
52-
53-
if (index === -1) {
54+
if (found.length === 0) {
5455
return this;
5556
}
5657

57-
while (index < this.items.length) {
58-
if (this.cmp(this.get(index), element) === 0) {
59-
if (firstOccurrence === null) {
60-
firstOccurrence = index;
61-
}
62-
numberOfEqualValues += 1;
63-
} else {
64-
break;
65-
}
66-
index += 1;
67-
}
68-
this.items.splice(firstOccurrence, numberOfEqualValues);
58+
this.items.splice(found.first, found.length);
59+
6960
return this;
7061
}
7162

@@ -94,6 +85,17 @@ export class SortedArray<T> {
9485
return -1;
9586
}
9687

88+
public has(value: any): boolean {
89+
return this.search(value) !== -1;
90+
}
91+
92+
public eq(value: any): SortedArray<T> {
93+
const found: IEqualElements = this.getEqual(value);
94+
const result = new SortedArray(this.key, this.cmp);
95+
result.insert((found.length === 0) ? [] : this.items.slice(found.first, found.first + found.length));
96+
return result;
97+
}
98+
9799
public gt(value: any): SortedArray<T> {
98100
const index = this.greaterThan(value);
99101
const result = new SortedArray(this.key, this.cmp);
@@ -135,7 +137,32 @@ export class SortedArray<T> {
135137
return this.items.toString();
136138
}
137139

138-
protected greaterThan(value: any, orEqual: boolean = false): number {
140+
private getEqual(value: any): IEqualElements {
141+
const element = this.prepareComparableValue(value);
142+
const result: IEqualElements = { first: -1, length: 0 };
143+
144+
let index = this.search(element);
145+
if (index === -1) {
146+
return result;
147+
}
148+
149+
result.first = index++;
150+
let numberOfEqualValues = 1;
151+
152+
while (index < this.items.length) {
153+
if (this.cmp(this.get(index), element) === 0) {
154+
numberOfEqualValues += 1;
155+
} else {
156+
break;
157+
}
158+
index += 1;
159+
}
160+
161+
result.length = numberOfEqualValues;
162+
return result;
163+
}
164+
165+
private greaterThan(value: any, orEqual: boolean = false): number {
139166
const element = this.prepareComparableValue(value);
140167

141168
let middle: number;
@@ -156,7 +183,7 @@ export class SortedArray<T> {
156183
return (max + 1 === this.items.length) ? -1 : max + 1;
157184
}
158185

159-
protected lessThan(value: any, orEqual: boolean = false): number {
186+
private lessThan(value: any, orEqual: boolean = false): number {
160187
const element = this.prepareComparableValue(value);
161188

162189
let middle: number;

tests/index.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ describe('SortedArray', () => {
3030
});
3131
});
3232

33+
describe('has', () => {
34+
it('returns correct result', () => {
35+
arr.insert([0, 5, 10]);
36+
assert.equal(arr.has(5), true);
37+
assert.equal(arr.has(2), false);
38+
});
39+
});
40+
3341
describe('insert', () => {
3442
it('adds a number', () => {
3543
assert.equal(arr.length, 0);
@@ -120,6 +128,16 @@ describe('SortedArray', () => {
120128
});
121129
});
122130

131+
describe('eq', () => {
132+
it('returns empty array', () => {
133+
assert.deepEqual(arr.eq(0).toArray(), []);
134+
});
135+
it('returns correct result', () => {
136+
arr.insert([0, 5, 5, 10]);
137+
assert.deepEqual(arr.eq(5).toArray(), [5, 5]);
138+
});
139+
});
140+
123141
describe('gt', () => {
124142
it('returns empty array', () => {
125143
assert.deepEqual(arr.gt(0).toArray(), []);
@@ -274,6 +292,19 @@ describe('SortedArray with objects', () => {
274292
});
275293
});
276294

295+
describe('eq', () => {
296+
it('returns empty array', () => {
297+
assert.deepEqual(arr.eq(0).toArray(), []);
298+
});
299+
it('returns correct result', () => {
300+
arr.insert(obj);
301+
assert.deepEqual(arr.eq(5).toArray(), [
302+
{ id: 5, args: 0 },
303+
{ id: 5, args: 3 }
304+
]);
305+
});
306+
});
307+
277308
describe('gt', () => {
278309
it('returns empty array', () => {
279310
assert.deepEqual(arr.gt(0).toArray(), []);

0 commit comments

Comments
 (0)