Jump to content

  • Log In with Google      Sign In   
  • Create Account


Accessing 2 arrays using indices of one array [Problem]


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 lipsryme   Members   -  Reputation: 986

Like
0Likes
Like

Posted 19 June 2013 - 06:08 AM

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.


Edited by lipsryme, 19 June 2013 - 06:11 AM.


Sponsor:

#2 Dawoodoz   Members   -  Reputation: 290

Like
0Likes
Like

Posted 19 June 2013 - 06:22 AM

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]
}

Edited by Dawoodoz, 19 June 2013 - 06:32 AM.

My open source DirectX 10/11 graphics engine. https://sites.google.com/site/dawoodoz

"My design pattern is the simplest to understand. Everyone else is just too stupid to understand it."


#3 lipsryme   Members   -  Reputation: 986

Like
0Likes
Like

Posted 19 June 2013 - 06:34 AM

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;
}

Edited by lipsryme, 19 June 2013 - 06:38 AM.


#4 Dawoodoz   Members   -  Reputation: 290

Like
0Likes
Like

Posted 19 June 2013 - 06:42 AM

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;
        }
    }

Edited by Dawoodoz, 19 June 2013 - 06:44 AM.

My open source DirectX 10/11 graphics engine. https://sites.google.com/site/dawoodoz

"My design pattern is the simplest to understand. Everyone else is just too stupid to understand it."


#5 lipsryme   Members   -  Reputation: 986

Like
0Likes
Like

Posted 19 June 2013 - 06:54 AM

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 


Edited by lipsryme, 19 June 2013 - 07:07 AM.


#6 Dawoodoz   Members   -  Reputation: 290

Like
0Likes
Like

Posted 19 June 2013 - 07:20 AM

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].


Edited by Dawoodoz, 19 June 2013 - 07:20 AM.

My open source DirectX 10/11 graphics engine. https://sites.google.com/site/dawoodoz

"My design pattern is the simplest to understand. Everyone else is just too stupid to understand it."


#7 lipsryme   Members   -  Reputation: 986

Like
0Likes
Like

Posted 19 June 2013 - 07:33 AM

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;
	}


#8 Dawoodoz   Members   -  Reputation: 290

Like
0Likes
Like

Posted 19 June 2013 - 07:53 AM

 

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.


My open source DirectX 10/11 graphics engine. https://sites.google.com/site/dawoodoz

"My design pattern is the simplest to understand. Everyone else is just too stupid to understand it."


#9 Waterlimon   Crossbones+   -  Reputation: 2375

Like
0Likes
Like

Posted 19 June 2013 - 07:58 AM

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.


Waterlimon (imagine this is handwritten please)


#10 lipsryme   Members   -  Reputation: 986

Like
0Likes
Like

Posted 19 June 2013 - 08:38 AM

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?


Edited by lipsryme, 19 June 2013 - 08:39 AM.


#11 Waterlimon   Crossbones+   -  Reputation: 2375

Like
0Likes
Like

Posted 19 June 2013 - 09:50 AM

AB=AAA...ABBB...B

if the entity is of type B:

IndexInAB = AmountOfA + AmountOfBEntitiesBeforeEntity + 1 //i think

 

if its A:

IndexInAB = AmountOfAEntitiesBeforeEntity + 1

 

 

This is assuming that you add the A/B in the AB container each time you add a new entity, always at the end of all As or Bs, and that the entity you add will be the highest ID and that there are no gaps in the entity ID's

 

But if you are going for something dynamic your system isnt really going to work. It might work for some serialization but for other things not so well.


Waterlimon (imagine this is handwritten please)


#12 Waterlimon   Crossbones+   -  Reputation: 2375

Like
0Likes
Like

Posted 19 June 2013 - 09:53 AM

No wait i might have misunderstood the question.

 

In the code you posted with vectors A,B,C

 

when going over B you should remove the amount of A from the index and it should work.


Waterlimon (imagine this is handwritten please)


#13 lipsryme   Members   -  Reputation: 986

Like
0Likes
Like

Posted 19 June 2013 - 12:26 PM

That's exactly the problem. Let's consider I instantiate 3 of type A first. So these will have sceneIDs of (0,1,2).

Accessing them using these indices - amount of B works of course because B is 0.

Now when I instantiate one of B it's going to crash because the amount of B is now 1. So when going through the array above for all the A's (0, 1, 2) the 0 is going to become -1 when subtracting the amount of B's from it.



#14 Waterlimon   Crossbones+   -  Reputation: 2375

Like
0Likes
Like

Posted 19 June 2013 - 01:22 PM

So you have A0, A1, A2, B3

you can directly use the A indices.
But to use B index (3) you need to subtract amount of A, which gets you 0, the correct B index.

Waterlimon (imagine this is handwritten please)


#15 lipsryme   Members   -  Reputation: 986

Like
0Likes
Like

Posted 19 June 2013 - 01:31 PM

That's true but in return A would fail if you were to do the same for getting the A index, because then it would give you -1, 0, 1 as indices for 0, 1, 2 scene ID's.

I don't think my design will work at all. If there's a perfect and easy solution to calculate the exact Indices from the sceneID's then that would be great, but I'm probably going to have to change my design.

 

Update: Got it working. I redesigned it to store an std::pair<unsigned int, unsigned int> to always store the actual data ID in combination with the unique sceneID.

Was a few hours of work but this was the best solution.


Edited by lipsryme, 19 June 2013 - 03:11 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS