14
14
* *
15
15
* hprose common unit for delphi. *
16
16
* *
17
- * LastModified: Dec 18 , 2016 *
17
+ * LastModified: Dec 19 , 2016 *
18
18
* Author: Ma Bingyao <andot@hprose.com> *
19
19
* *
20
20
\**********************************************************/
@@ -90,8 +90,12 @@ EArrayListError = class(Exception);
90
90
function GetCount : Integer;
91
91
function Contains (const Value : Variant): Boolean;
92
92
function GetEnumerator : IListEnumerator;
93
- function IndexOf (const Value : Variant): Integer;
94
- function LastIndexOf (const Value : Variant): Integer;
93
+ function IndexOf (const Value : Variant): Integer; overload;
94
+ function IndexOf (const Value : Variant; StartIndex: Integer): Integer; overload;
95
+ function IndexOf (const Value : Variant; StartIndex, ACount: Integer): Integer; overload;
96
+ function LastIndexOf (const Value : Variant): Integer; overload;
97
+ function LastIndexOf (const Value : Variant; StartIndex: Integer): Integer; overload;
98
+ function LastIndexOf (const Value : Variant; StartIndex, ACount: Integer): Integer; overload;
95
99
function Join (const Glue: string = ' ,' ;
96
100
const LeftPad: string = ' ' ;
97
101
const RightPad: string = ' ' ): string;
@@ -200,8 +204,12 @@ TAbstractList = class(TInterfacedObject, IList, IInvokeableVarObject)
200
204
function Get (Index: Integer): Variant; virtual ; abstract ;
201
205
procedure Put (Index: Integer; const Value : Variant); virtual ; abstract ;
202
206
function GetEnumerator : IListEnumerator; virtual ;
203
- function IndexOf (const Value : Variant): Integer; virtual ; abstract ;
204
- function LastIndexOf (const Value : Variant): Integer; virtual ; abstract ;
207
+ function IndexOf (const Value : Variant): Integer; overload; virtual ;
208
+ function IndexOf (const Value : Variant; StartIndex: Integer): Integer; overload; virtual ;
209
+ function IndexOf (const Value : Variant; StartIndex, ACount: Integer): Integer; overload; virtual ; abstract ;
210
+ function LastIndexOf (const Value : Variant): Integer; overload; virtual ;
211
+ function LastIndexOf (const Value : Variant; StartIndex: Integer): Integer; overload; virtual ;
212
+ function LastIndexOf (const Value : Variant; StartIndex, ACount: Integer): Integer; overload; virtual ; abstract ;
205
213
procedure Insert (Index: Integer; const Value : Variant); virtual ; abstract ;
206
214
procedure InsertRange (Index: Integer; const AList: IImmutableList); overload; virtual ; abstract ;
207
215
procedure InsertRange (Index: Integer; const Container: Variant); overload; virtual ; abstract ;
@@ -275,8 +283,8 @@ TArrayList = class(TAbstractList, IArrayList)
275
283
procedure Exchange (Index1, Index2: Integer); override;
276
284
function Get (Index: Integer): Variant; override;
277
285
procedure Put (Index: Integer; const Value : Variant); override;
278
- function IndexOf (const Value : Variant): Integer; override;
279
- function LastIndexOf (const Value : Variant): Integer; override;
286
+ function IndexOf (const Value : Variant; StartIndex, ACount: Integer ): Integer; overload ; override;
287
+ function LastIndexOf (const Value : Variant; StartIndex, ACount: Integer ): Integer; overload ; override;
280
288
procedure Insert (Index: Integer; const Value : Variant); override;
281
289
procedure InsertRange (Index: Integer; const AList: IImmutableList); overload; override;
282
290
procedure InsertRange (Index: Integer; const Container: Variant); overload; override;
@@ -320,9 +328,9 @@ THashBucket = class(TObject)
320
328
procedure Clear ;
321
329
procedure Delete (HashCode, Index: Integer);
322
330
function IndexOf (HashCode: Integer; const Value : Variant;
323
- CompareProc: TIndexCompareMethod): Integer;
331
+ CompareProc: TIndexCompareMethod; StartIndex, ACount: Integer ): Integer;
324
332
function LastIndexOf (HashCode: Integer; const Value : Variant;
325
- CompareProc: TIndexCompareMethod): Integer;
333
+ CompareProc: TIndexCompareMethod; StartIndex, ACount: Integer ): Integer;
326
334
function Modify (OldHashCode, NewHashCode, Index: Integer): PHashItem;
327
335
property Count: Integer read FCount;
328
336
property Capacity: Integer read FCapacity write SetCapacity;
@@ -357,8 +365,8 @@ THashedList = class(TArrayList, IHashedList)
357
365
procedure DeleteRange (Index, ACount: Integer); override;
358
366
procedure Exchange (Index1, Index2: Integer); override;
359
367
procedure Put (Index: Integer; const Value : Variant); override;
360
- function IndexOf (const Value : Variant): Integer; override;
361
- function LastIndexOf (const Value : Variant): Integer; override;
368
+ function IndexOf (const Value : Variant; StartIndex, ACount: Integer ): Integer; overload ; override;
369
+ function LastIndexOf (const Value : Variant; StartIndex, ACount: Integer ): Integer; overload ; override;
362
370
procedure Insert (Index: Integer; const Value : Variant); override;
363
371
procedure InsertRange (Index: Integer; const AList: IImmutableList); overload; override;
364
372
procedure InsertRange (Index: Integer; const Container: Variant); overload; override;
@@ -2448,6 +2456,17 @@ destructor TAbstractList.Destroy;
2448
2456
inherited Destroy;
2449
2457
end ;
2450
2458
2459
+ function TAbstractList.IndexOf (const Value : Variant): Integer;
2460
+ begin
2461
+ Result := IndexOf(Value , 0 , Count);
2462
+ end ;
2463
+
2464
+ function TAbstractList.IndexOf (const Value : Variant;
2465
+ StartIndex: Integer): Integer;
2466
+ begin
2467
+ Result := IndexOf(Value , StartIndex, Count - StartIndex);
2468
+ end ;
2469
+
2451
2470
procedure TAbstractList.InitLock ;
2452
2471
begin
2453
2472
if not Assigned(FLock) then
@@ -2716,6 +2735,17 @@ function TAbstractList.Last: Variant;
2716
2735
Result := Get(Count - 1 );
2717
2736
end ;
2718
2737
2738
+ function TAbstractList.LastIndexOf (const Value : Variant): Integer;
2739
+ begin
2740
+ Result := LastIndexOf(Value , Count - 1 , Count);
2741
+ end ;
2742
+
2743
+ function TAbstractList.LastIndexOf (const Value : Variant;
2744
+ StartIndex: Integer): Integer;
2745
+ begin
2746
+ Result := LastIndexOf(Value , StartIndex, StartIndex + 1 );
2747
+ end ;
2748
+
2719
2749
procedure TAbstractList.Pack ;
2720
2750
var
2721
2751
I: Integer;
@@ -2939,23 +2969,15 @@ function TArrayList.IndexCompare(Index: Integer;
2939
2969
Result := VarEquals(FList[Index], Value );
2940
2970
end ;
2941
2971
2942
- function TArrayList.IndexOf (const Value : Variant): Integer;
2972
+ function TArrayList.IndexOf (const Value : Variant; StartIndex,
2973
+ ACount: Integer): Integer;
2943
2974
var
2944
- I: Integer;
2945
- begin
2946
- for I := 0 to FCount - 1 do
2947
- if IndexCompare(I, Value ) then begin
2948
- Result := I;
2949
- Exit;
2950
- end ;
2951
- Result := -1 ;
2952
- end ;
2953
-
2954
- function TArrayList.LastIndexOf (const Value : Variant): Integer;
2955
- var
2956
- I: Integer;
2975
+ I, EndIndex: Integer;
2957
2976
begin
2958
- for I := FCount - 1 downto 0 do
2977
+ if StartIndex < 0 then StartIndex := 0 ;
2978
+ EndIndex := StartIndex + ACount - 1 ;
2979
+ if EndIndex >= FCount then EndIndex := FCount - 1 ;
2980
+ for I := StartIndex to EndIndex do
2959
2981
if IndexCompare(I, Value ) then begin
2960
2982
Result := I;
2961
2983
Exit;
@@ -3018,6 +3040,22 @@ procedure TArrayList.InsertRange(Index: Integer; const ConstArray: array of cons
3018
3040
Inc(FCount, N);
3019
3041
end ;
3020
3042
3043
+ function TArrayList.LastIndexOf (const Value : Variant; StartIndex,
3044
+ ACount: Integer): Integer;
3045
+ var
3046
+ I, EndIndex: Integer;
3047
+ begin
3048
+ if StartIndex >= FCount then StartIndex := FCount - 1 ;
3049
+ EndIndex := StartIndex - ACount + 1 ;
3050
+ if EndIndex < 0 then EndIndex := 0 ;
3051
+ for I := StartIndex downto EndIndex do
3052
+ if IndexCompare(I, Value ) then begin
3053
+ Result := I;
3054
+ Exit;
3055
+ end ;
3056
+ Result := -1 ;
3057
+ end ;
3058
+
3021
3059
procedure TArrayList.Move (CurIndex, NewIndex: Integer);
3022
3060
var
3023
3061
Elem: Variant;
@@ -3224,16 +3262,21 @@ procedure THashBucket.Grow;
3224
3262
end ;
3225
3263
3226
3264
function THashBucket.IndexOf (HashCode: Integer; const Value : Variant;
3227
- CompareProc: TIndexCompareMethod): Integer;
3265
+ CompareProc: TIndexCompareMethod; StartIndex, ACount: Integer ): Integer;
3228
3266
var
3267
+ EndIndex: Integer;
3229
3268
HashIndex: Integer;
3230
3269
Item: PHashItem;
3231
3270
begin
3271
+ if StartIndex < 0 then StartIndex := 0 ;
3272
+ EndIndex := StartIndex + ACount - 1 ;
3273
+ if EndIndex >= FCount then EndIndex := FCount - 1 ;
3232
3274
Result := -1 ;
3233
3275
HashIndex := GetHashIndex(HashCode);
3234
3276
Item := FIndices[HashIndex];
3235
3277
while Assigned(Item) do begin
3236
- if (Item^.HashCode = HashCode) and CompareProc(Item^.Index, Value ) then begin
3278
+ if Item^.Index > EndIndex then Exit;
3279
+ if (Item^.Index >= StartIndex) and (Item^.HashCode = HashCode) and CompareProc(Item^.Index, Value ) then begin
3237
3280
Result := Item^.Index;
3238
3281
Exit;
3239
3282
end ;
@@ -3242,18 +3285,23 @@ function THashBucket.IndexOf(HashCode: Integer; const Value: Variant;
3242
3285
end ;
3243
3286
3244
3287
function THashBucket.LastIndexOf (HashCode: Integer; const Value : Variant;
3245
- CompareProc: TIndexCompareMethod): Integer;
3288
+ CompareProc: TIndexCompareMethod; StartIndex, ACount: Integer ): Integer;
3246
3289
var
3290
+ EndIndex: Integer;
3247
3291
HashIndex: Integer;
3248
3292
Item: PHashItem;
3249
3293
begin
3294
+ if StartIndex >= FCount then StartIndex := FCount - 1 ;
3295
+ EndIndex := StartIndex - ACount + 1 ;
3296
+ if EndIndex < 0 then EndIndex := 0 ;
3250
3297
Result := -1 ;
3251
3298
HashIndex := GetHashIndex(HashCode);
3252
3299
Item := FIndices[HashIndex];
3253
3300
if not Assigned(Item) then Exit;
3254
3301
Item := Item^.Prev;
3255
3302
repeat
3256
- if (Item^.HashCode = HashCode) and CompareProc(Item^.Index, Value ) then begin
3303
+ if Item^.Index < EndIndex then Exit;
3304
+ if (Item^.Index <= StartIndex) and (Item^.HashCode = HashCode) and CompareProc(Item^.Index, Value ) then begin
3257
3305
Result := Item^.Index;
3258
3306
Exit;
3259
3307
end ;
@@ -3401,16 +3449,6 @@ function THashedList.HashOf(const Value: Variant): Integer;
3401
3449
Result := HashOfVariant(Value );
3402
3450
end ;
3403
3451
3404
- function THashedList.IndexOf (const Value : Variant): Integer;
3405
- begin
3406
- Result := FHashBucket.IndexOf(HashOf(Value ), Value , { $IFDEF FPC} @{ $ENDIF} IndexCompare);
3407
- end ;
3408
-
3409
- function THashedList.LastIndexOf (const Value : Variant): Integer;
3410
- begin
3411
- Result := FHashBucket.LastIndexOf(HashOf(Value ), Value , { $IFDEF FPC} @{ $ENDIF} IndexCompare);
3412
- end ;
3413
-
3414
3452
procedure THashedList.InsertHash (Index, N: Integer);
3415
3453
var
3416
3454
HashCode, NewHashCode, I: Integer;
@@ -3426,6 +3464,14 @@ procedure THashedList.InsertHash(Index, N: Integer);
3426
3464
end ;
3427
3465
end ;
3428
3466
3467
+ function THashedList.IndexOf (const Value : Variant; StartIndex,
3468
+ ACount: Integer): Integer;
3469
+ begin
3470
+ Result := FHashBucket.IndexOf(HashOf(Value ), Value ,
3471
+ { $IFDEF FPC} @{ $ENDIF} IndexCompare, StartIndex, ACount);
3472
+
3473
+ end ;
3474
+
3429
3475
procedure THashedList.Insert (Index: Integer; const Value : Variant);
3430
3476
begin
3431
3477
inherited Insert(Index, Value );
@@ -3455,6 +3501,13 @@ procedure THashedList.InsertRange(Index: Integer; const ConstArray: array of con
3455
3501
InsertHash(Index, Length(ConstArray));
3456
3502
end ;
3457
3503
3504
+ function THashedList.LastIndexOf (const Value : Variant; StartIndex,
3505
+ ACount: Integer): Integer;
3506
+ begin
3507
+ Result := FHashBucket.LastIndexOf(HashOf(Value ), Value ,
3508
+ { $IFDEF FPC} @{ $ENDIF} IndexCompare, StartIndex, ACount);
3509
+ end ;
3510
+
3458
3511
procedure THashedList.Put (Index: Integer; const Value : Variant);
3459
3512
var
3460
3513
HashCode, I: Integer;
0 commit comments