繁体   English   中英

我可以使用运算符重载来创建指向通用类型的指针吗?

[英]Can I use operator overloading to create a pointer to a generic type?

我正在尝试解决此问题(以及其他一些问题。)

问题

//None of these compile
type
  PAType<T> = ^AType<T>;  
  P<T> = ^T;                
  PAType = ^AType<T>

因此,我尝试使用记录和运算符重载来实现自己的目标。

我正在编写以下代码:

  TCell<T> = record
  private
    FData: T;
    procedure SetData(Value: T); inline;
    function GetData: T; inline;
  public
    property data: T read GetData write SetData;
  end;

  //Type safe pointer to T, because it knows SizeOf(T).
  P<T> = record  //wrapper around pointer: ^TCell<T>;^H^H^H^H any <T> actually 
  private
    FPointerToT: pointer;
  public
    class operator Implicit(a: pointer): P<T>; inline;
    class operator Implicit(a: P<T>): pointer; inline;
    class operator Implicit(Cell: TCell<T>): P<T>; inline;
    class operator Implicit(p: P<T>): TCell<T>; inline;
    class operator Add(a: P<T>; b: NativeUInt): P<T>; inline;
    class operator NotEqual(a,b : P<T>): Boolean; inline;
    class operator NotEqual(a: P<T>; b: pointer): Boolean; inline;
    class operator Equal(a,b : P<T>): Boolean; inline;
    class operator GreaterThan(a,b : P<T>): Boolean; inline;
    class operator GreaterThanOrEqual(a,b : P<T>): Boolean; inline;
    class operator LessThan(a,b : P<T>): Boolean; inline;
    class operator LessThanOrEqual(a,b : P<T>): Boolean; inline;
    class operator Inc(a: P<T>): P<T>; inline;
    class operator Dec(a: P<T>): P<T>; inline;
    class operator Explicit(a: P<T>): T; inline;
  end;

我正在写一个哈希表。 因为我正在尝试不同的哈希选项。
哈希表应获取包含数据的记录,将记录放入动态数组(记录本身, 而不是指针)中,并返回指向该记录的指针。

这将允许应用程序以或多或少的顺序存储数据(有一些间隔)。这对缓存很有用。
我想使用泛型,因为即使在任何时候哈希表仅持有一种类型,在不同的哈希表中也要哈希不同的类。

通过返回指针,可以防止双重存储。

上面未完成的结构使我可以像这样编写代码:

//FCells: array of T;
//FArrayEnd: pointer; //points to element FCells[max_elements+1] (i.e. access violation)  

function THashTable<K, T>.NextItem(Item: P<T>): P<T>;
begin
  Result:= Item + SizeOf(T);  //pointer arithmetic 
  if Result >= FArrayEnd then Result:= @FCells[0];  //comparison and assignment
end;

function THashTable<K, T>.Lookup(const key: K): P<T>;
var
  Index: NativeUInt;
  ItemKey: K;
begin
  if IsValid(key) then begin
    // Check regular cells
    Index:= First_Cell(FGetHashFromKey(key));  //FGet.. is a user supplied key generation proc.
    while (true) do begin
      ItemKey:= FGetKey(FCells[Index]);
      if (IsEqual(ItemKey, key)) then exit(@FCells[Index]);
      if (IsEmpty(ItemKey)) then exit(nil);  //nil pointers denote no-hit 
      Index:= NextIndex(Index);
    end;
  end
  else { if IsEmpty(key) then } begin
    // Check zero cell
    Result:= @FZeroCell;
  end;
end;

请注意,我不需要Nullable<T>表示未命中。 我标准的nil指针有效。

我不必键入强制类型转换,并且指针知道它有多大。
它甚至知道什么有点T的。

我知道泛型有很多陷阱,所以:

在我深入之前。
这项工作(原则上)还是这种方法只是一厢情愿?

您可以具有指向通用类型的指针。 像这样:

type
  THashTable<K, T> = class
  public type
    TCell = TCell<T>;
    PCell = ^TCell;
  public
    function NextItem(Item: PCell): PCell;
  end;

要实现指针算法,您将需要以下代码:

function THashTable<K, T>.NextItem(Item: PCell): PCell;
begin
  Result := Item;
  inc(Result);
end;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM