I have a COWarray that works OK, but now I want to expand the number of dimensions like so:
type
TCOWArray2<T> = record
private
type
TItem = record
fItems: TArray<T>;
fStart, fFinish: NativeInt;
end;
private
fItems: array of TItem;
private
methods
public
....
end;
The array splits up its items in blocks. Every sub array has e.g. 100 items and the mean array has however many items are needed.
To the outside only a one dimensional array is presented, but internally the data of type T
is stored in the subarray.
This way I can have copy of write with very little copying going on when a single item changes. Instead of cloning all 20,000 items, only 100 items are clones plus the mean array with 200 items, i.e. only 300 items a nearly 99% reduction of effort and storage.
The problem is that I need to keep track of changes in the reference count of the main array and propagate those to the sub-arrays.
Something like:
procedure TCOWArray<T>.SomeMember.AddRef;
var
Item: TItem;
begin
inherited;
for Item in fItems do Item.IncreaseRefCount;
end;
Obviously for performance reasons I will use a plain for i loop
How do I do this?
I was thinking of adding a custom TInterfaced Object, but I'm not sure how to make it work.