德尔福:" INDEXOF"在数组的对象 [英] Delphi: "IndexOF" an Object in an Array

查看:127
本文介绍了德尔福:" INDEXOF"在数组的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我TPoint数组。现在,我想知道是否有东西在那里,如:

I got an Array of TPoint. Now I was wondering if there is something out there like:

apts: TArray<TPoint>;
//
if (apts.indexOF(p1) < 0) do smth

所以是有一些实际搜索的东西的阵列过程(在这种情况下,点(P1))
并返回我的指数呢?

so is there some procedure that actually searches an array for something ( in this case point (p1) ) and returns me the Index of it?

推荐答案

Generics.Collections 在tarray 静态类>有一个二进制搜索,但没有线性搜索。我填补这一缺口尤其像这样的:

The TArray static class in Generics.Collections has a binary search, but no linear search. I fill that particular gap like this:

type
  TArray = class(Generics.Collections.TArray)
  public
    class function Contains<T>(const Values: array of T; const Item: T;
      const Comparer: IEqualityComparer<T>; out ItemIndex: Integer): Boolean;
      overload; static;
    class function Contains<T>(const Values: array of T; const Item: T;
      out ItemIndex: Integer): Boolean; overload; static;
    class function Contains<T>(const Values: array of T; const Item: T): Boolean;
      overload; static;
    class function IndexOf<T>(const Values: array of T; const Item: T;
      const Comparer: IEqualityComparer<T>): Integer; overload; static;
    class function IndexOf<T>(const Values: array of T; const Item: T): Integer;
      overload; static;
  end;

class function TArray.Contains<T>(const Values: array of T; const Item: T;
  const Comparer: IEqualityComparer<T>; out ItemIndex: Integer): Boolean;
var
  Index: Integer;
begin
  for Index := 0 to high(Values) do begin
    if Comparer.Equals(Values[Index], Item) then begin
      ItemIndex := Index;
      Result := True;
      exit;
    end;
  end;
  ItemIndex := -1;
  Result := False;
end;

class function TArray.Contains<T>(const Values: array of T; const Item: T;
  out ItemIndex: Integer): Boolean;
begin
  Result := Contains<T>(Values, Item, TEqualityComparer<T>.Default, ItemIndex);
end;

class function TArray.Contains<T>(const Values: array of T; const Item: T): Boolean;
var
  ItemIndex: Integer;
begin
  Result := Contains<T>(Values, Item, ItemIndex);
end;

class function TArray.IndexOf<T>(const Values: array of T; const Item: T;
  const Comparer: IEqualityComparer<T>): Integer;
begin
  Contains<T>(Values, Item, Comparer, Result);
end;

class function TArray.IndexOf<T>(const Values: array of T; const Item: T): Integer;
begin
  Contains<T>(Values, Item, Result);
end;

我的静态类有一个整体的负载更多的功能,但这些都是你需要为此目的的人。

My static class has a whole load more functions, but these are the ones you need for this purpose.

这code依赖于默认的相等比较你的类型是适合的目的。这是一个简单的类型,比如 TPoint 的情况,但被prepared的惊喜,如果您使用更复杂的类型,其默认comparers是不够的。

This code relies on the default equality comparer for your type being fit for purpose. That is the case for a simple type like TPoint, but be prepared for surprises if you use more complex types for which the default comparers are insufficient.

这篇关于德尔福:&QUOT; INDEXOF&QUOT;在数组的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆