footballjds
2014-01-23 19:39:01
anyway to do this with simba?
https://github.com/JuhaManninen/Pascal/blob/master/StrHashMap/StrHashMap.pas
https://github.com/JuhaManninen/Pascal/blob/master/StrHashMap/StrHashMap.pas
2014-01-23 19:39:01
2014-01-23 23:12:13
type Pair = record
Key: Pointer;
Value: Pointer;
end;
type Iterator_Type = ^Pair;
type Map = record
size: Uint32;
cap: Uint32;
data: Array of Pair;
end;
Procedure Map.Init();
Begin
size := 0;
cap := 0;
End;
Procedure Map.___resize(NewSize: Uint32);
Begin
cap := NewSize;
SetLength(data, NewSize);
If (size > cap) then
size := cap;
End;
Procedure Map.Insert(var p: Pair);
var
I: Integer;
Begin
if (size >= cap) then
begin
if (cap = 0) then cap := 1;
self.___resize(cap * 2);
end;
For I := 0 To Size Do
If (data[I].Key = P.Key) Then
Exit;
data[size] := p;
size := size + 1;
End;
Function Map.First(): Iterator_Type;
Begin
Result := @data[0];
End;
Function Map.Last(): Iterator_Type;
Begin
Result := @data[size - 1];
End;
Function Iterator_Type.Next(): Iterator_Type;
Begin
Result := self + sizeof(Pair);
End;
Function ToPair(Key: Pointer; Value: Pointer): Pair;
Begin
Result.Key := Key;
Result.Value := Value;
End;
var
MP: Map;
K: Integer;
V: Integer;
type PInt = ^Integer;
begin
K := 100;
V := 1024;
MP.Insert(ToPair(@K, @V));
writeln(Integer(MP.Last()^.Key^));
MP.Insert(ToPair(@K, @V));
writeln(MP.size);
end.
type Pair = record
Key: Pointer;
Value: Pointer;
end;
type Iterator_Type = ^Pair;
type PairComparator = Function(Val: Pointer; Val2: Pointer): Boolean;
type Map = record
size: Uint32;
cap: Uint32;
comparator: PairComparator;
data: Array of Pair;
end;
Procedure Map.Init(Comp: PairComparator);
Begin
size := 0;
cap := 0;
comparator := @comp;
End;
Procedure Map.___resize(NewSize: Uint32);
Begin
cap := NewSize;
SetLength(data, NewSize);
If (size > cap) then
size := cap;
End;
Procedure Map.Insert(var p: Pair);
var
I: Integer;
Begin
if (@comparator = nil) then
RaiseException('PAIR INVALID COMPARATOR!');
if (size >= cap) then
begin
if (cap = 0) then cap := 1;
self.___resize(cap * 2);
end;
For I := 0 To Size - 1 Do
If (comparator(data[I].Key, p.Key)) Then
Exit;
data[size] := p;
size := size + 1;
End;
Function Map.First(): Iterator_Type;
Begin
Result := @data[0];
End;
Function Map.Last(): Iterator_Type;
Begin
Result := @data[size];
End;
Function Iterator_Type.Next(): Iterator_Type;
Begin
Result := self + sizeof(Pair);
End;
Function ToPair(Key: Pointer; Value: Pointer): Pair;
Begin
Result.Key := Key;
Result.Value := Value;
End;
Function IntComparator(Key: Pointer; Key2: Pointer): Boolean;
type PInt = ^Integer;
Begin
Result := PInt(Key)^ = PInt(Key2)^;
End;
var
MP: Map;
K, V, K2, V2: Integer;
it: Iterator_Type;
begin
K := 100;
V := 1024;
K2 := 1024;
V2 := 100;
MP.Init(@IntComparator);
MP.Insert(ToPair(@K, @V));
MP.Insert(ToPair(@K2, @V2));
it := MP.First();
while(it <> MP.Last()) do
begin
writeln(Integer(it^.Key^));
it := it.Next();
end;
end.
2014-01-24 13:27:10
2014-01-24 14:06:06
From: footballjds
Thanks for the reply Brandon. Could you explain what the ^ means?