On 3/22/16 9:53 AM, Denis Kozlov wrote:



Please consider the following implementation logic, I think it covers all angles:

procedure Find(const S: string; out Index: Integer):Boolean;
begin
  if Sorted then
    Result := FindSorted(S, Index);
  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
Denis,

I like how this solves the issue of calling find on truly unsorted lists. My only quibble with this is that 'Sorted' still does not reflect the actual state of the list. For a naturally sorted list there is still unnecessary calls to sort both in your suggested code above and in the existing code (if sorted is set). Can that be handled as well? 'Sorted' right now is a request and an expression of state. As an expression of state it is only valid if the request has been first made.

David

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

Reply via email to