3
3
* @copyright © 2017 Matus Gura
4
4
*/
5
5
6
+ interface IEqualElements {
7
+ first : number ,
8
+ length : number
9
+ }
10
+
6
11
export class SortedArray < T > {
7
12
protected items : T [ ] ;
8
13
protected key : string ;
@@ -43,29 +48,15 @@ export class SortedArray<T> {
43
48
return this ;
44
49
}
45
50
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 ) ;
48
53
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 ) {
54
55
return this ;
55
56
}
56
57
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
+
69
60
return this ;
70
61
}
71
62
@@ -94,6 +85,17 @@ export class SortedArray<T> {
94
85
return - 1 ;
95
86
}
96
87
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
+
97
99
public gt ( value : any ) : SortedArray < T > {
98
100
const index = this . greaterThan ( value ) ;
99
101
const result = new SortedArray ( this . key , this . cmp ) ;
@@ -135,7 +137,32 @@ export class SortedArray<T> {
135
137
return this . items . toString ( ) ;
136
138
}
137
139
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 {
139
166
const element = this . prepareComparableValue ( value ) ;
140
167
141
168
let middle : number ;
@@ -156,7 +183,7 @@ export class SortedArray<T> {
156
183
return ( max + 1 === this . items . length ) ? - 1 : max + 1 ;
157
184
}
158
185
159
- protected lessThan ( value : any , orEqual : boolean = false ) : number {
186
+ private lessThan ( value : any , orEqual : boolean = false ) : number {
160
187
const element = this . prepareComparableValue ( value ) ;
161
188
162
189
let middle : number ;
0 commit comments