On 22/03/2016 17:56, Michael Van Canneyt wrote:
or better something concise like ltAuto,ltUser,ltNone.

It may make more sense to call it ListSortType (as opposed to ListType):
  TListSortType = (lstNone, lstAuto, lstManual);

Something like this could work (prototype code):

=======================================================
function TStringList.GetSorted: Boolean;
begin
  Result := (FSortType = lstAuto);
end;

procedure TStringList.SetSorted(Value: Boolean);
begin
  if Value xor (FSortType= lstAuto)then
  begin
    if Value then
begin
      Sort;
      FSortType:= lstAuto;
    end
    else
FSortType:= lstNone;
  end;
end;

procedure TStringList.SetSortType(Value: TListSortType);
begin
  if FSortType<> Value then
  begin
    if Value = lstAutothen
Sort;
    FSorted := Value;
  end;
end;

procedure TStringList.Find(const S: string; out Index: Integer):Boolean;
begin
  if FSortType <> lstNonethen
    Result := FindSorted(S, Index);
  else
  begin
    Index := IndexOf(S);
    Result := (Index >= 0);
  end;
end;

procedure TStringList.FindSorted(const S: string; out Index: Integer):Boolean;
begin
  ifFSortType= lstNonethen
    raise Exception.Create('List must be sorted');
  // sorted search logic here...
end;
=======================================================

An alternative solution, without adding TListSortType:

=======================================================
procedure Find(const S: string; out Index: Integer; *AssumeSorted: Boolean = False*):Boolean;
begin
  if *AssumeSorted* or Sorted then
    Result := FindSorted(S, Index, False);
  else
  begin
    Index := IndexOf(S);
    Result := (Index >= 0);
  end;
end;

procedure FindSorted(const S: string; out Index: Integer; *CheckSorted: Boolean = True*):Boolean;
begin
  if *CheckSorted* and not Sorted then
    raise Exception.Create('List must be sorted');
  // search logic here...
end;
=======================================================

Denis
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to