Menu

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

Download this file

86 lines (67 with data), 2.2 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
{
* 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.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;
implementation
{ TArrayEnumerator<T> }
constructor TArrayEnumerator<T>.Create(const A: array of T);
var
Idx: Integer;
begin
inherited Create;
SetLength(fArray, Length(A));
for Idx := Low(A) to High(A) do
fArray[Idx] := A[Idx];
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;
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.