Unit AssocArray;
Interface
Type TAssocArray<Typ> =
Record
Private
Type TRec =
Record
Name: WideString;
Value: Typ;
End;
Var Data:
Array of TRec;
Function GetName (
Index: Integer):
String;
Function GetValue (
Index: Integer): Typ;
Procedure SetValue (
Index: Integer;
Const Value: Typ);
Function GetNamedValue(
Const Name:
String): Typ;
Procedure SetNamedValue(
Const Name:
String;
Const Value: Typ);
Function GetIndex (
Const Name:
String): Integer;
Public
Procedure SetLength(i: Integer);
Function Length: Integer;
Property Name [
Index: Integer]:
String Read GetName;
Property Value[
Index: Integer]: Typ
Read GetValue
Write SetValue;
Default;
Property Value[
Const Name:
String]: Typ
Read GetNamedValue
Write SetNamedValue;
Default;
Property Index[
Const Name:
String]: Integer
Read GetIndex;
Function Add (
Const Value: Typ;
Const Name:
String = '
'): Typ;
Procedure Insert(
Const Value: Typ;
Index: Integer;
Const Name:
String = '
');
Procedure Move ( OldIndex, NewIndex: Integer);
Function Delete(
Index: Integer): Typ;
Overload;
Function Delete(
Const Name:
String): Typ;
Overload;
End;
Implementation
Uses Windows;
Function TAssocArray<Typ>.GetName(
Index: Integer):
String;
Begin
If (
Index >= 0)
and (
Index < System.Length(Data))
Then
Result := Data[
Index].
Name
Else System.Error(reRangeError);
End;
Function TAssocArray<Typ>.GetValue(
Index: Integer): Typ;
Begin
If (
Index >= 0)
and (
Index < System.Length(Data))
Then
Result := Data[
Index].Value
Else System.Error(reRangeError);
End;
Procedure TAssocArray<Typ>.SetValue(
Index: Integer;
Const Value: Typ);
Begin
If (
Index >= 0)
and (
Index < System.Length(Data))
Then
Data[
Index].Value := Value
Else System.Error(reRangeError);
End;
Function TAssocArray<Typ>.GetNamedValue(
Const Name:
String): Typ;
Var i: Integer;
Begin
i := GetIndex(
Name);
If (i >= 0)
and (i < System.Length(Data))
Then
Result := Data[i].Value
Else System.Error(reRangeError);
End;
Procedure TAssocArray<Typ>.SetNamedValue(
Const Name:
String;
Const Value: Typ);
Var i: Integer;
Begin
i := GetIndex(
Name);
If (i >= 0)
and (i < System.Length(Data))
Then
Data[i].Value := Value
Else If Name <> '
'
Then
Add(Value,
Name)
Else System.Error(reRangeError);
End;
Function TAssocArray<Typ>.GetIndex(
Const Name:
String): Integer;
Begin
If Name <> '
'
Then Begin
Result := High(Data);
While (Result >= 0)
and (Data[Result].
Name <>
Name)
do Dec(Result);
End Else Result := -1;
End;
Procedure TAssocArray<Typ>.SetLength(i: Integer);
Begin
System.SetLength(Data, i);
End;
Function TAssocArray<Typ>.Length: Integer;
Begin
Result := System.Length(Data);
End;
Function TAssocArray<Typ>.Add(
Const Value: Typ;
Const Name:
String = '
'): Typ;
Var i: Integer;
Begin
If GetIndex(
Name) >= 0
Then System.Error(reInvalidOp);
i := System.Length(Data);
System.SetLength(Data, i + 1);
Data[i].
Name :=
Name;
Data[i].Value := Value;
End;
Procedure TAssocArray<Typ>.Insert(
Const Value: Typ;
Index: Integer;
Const Name:
String = '
');
Var i: Integer;
Temp: TRec;
Begin
If (
Index >= 0)
and (
Index <= System.Length(Data))
Then Begin
If GetIndex(
Name) >= 0
Then System.Error(reInvalidOp);
i := System.Length(Data);
System.SetLength(Data, i + 1);
If Index < i
Then Begin
{ja und hier wird nicht gemeckert}
{} MoveMemory(@Temp, @Data[i], SizeOf(TRec));
{} MoveMemory(@Data[
Index + 1], @Data[
Index], (i -
Index) * SizeOf(TRec));
{} MoveMemory(@Data[
Index], @Temp, SizeOf(TRec));
{} ZeroMemory(@Temp, SizeOf(TRec));
End;
Data[
Index].
Name :=
Name;
Data[
Index].Value := Value;
End Else System.Error(reRangeError);
End;
Procedure TAssocArray<Typ>.Move(OldIndex, NewIndex: Integer);
Var S:
String;
Temp: Typ;
Begin
If (NewIndex >= 0)
and (NewIndex < System.Length(Data))
Then Begin
S := GetName(OldIndex);
Temp := Delete(OldIndex);
Insert(Temp, NewIndex, S);
End Else System.Error(reRangeError);
End;
Function TAssocArray<Typ>.Delete(
Index: Integer): Typ;
Var i: Integer;
Temp: TRec;
Begin
If (
Index >= 0)
and (
Index < System.Length(Data))
Then Begin
Result := Data[
Index].Value;
i := System.High(Data);
If Index < i
Then Begin
{hier tritt jeweils der Fehler auf}
{} MoveMemory(@Temp, @Data[
Index], SizeOf(TRec));
{} MoveMemory(@Data[
Index], @Data[
Index + 1], (i -
Index) * SizeOf(TRec));
{} MoveMemory(@Data[i], @Temp, SizeOf(TRec));
{} ZeroMemory(@Temp, SizeOf(TRec));
End;
System.SetLength(Data, i);
End Else System.Error(reRangeError);
End;
Function TAssocArray<Typ>.Delete(
Const Name:
String): Typ;
Begin
Result := Delete(GetIndex(
Name));
End;
End.