Accessing 2 arrays using indices of one array [Problem]

Started by
13 comments, last by lipsryme 10 years, 11 months ago

I'm having a problem that I can't seem to find a solution to, without having to rewrite everything.

Let me try to explain the problem:

I've got 2 object's data types, let's call them A and B.

I'm storing my collection of both of them inside an array (seperately).

Now those two are just for the actual data that I need. There's also another array of just indices which I use to uniquely define my objects inside the scene so they're being assigned to my objects of type A and B during instantiation (0, 1, 2 ,3 ,4, ...).

The problem I'm having right now is in the end what my function is getting is these unique IDs which then have to access the actual data inside the A and B arrays. When just instantiating one of the types A or B this works, but if I have two different arrays, and a single array with indices, how could I access those, using the indices of the unique scene ID array.

I hope this wasn't too confusing tongue.png

Basically I have two different arrays that I need to access with their corresponding ID from a single array.

I have the information that the unique ids are either of type A or B and I have their corresponding array's sizes.

I just don't know how to get the corresponding ID into their arrays.

Advertisement

Subtract the size of one element to each array's pointer to initiate them at base 1.

Now you can use 0 for a null index, positive values for array A and negative values for array B.


If (index == 0) {
    // Raise exception
} else if ( index > 0) {
    Use A[index]
} else {
    Use B[-index]
}

Hmm I'm not quite sure I understand...

I made a simple code example for my problem, maybe it'll make it clearer:


int main()
{
	std::vector<unsigned int> A;
	std::vector<unsigned int> B;
	std::vector<unsigned int> C;

        // Instantitate object of type A
	unsigned int sceneIDCounter = 0;
	A.push_back(sceneIDCounter); // data inside here doesn't matter
	C.push_back(sceneIDCounter);
	sceneIDCounter++;

        // Instantiate object of type B
	B.push_back(sceneIDCounter); // data inside here doesn't matter
	C.push_back(sceneIDCounter);
	sceneIDCounter++;



	// Now we assume that not every object is being rendered
	// and we're getting an array of unique scene indices to render
	// I've got C (culledSceneIDs) but need to access the corresponding indices inside A and B

	
	// Do A here...
	unsigned int culledSceneIDsA[1] = {0};
	for(int i = 0; i < 1; i++)
	{
		std::cout << A[culledSceneIDsA[i]] << std::endl;
	}



	// Do B here...
	unsigned int culledSceneIDsB[1] = {1};
	for(int i = 0; i < 1; i++)
	{
                // In this example this line would fail because the unique scene ID I'm getting is 1 but the actual data inside B would be index 0
		std::cout << B[culledSceneIDsB[i]] << std::endl; 
	}


	return 0;
}

If you are using a predefined vector type, you can subtract 1 from the index during access but it will be harder to understand the meaning of the index.


    // Do A and B here...
    unsigned int culledSceneIDs[2] = {1, -1};
    for(int i = 0; i < 2; i++)
    {
        int index = culledSceneIDs[i];
        if (index == 0) {
            // Raise error
        } else if (index > 0) {
            std::cout << A[index - 1] << std::endl;
        } else {
            std::cout << B[(-index) - 1] << std::endl;
        }
    }

I'm not sure this would work.

The function that is doing the access is being called once for an array of sceneIDs of either type A or B so I can't loop over both at once and branch.

And second the culledSceneIDs can never be negative, they are defined as [0, INF] and increment after each instantiation of either an object A or B.

So let's say I've got 10 objects inside my scene, which translates to a storage of 10 unique scene IDs (going from 0 to 9).

Now the function that wants to access the actual data gets an array of those that are not culled, so basically I'm getting something like:

culledSceneIDs[4] = { 0, 5, 2, 1 }

Which I know are of either type A or B. But I don't know at what index these are inside the actual data array.

These 10 objects I've got in my scene could be 3 of type A and 7 of type B, meaning that the data array of A would be from 0-2 and the data array of B from 0-6.

Now I have to somehow translate the unique scene ID to the actual data id.

Assuming the above sceneIDs are of type A, that means the first object instantiated was of type A, then came 4 objects of type B and the fifth instantiation was type A again.

But of course the actual data of this fifth would be at index 1, not 5.

In this case you could say, sure just subtract the size of B from the sceneID. But now you'd have the problem that when you're processing the first sceneID (0) you would get a negative value of -4 instead of the actual 0

You subtract the size of one element from the pointer like this:


Array = (myType*)((unsigned int)Array - sizeof(Array[0]))

Now Array[1] is the new Array[0].

Am I doing something wrong?


	// Do B here...
	unsigned int culledSceneIDsB[1] = {0};

	unsigned int* newArray = (unsigned int*)((unsigned int)culledSceneIDsB - sizeof(culledSceneIDsB[0]));

	for(int i = 0; i < 1; i++)
	{
		std::cout << B[newArray[i]] << std::endl;
	}

Am I doing something wrong?


	// Do B here...
	unsigned int culledSceneIDsB[1] = {0};

	unsigned int* newArray = (unsigned int*)((unsigned int)culledSceneIDsB - sizeof(culledSceneIDsB[0]));

	for(int i = 0; i < 1; i++)
	{
		std::cout << B[newArray[i]] << std::endl;
	}

The offset should be applied to the B array and the indices should use -1 to reach the first element of B.

So you have an array of scene object ID's:

1,2,3,4,5

And each has a corresponding type that you know:

A,A,B,B,A

And youve got the type storage:

A,A,A,B,B

So to get the data of a entity, you need to:

1.Calculate how manyth [Data type] of all entities it is (how many entities with the same data type are before it)

2.If it is B, you need to know how many A in total there are.

So, you need to keep track of how many A there are in total. That should be easy.

But you also need to calculate how many entities with the same type there are before the current entity.

This can be done by either:

1. Iterating through the entities in order (1,2,3,4...) and counting at the same time. You might be able to do this while culling (count while checking visibility) but it will not work for all methods of culling (eg. ones that dont iterate over all entites in order). Basically you then save the preceding entities of same type with the visible entity entry.

2. Iterate over all entities in order and for each save how many preceding entities of the same type there are. Same as above but this adds memory overhead for each entity, but only needs to be done for new entities and when an entitys type is changed.

o3o

I'm still not quite sure how the algorithm for the lookup would be assuming I'd know how many object's of the same type are before the index I'm trying to access. (And you are talking about the array that has all unique scene IDs stored, when you talk about the entities?)

I know how many items of each type there are in the scene of course.

Could you give me an example?

This topic is closed to new replies.

Advertisement