Skip to main content
Participant
April 17, 2008
Question

Associative array with unsigned integers

  • April 17, 2008
  • 4 replies
  • 674 views
Hello guys!

I have an associative array that is used in a special way: its keys are IDs as unsigned integers, but their order is random and some IDs are large numbers. That's why I have used an associative array. So, 2 questions:
1) Is this ok to use associative arrays this way? Because all I've read about associative arrays is that you usually use them with strings or objects, not int.
2) Is it fast to retrieve a value, given the key? Is it really hashed to be fast?

Thanks a lot!

Barbara
This topic has been closed for replies.

4 replies

kglad
Community Expert
Community Expert
April 21, 2008
good question, but no, flash doesn't reserve memory for unused array elements:

var a:Array = [];
a[10000000] = "HI";

uses the same memory as

var a:Array = [];
a[0] = "HI";
Participant
April 22, 2008
Mmmh, that's very interesting!
In that case, I guess I'll switch to an Array instead of an Associative one. I'm more of a C/C++ coder, which might explain why my original intuition told me not to use an Array.
Thanks a lot for your input!

Barbara.
kglad
Community Expert
Community Expert
April 21, 2008
you don't need to use successive indexes in an array.

you don't have to iterate over all indexed array elements. you can also use the for-in construct using an array, as well as an object.

so, those are not advantages. on the other hand, if everything's working i don't see any reason to change it.
Inspiring
April 21, 2008
In actionscript, does a regular indexed array allocate memory for the unused members? e.g. does doing myArray[1000000] allocate memory for items 0->999999? I know, for example, in c that using an array for the purpose of storing 10 items with uint keys that could be anywhere from 0 to 1000000 would be a bad idea, as you'd have to predeclare the array variable (int myArray[1000000] and you'd end up eating up a megabyte.

But I imagine that in actionscript, where everything else seems to be dynamically allocated, and it doesn't know what you're going to store in the array anyways, it could be different.

kglad
Community Expert
Community Expert
April 19, 2008
that should work (assuming myObject is a display object).

but it's a little screwy: it's an indexed array disguised as an associative array by which i mean it has numeric indices just like an indexed array and will work like an indexed array.

but there are two drawbacks to that approach:

1. you can't use the array methods and properties and

2. i can't see any advantage to that over an indexed array.

is there some advantage that you think you're achieving?
Participant
April 21, 2008
Hello again,

I initially chose to use an associative array because the IDs I use as keys are not necessarily successive. What I mean is that I will have about 100 IDs ranging between 0 and 20'000. It's a waste to use an array that has a length of 20'000 if there is only 100 IDs actually used in it, no? What do you think?
On the other hand, new IDs are added and some are deleted dynamically. With an associative array, I can iterate over all its elements without having to check whether a key's value is null. With a normal array, I would have to iterate over all the 20'000 elements and check that each of them doesn't point to a null value before working with it.

Thanks for the help :-) Let me know if my reasoning is wrong!

Barbara
kglad
Community Expert
Community Expert
April 17, 2008
1) i think you're probably just using the uint instance as your key, NOT its value. so, you're using an object as the key, not a uint.

you can show your code if you want more info.

2) fast compared to what? it's faster than searching through an indexed array.
Participant
April 19, 2008
public var myMap:Object = new Object();
Ok, I'm not sure I understand the difference... Here is the code:

public var myMap:Object = new Object();

public function addToMap( id:uint ):void {
var myObject:MyObject = new MyObject();
addChild(myObject);
myMap[id] = myObject;
}

Please, let me know if this is a correct use for an associative array :-) Thanks!

Barbara