Menu

[r4761]: / branches / parsnip / Src / UContainers.pas  Maximize  Restore  History

Download this file

133 lines (109 with data), 3.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2010-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Provides various generic container classes and enumerators.
}
// TODO: Rename UContainers to something more appropriate to its reduced state
unit UContainers;
interface
uses
// Delphi
Generics.Defaults,
Generics.Collections;
type
/// <summary>Generic enumerator for dynamic arrays.</summary>
TArrayEnumerator<T> = class(TEnumerator<T>)
strict private
var
/// <summary>Array being enumerated.</summary>
fArray: TArray<T>;
/// <summary>Index of current array element in enumeration.</summary>
fIndex: Integer;
strict protected
/// <summary>Gets current array element in enumeration.</summary>
/// <returns>T. Content of current array element.</returns>
function DoGetCurrent: T; override;
/// <summary>Moves to next item in enumeration.</summary>
/// <returns>Boolean. True if there is a next item, False if at end of
/// enumeration.</returns>
function DoMoveNext: Boolean; override;
public
/// <summary>Creates enumerator for given dynamic array.</summary>
/// <param name="A">array of T [in] Array to be enumerated.</param>
/// <remarks>Constructor makes a shallow copy of the given array: value
/// type elements are copied but reference type elements are simply
/// referenced.</remarks>
constructor Create(const A: array of T);
end;
type
/// <summary>Container of functions that help in manipulating arrays.
/// </summary>
TArrayHelper = record
public
/// <summary>Creates and returns a new dynamic array that is an exact copy
/// of the given open array.</summary>
class function Copy<T>(const A: array of T): TArray<T>; static;
/// <summary>Finds the index of the first array element in the given array
/// that has the given value, using the given comparer to check for
/// equality of values. Returns -1 if the value is not in the array.
/// </summary>
class function IndexOf<T>(const A: array of T; const Value: T;
const EqualsFn: TEqualityComparison<T>): Integer; static;
/// <summary>Checks if the value exists as an element of the given array,
/// using the given comparer to check for equality of values.</summary>
class function Contains<T>(const A: array of T; const Value: T;
const EqualsFn: TEqualityComparison<T>): Boolean; static;
end;
implementation
{ TArrayEnumerator<T> }
constructor TArrayEnumerator<T>.Create(const A: array of T);
var
Idx: Integer;
begin
inherited Create;
fArray := TArrayHelper.Copy<T>(A);
fIndex := -1;
end;
function TArrayEnumerator<T>.DoGetCurrent: T;
begin
Result := fArray[fIndex];
end;
function TArrayEnumerator<T>.DoMoveNext: Boolean;
begin
if fIndex >= Length(fArray) then
Exit(False);
Inc(fIndex);
Result := fIndex < Length(fArray);
end;
{ TArrayHelper }
class function TArrayHelper.Contains<T>(const A: array of T; const Value: T;
const EqualsFn: TEqualityComparison<T>): Boolean;
begin
Result := TArrayHelper.IndexOf<T>(A, Value, EqualsFn) >= 0;
end;
class function TArrayHelper.Copy<T>(const A: array of T): TArray<T>;
var
Idx: Integer;
begin
SetLength(Result, Length(A));
for Idx := 0 to High(A) do
Result[Idx] := A[Idx];
end;
class function TArrayHelper.IndexOf<T>(const A: array of T; const Value: T;
const EqualsFn: TEqualityComparison<T>): Integer;
var
Idx: Integer;
begin
for Idx := 0 to High(A) do
if EqualsFn(A[Idx], Value) then
Exit(Idx);
Result := -1;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.