How to get TFPGList pointer to item? - freepascal

I have list which is TFPGList<TmyRec> and I need pointer to item's record TMyRec with index N. Not entire record Items[N] but pointer to item's record. How to get this pointer?

You cant. the .List member that returned an array of pointer in the classic non-generic TFPList is replaced by a array[0..MaxGListSize] of T.
This means that if T is not a reference type (for example a class or a pointer to a record) then this array stores the full value for each item and not a pointer to the item, like in the old days.

Related

MySQL: Can we create an array (associative or not) as a MySQL server script variable? [duplicate]

This question already has answers here:
How to store arrays in MySQL?
(7 answers)
Closed 10 months ago.
I've seen a lot of SQL server script variables of this kind: #variable. But can we, in fact, store an array (associative or not) behind the #variable?
UPDATE
This question turns out to be a duplicate of this one, which suggests to consider possible using of:
SET type and JSON type, which seem to be only column types but not #variable types.
A TEMPORARY TABLE, which seem to be stored in HDD (right?).
Functions working with JSON strings (e.g., JSON_VALUE and JSON_LENGTH), which are usable entirely within MySQL server script. Although, these functions do not help to derive an array and store it in a #variable and are merely JSON walkarounds. I would accept this variant but it seems like #json_string is parsed each time we call JSON_VALUE(#json_string).
So, till now it seems that there IS an opportunity to CREATE an array (associative or not!) but there IS NO an opportunity to surely KEEP the array for its further processing!
Regarding the question mentioned in the beginning of this one. Right now I've only reached 5th and 6th answers, which are related to JSON strings. They are interesting! Be sure to check them out if you're interested in the subject!
Thanks to everyone for your time!
UPDATE
As #Panagiotis Kanavos has mentioned, fetching data by value is slower in case of arrays.
But what if:
We indeed want to simply iterate over M input arrays simultaneously and produce N output arrays? (Maybe, we are simply interested in collation of parameters along a timeline and keep the results.) Arrays are perfectly suitable for this task. But of course, in this case we can still use tables. The question is what will be faster? If our iterative process involves many requests to arrays' elements (can we rely on the server caching the M input arrays and that they'll always be at hand?) and creation of multiple result arrays (how long will it take in case of tables and how do we know that tables are created in RAM for fast access?)?
We want to create an array manually along the course of a server script and are going to only use it in C-like style (aren't going to fetch its data by value) and after the script execution there'll be no need in the array? So, this will be a classic C-like script-only array. To me, in this case putting the array directly into the RAM is what we need and will be more effective than table creation (which'll probably go to HDD), won't it?
And so, the 2nd (and more general) question arises: How far can we rely on the server's optimizations?
I understand that a huge work's been put in optimization in many ways. But has somebody met a situation when a server didn't optimize in the best way? When a programmer had to explicitly rearrange the code in order to manually bring it to the optimal state?
MySQL will implement a data type, ARRAY, to store
variable-sized arrays, in compliance with Standard
SQL (SQL:2003) array functionality.
Syntax
Add a new column data type:
ARRAY [[ may be any data type supported (except
for ARRAY itself, and REF, which MySQL does not support).
It defines the type of data that the array will contain.
-- The [] must be an unsigned integer
greater than zero. It defines the maximum cardinality of
the array, rather than its exact size. Note that the inner
set of brackets is mandatory when defining an array with
a specific size, e.g. INT ARRAY is correct for defining an
array with the default size, INT ARRAY[5] is the correct syntax
for defining an array that will contain 5 elements.
-- As shown in the syntax diagram, []
is optional. If omitted, the maximum cardinality of the
array defaults to an implementation-defined default value.
Oracle's VARRAY size is limited to the maximum number of
columns allowed in a table, so I suggest we make our default
match that maximum. Thus, if [] is omitted,
the maximum cardinality of the array defaults to 1000, which
should also be the absolute maximum cardinality. Thus:
-- [] defaults to 1000.
-- [] may range from 1 to 1000.
Function
An array is an ordered collection of elements, possibly
containing data values. The ARRAY data type will be used
to store data arrays in database tables.
Rules
-- An array is an ordered set of elements.
-- The maximum number of elements in the array is known as
the array's maximum cardinality. The maximum cardinality is
defined at the time the array is defined, as is the element
data type.
-- The actual number of elements that contain data values is
known as the array's cardinality. The cardinality of an array
may vary and is not defined at the time the array is defined.
That is, an instance of an array may always contain fewer
elements than the maximum cardinality allows.
-- Each element may contain a data value that corresponds
to the array's defined data type.
-- Each element has three states: blank (no value assigned
to the element), NULL (NULL assigned to the element), and
containing the valid value (data value assigned to the element).
-- Each element is associated with exactly one ordinal position
in the array. The first array element is found at position 1 (one),
the next at position 2 (two), and so on. Thus, assuming n is the
cardinality of an array, the ordinal position of an array element
is an integer in the range 1 (one) <= element <= n.
-- An array has a maximum cardinality and an actual cardinality.
-- It is an error if one attempts to assign a value to an array
an element whose position is greater than the maximum cardinality of
the array.
-- It is not an error if one attempts to assign values to only
some of an array's elements.
-- Privileges:
-- No special privileges are required to create a table
with the ARRAY data type, or to utilize ARRAY data.
-- Comparison:
-- See WL#2084 Add the ability to compare ARRAY data.
-- Assignment:
-- See WL#2082 Add ARRAY element reference function and
LW #2083 Add ARRAY value constructor function.
Other statements
-- Two other syntax elements must be implemented for
the ARRAY data type to be useful. See WL#2082 for
Array Element Reference syntax and WL#2083 for Array Constructor
syntax.
-- Also related:
-- CARDINALITY(). See WL#2085.
-- Array concatenation. See WL#.
An example
Create a table with the new data type:
CREATE TABLE ArrayTable (array_column INT ARRAY[3]);
Insert data:
INSERT INTO ArrayTable (array_column)
VALUES (ARRAY[10,20,30]);
Retrieve data:
SELECT array_column from ArrayTable
WHERE array_column <> ARRAY[];
-- Returns all cases where array_column is not an empty array
Reserved words
ARRAY, eventually CARDINALITY

Why Octave shows wrong dimension of this char array

I don't understand why this array:
Type = Reflective Event, Reflective Event, Reflective Event
according to Octave's variable editor is a 3x19 char array.
In the variable editor, Type is shown as follows (I don't know if that's helpful)
TYPE =
Reflective Event
Reflective Event
Reflective Event
The thing is odd, in fact when I code disp(size(TYPE)) I get 3 19 accordingly.
Now, the even wierder thing is that, if I then code size(TYPE,1) which should give the size of TYPE's first dimension, I instead get 1 1.
Can you guys help me figure out what is wrong here?
Just guessing, as the syntax in the post is invalid Octave:
Your variable is actually a matrix of chars. Note that a string is simply an array of independent chars. so
var1=" Reflective Event, ";
is a length(var1)==19 array, or a size(var1)==(1,19) array. Now, if you repeat that, you can either make a (1,19*3) array, or a (3,19) matrix, which I assume is what you have.
Now, as you did not know that each char is individual, you reached the wrong conclusion with your second test. When you index a matrix using linear indices (i.e. a single index), it will "unroll" the array and give you that single elements.
i.e. A=[1 2 3; 4 5 6; 7 8 9] is a 3x3 matrix, but you can do A(2,1) or just A(4) to access the value 4.
In your test size(TYPE(1)) is (1,1), because TYPE(1)==" ", as TYPE(3)=="e" and so on.

How do I make action for certain instances of object?

I don't want to make action to the object in general
I have 8 instances for example (0,1,2,3,4,5,6,7)
I need to make the action on 2 , 5 and 7 only for example
how ??
This is something that really confused me about Construct 2 coming from a software engineering background.
Picking which instances of a Sprite to effect works sort of like filtering a database. You start with all the instances in a list, and then you filter them out using conditions. When possible, Construct 2 will automatically guess which instance you want. (Like if you just spawned Enemy at EnemySpawn, it will know your next reference Enemy is only the last one created).
This magically works most of the time, but you can select a set of objects with a few of these conditions:
Compare instance variable
Compare the current value of one of the object's instance variables.
Is boolean instance variable set
Test if one of the object's boolean instance variables is set to true. (Invert the condition to test if false.)
Pick by unique ID (UID)
Pick the instance matching a given unique ID (UID) number.
In my experience, adding an instance variable and setting it to keep track of a subset of Sprites or the state of the sprite is the cleanest way to limit an action to a specific group of sprites.
See also Object Expressions from the Scirra Manual:
You can add a 0-based object index to get expressions from different object instances. For example Sprite(0).X gets the first Sprite instance's X position, and Sprite(1).X gets the second instance's X position. For more information see index IDs (IIDs) in common features. You can also pass another expression for the index. Negative numbers start from the opposite end, so Sprite(-1).X gets the last Sprite's X position.
These are not "specific" object IDs (Construct 2 calls them Index IDs, or IIDs) but the refer to all the objects of that type in the order they were created.
So:
Enemy(0) is the first
Enemy(-1) is the most recent
Enemy(5) is the 6th Enemy created, ignoring destroyed Sprites (See below).
You have to keep in mind though, that if you destroy an object with IID 3, then all the objects after it will shift in the list and their IID will decrease by one. (Like a LinkedList data structure)
If you destroy instance D, the others shift
Instance |A|B|C|D|E|F| --> |A|B|C|E|F|
IID |0|1|2|3|4|5| --> |0|1|2|3|4|
Hopefully one of those methods can help you get to what you need!

Associative array with int as key

In my application I want to have a dictionnary where the key is an integer.
Since it's an integer, I use normal Array :
var arr : Array = [];
arr[5] = anObject;
arr[82] = anOtherObject;
When I iterate with for each, no problem, it iterates through those 2 object. The problem is that arr.length return 83... So I have to create a variable that count the number as I modify the array.
Question 1 : Is there a best practice for that (IE: associative array with int as key)? I hesitated to use a Dictionnary.
Question 2 : Does flash allocates memory for the unused buckets of the array?
Arrays in flash are sparse (unlike Vector), so the empty entries will not be allocated. If you need to know the length, you will probably need to keep track of it manually (make a wrapper class perhaps).
Adobe says:
Arrays are sparse arrays, meaning there might be an element at index 0 and another at index 5, but nothing in the index positions between those two elements. In such a case, the elements in positions 1 through 4 are undefined, which indicates the absence of an element, not necessarily the presence of an element with the value undefined.

Stream filter in cuda

I have an array of values and a linked list of indexes. Now, i only want to keep those values from the array that correspond to the indexes in the LL. is there a standard algorithm to do this. Please give example if possible
So, suppose i have an array 1,2,5,6,7,9
and i have a linked list 2->3
So, i want to keep the values at the index 2 and 3. That is keep 5 and 6.
Thus my function should return 5 and 6
In general, linked list is inherently serial. Having a parallel machine will not speed up the traversal of your list, hence the number of steps of your problem cannot go below O(n), where n is the size of the list.
However, if you have some additional way to access the list you can do something with it.
For example, all elements of the list could be stored in a fixed-size array (although, not necesairly in a consecutive way). List member could be represented in an array using the following struct.
struct ListNode {
bool isValid;
T data;
int next;
}
The value isValid sets if given cell in an array is occupied by a valid list member, or it is just an empty cell.
Now, a parallel algorithm would read all cells at once, check if it represents a valid data, and if so, do something with it.
Second part: Each thread, having a valid index idx of your input array A would have to mark A[idx] not to be deleted. Once we know which elements of A should be removed and which not - a parallel compaction algorithm can be applied.