home *** CD-ROM | disk | FTP | other *** search
- {*********************************************************}
- {* AAIntLst *}
- {* Copyright (c) Julian M Bucknall 2001 *}
- {* All rights reserved. *}
- {*********************************************************}
- {* Algorithms Alfresco: An integer list *}
- {*********************************************************}
-
- {Note: this unit is released as freeware. In other words, you are free
- to use this unit in your own applications, however I retain all
- copyright to the code. JMB}
-
- unit AAIntLst;
-
- interface
-
- uses
- Classes;
-
- type
- TaaIntList = class
- private
- FAllowDups : boolean;
- FCount : integer;
- FIsSorted : boolean;
- FList : TList;
- protected
- function ilGetCapacity : integer;
- function ilGetItem(aInx : integer) : integer;
-
- procedure ilSetCapacity(aValue : integer);
- procedure ilSetCount(aValue : integer);
- procedure ilSetIsSorted(aValue : boolean);
- procedure ilSetItem(aInx : integer; aValue : integer);
-
- procedure ilSort;
- public
- constructor Create;
- destructor Destroy; override;
-
- function Add(aItem : integer) : integer;
- procedure Clear;
- procedure Insert(aInx : Integer; aItem : Pointer);
-
- property AllowDups : boolean
- read FAllowDups write FAllowDups;
- property Capacity : integer
- read ilGetCapacity write ilSetCapacity;
- property Count : integer
- read FCount write ilSetCount;
- property IsSorted : boolean
- read FIsSorted write ilSetIsSorted;
- property Items[aInx : integer] : integer
- read ilGetItem write ilSetItem; default;
- end;
-
- implementation
-
- uses
- SysUtils;
-
- {====================================================================}
- constructor TaaIntList.Create;
- begin
- inherited Create;
- FList := TList.Create;
- FIsSorted := true;
- FAllowDups := false;
- end;
- {--------}
- destructor TaaIntList.Destroy;
- begin
- FList.Free;
- inherited Destroy;
- end;
- {--------}
- function TaaIntList.Add(aItem : integer) : integer;
- var
- L, R, M : integer;
- begin
- if (not IsSorted) or (Count = 0) then
- Result := FList.Add(pointer(aItem))
- else begin
- Result := -1;
- L := 0;
- R := pred(Count);
- while (L <= R) do begin
- M := (L + R) div 2;
- if (integer(FList.List^[M]) = aItem) then begin
- if AllowDups then begin
- FList.Insert(M, pointer(aItem));
- Result := M;
- end;
- Exit;
- end;
- if (integer(FList.List^[M]) < aItem) then
- L := M + 1
- else
- R := M - 1;
- end;
- FList.Insert(L, pointer(aItem));
- Result := L;
- end;
- inc(FCount);
- end;
- {--------}
- procedure TaaIntList.Clear;
- begin
- FList.Clear;
- FCount := 0;
- FIsSorted := true;
- end;
- {--------}
- function TaaIntList.ilGetCapacity : integer;
- begin
- Result := FList.Capacity;
- end;
- {--------}
- function TaaIntList.ilGetItem(aInx : integer) : integer;
- begin
- Assert((0 <= aInx) and (aInx < Count), 'Index out of bounds');
- Result := integer(FList.List^[aInx]);
- end;
- {--------}
- procedure TaaIntList.ilSetCapacity(aValue : integer);
- begin
- FList.Capacity := aValue;
- end;
- {--------}
- procedure TaaIntList.ilSetCount(aValue : integer);
- begin
- FList.Count := aValue;
- FCount := FList.Count;
- end;
- {--------}
- procedure TaaIntList.ilSetIsSorted(aValue : boolean);
- begin
- if (aValue <> FIsSorted) then begin
- FIsSOrted := aValue;
- if FIsSorted then
- ilSort;
- end;
- end;
- {--------}
- procedure TaaIntList.ilSetItem(aInx : integer; aValue : integer);
- begin
- Assert((0 <= aInx) and (aInx < Count), 'Index out of bounds');
- FList.List^[aInx] := pointer(aValue);
- end;
- {--------}
- procedure TaaIntList.ilSort;
- begin
- Assert(false, 'TaaIntList.ilSort has not been implemented yet');
- end;
- {--------}
- procedure TaaIntList.Insert(aInx : Integer; aItem : Pointer);
- begin
- FIsSorted := false;
- FList.Insert(aInx, pointer(aItem));
- inc(FCount);
- end;
- {====================================================================}
-
- end.
-