stata
2013-08-30 02:13:07
Just curious as to why, when dealing with TPoints, TPAs are always the go to. A lot of times we don't need to know the exact index a TPoint is located at right? Maybe a different structure would be more efficient in those cases (like linked lists, etc), rather than constantly dealing with dynamic arrays where the memory for the entire array must be reallocated when changing size.
Also, is there a generic, dynamic array that can be used for any type? If not, this would be a good addition.
Edit: Sorry about apostrophe misuse in the title
KingKong
2013-08-30 04:18:06
1) an array is the most efficient in terms of storage space compared to linked list(or anything else), not to mention the access speed
2) the array is constantly changing because the colors are constantly changing, linked list is no different since you have to change the entire list to reflect the updated colors
3) I don't see a use for a generic array in simba, but im pretty sure you can just use array of Tobject.
A TVariantArray can store any data type, though it's best to use the specific array type if the array members are of the same type (for reasons similar to the advantages of a strongly typed language)
Brandon
2013-08-30 09:17:34
In this case OP is right though..
Arrays are only efficient when on the stack.. Otherwise arrays are as efficient as a pointer to a block of memory on the heap.
Since Tpa's are obviously heap allocated an resizeable probably by using realloc or malloc and copy, then being able to allocate your own memory or structure space would be more efficient since you can reserve extra space (capacity) and keep track of size rather than alloc and copy every time you modify a tpa
Still you can just preallocate using large arrays and store what you want in it so another structure won't make a difference.
stata
2013-08-30 18:38:40
This is exactly what I was getting at. That's a good point about the preallocation though. I did not think of that.
A TVariantArray can store any data type, though it's best to use the specific array type if the array members are of the same type (for reasons similar to the advantages of a strongly typed language)
What I meant by "general" dynamic array was really a dynamic array of one type, but it can be any type (including custom). Basically, a dynamic array of void pointers. I don't see how this would NOT be useful (imagine a custom point type that included associated color or something like that).
Brandon
2013-08-30 18:43:23
From: stata
This is exactly what I was getting at. That's a good point about the preallocation though. I did not think of that.
What I meant by "general" dynamic array was really a dynamic array of one type, but it can be any type (including custom). Basically, a dynamic array of void pointers. I don't see how this would NOT be useful (imagine a custom point type that included associated color or something like that).
type MyType = record
X, Y: Integer;
end;
var
I: Integer;
Foo: Array of MyType; //Dynamic array must be allocated on heap before usage.
Bar: Array[0..100] of Integer; //Constant array of size 100. Stack Allocated.
Begin
SetLength(Foo, 100); //Allocate 100 "MyType" contiguous Blocks and Foo points to index 0.
For I := 0 To High(Foo) Do
Begin
Foo[I].X := I;
Foo[I].Y := I + 1;
End;
End;
stata
2013-08-30 18:51:25
Ahh thanks, I did not know that all arrays (without specifying size) declared with "Array of" are dynamic. I guess that makes sense after all; not sure what I was thinking