473,662 Members | 2,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Running array and hash in parallel

I'm using an array to store map features (name, lat, lon, caption,
etc), from which the user can then select an individual feature. The
problem is that when thousands of features are stored in the array,
looping over the entire array looking for a match is SLOW.

So I'm running a hash in parallel, where every time a feature is
pushed onto the array it's name is also added to the hash as an
identical key value pair. I then check if the key is defined in the
hash, and if it is, I want to use that feature's values from the
array. Problem is, I don't know the index number in the array for this
feature. Is there a way to look this up without looping over it, by
matching values between the array and hash?

Eg:

featureArray = new Array();
featureHash = new Object();
//assume lots of features pushed
featureArray.pu sh({name:name, lat:lat, lon:lon, caption:caption });
featureHash[name] = name;
//more features pushed

function useSelectedFeat ure (name) {
if (featureHash[name] != undefined) {
//match featureArray.na me to featureHash[name]
//use matched featureArray values
}
}

Thanks.

Mar 3 '07 #1
9 1840
On 3 Mar, 00:38, "IamIan" <ian...@gmail.c omwrote:
I'm using an array to store map features (name, lat, lon, caption,
etc), from which the user can then select an individual feature. The
problem is that when thousands of features are stored in the array,
looping over the entire array looking for a match is SLOW.

So I'm running a hash in parallel, where every time a feature is
pushed onto the array it's name is also added to the hash as an
identical key value pair. I then check if the key is defined in the
hash, and if it is, I want to use that feature's values from the
array. Problem is, I don't know the index number in the array for this
feature. Is there a way to look this up without looping over it, by
matching values between the array and hash?

Eg:

featureArray = new Array();
featureHash = new Object();
//assume lots of features pushed
featureArray.pu sh({name:name, lat:lat, lon:lon, caption:caption });
featureHash[name] = name;
//more features pushed

function useSelectedFeat ure (name) {
if (featureHash[name] != undefined) {
//match featureArray.na me to featureHash[name]
//use matched featureArray values
}

}

Thanks.
You are implementing a basic database here, does your app require you
not to use one already pre-written, such as MySql, if you /can/ use
mysql do so... instead of pushing data to the js array, push it back
to the server, the performance hit of 100ms retrieving the row you
need when you query the database which has 100 million rows in it will
be negligible even coupled with a slow connection, this way you don't
reinvent the wheel learning about the best hash and how to make it all
speedy. Think google maps here. SOme parts are cached, others are
released depending on context and liklihood you will need that data.

Mar 3 '07 #2
On Mar 3, 10:38 am, "IamIan" <ian...@gmail.c omwrote:
I'm using an array to store map features (name, lat, lon, caption,
etc), from which the user can then select an individual feature. The
problem is that when thousands of features are stored in the array,
looping over the entire array looking for a match is SLOW.

So I'm running a hash in parallel
You mean an object.
, where every time a feature is
pushed onto the array it's name is also added to the hash as an
identical key value pair. I then check if the key is defined in the
hash, and if it is, I want to use that feature's values from the
array. Problem is, I don't know the index number in the array for this
feature. Is there a way to look this up without looping over it, by
matching values between the array and hash?

Eg:

featureArray = new Array();
featureHash = new Object();
//assume lots of features pushed
featureArray.pu sh({name:name, lat:lat, lon:lon, caption:caption });
The new element's index will be featureArray.le ngth - 1.
featureHash[name] = name;
Why not store the feature's data in the object:

featureObj = {
name: {lat: lat, long: long, caption: '...'},
...
}

So you get:

featureOb[name].lat = ...
featureOb[name].long = ...
...

Why do two look-ups when one will do?
//more features pushed

function useSelectedFeat ure (name) {
if (featureHash[name] != undefined) {
var feature;
if (featureObj[name]){
feature = featureObj[name];
/* use feature's properties */
}
You can probably abbreviate that to:

if (feature = featureObj[name]) {
/* use feature's properties */
}
--
Rob

Mar 3 '07 #3
On Mar 3, 8:08 am, "RobG" <r...@iinet.net .auwrote:
On Mar 3, 10:38 am, "IamIan" <ian...@gmail.c omwrote:
I'm using an array to store map features (name, lat, lon, caption,
etc), from which the user can then select an individual feature. The
problem is that when thousands of features are stored in the array,
looping over the entire array looking for a match is SLOW.
So I'm running a hash in parallel

You mean an object.
, where every time a feature is
pushed onto the array it's name is also added to the hash as an
identical key value pair. I then check if the key is defined in the
hash, and if it is, I want to use that feature's values from the
array. Problem is, I don't know the index number in the array for this
feature. Is there a way to look this up without looping over it, by
matching values between the array and hash?
Eg:
featureArray = new Array();
featureHash = new Object();
//assume lots of features pushed
featureArray.pu sh({name:name, lat:lat, lon:lon, caption:caption });

The new element's index will be featureArray.le ngth - 1.
featureHash[name] = name;

Why not store the feature's data in the object:

featureObj = {
name: {lat: lat, long: long, caption: '...'},
...
}

So you get:

featureOb[name].lat = ...
featureOb[name].long = ...
...

Why do two look-ups when one will do?
//more features pushed
function useSelectedFeat ure (name) {
if (featureHash[name] != undefined) {

var feature;
if (featureObj[name]){
feature = featureObj[name];
/* use feature's properties */
}

You can probably abbreviate that to:

if (feature = featureObj[name]) {
/* use feature's properties */
}

--
Rob
Hey,

You can use the array of objects as a hash itself (assuming name is a
unique property). Something like that:

featureArray = new Array();
featureArray[_name_of_featur e_as_string] = {lat:lat, lon:lon,...};

Then checking if a particular feature exists is as simple as this:

if ( featureArray[_name_of_featur e_as_string] )

and getting to its properties:

featureArray[_name_of_featur e_as_string].lat
featureArray[_name_of_featur e_as_string].lon

Let me know if this helps.

Mar 4 '07 #4
On Mar 4, 6:02 pm, "varun" <varungupt...@g mail.comwrote:
On Mar 3, 8:08 am, "RobG" <r...@iinet.net .auwrote:
On Mar 3, 10:38 am, "IamIan" <ian...@gmail.c omwrote:
I'm using an array to store map features (name, lat, lon, caption,
etc), from which the user can then select an individual feature. The
problem is that when thousands of features are stored in the array,
looping over the entire array looking for a match is SLOW.
So I'm running a hash in parallel
[...]
Why not store the feature's data in the object:
[...]
Hey,

You can use the array of objects as a hash itself (assuming name is a
unique property). Something like that:

featureArray = new Array();
featureArray[_name_of_featur e_as_string] = {lat:lat, lon:lon,...};

Then checking if a particular feature exists is as simple as this:

if ( featureArray[_name_of_featur e_as_string] )

and getting to its properties:

featureArray[_name_of_featur e_as_string].lat
featureArray[_name_of_featur e_as_string].lon

Let me know if this helps.
No, it doesn't. Arrays should be used as arrays, not plain objects -
that's what Objects are for.

Using an Array as a plain object is discourage because you run the
risk that one of the properties you add will conflict with an existing
Array property, particularly where you have no control over the names
of the properties being added.
--
Rob

Mar 4 '07 #5
On Mar 4, 3:54 pm, "RobG" <r...@iinet.net .auwrote:
On Mar 4, 6:02 pm, "varun" <varungupt...@g mail.comwrote:
On Mar 3, 8:08 am, "RobG" <r...@iinet.net .auwrote:
On Mar 3, 10:38 am, "IamIan" <ian...@gmail.c omwrote:
I'm using anarrayto store map features (name, lat, lon, caption,
etc), from which the user can then select an individual feature. The
problem is that when thousands of features are stored in thearray,
looping over the entirearraylook ing for a match is SLOW.
So I'm running ahashinparallel
[...]
Why not store the feature's data in the object:
[...]
Hey,
You can use thearrayof objects as ahashitself (assuming name is a
unique property). Something like that:
featureArray = newArray();
featureArray[_name_of_featur e_as_string] = {lat:lat, lon:lon,...};
Then checking if a particular feature exists is as simple as this:
if ( featureArray[_name_of_featur e_as_string] )
and getting to its properties:
featureArray[_name_of_featur e_as_string].lat
featureArray[_name_of_featur e_as_string].lon
Let me know if this helps.

No, it doesn't. Arrays should be used as arrays, not plain objects -
that's what Objects are for.

Using anArrayas a plain object is discourage because you run the
risk that one of the properties you add will conflict with an existingArraypr operty, particularly where you have no control over the names
of the properties being added.

--
Rob

Rob,

You're spot on about distinguishing associative arrays from numbered
arrays. Objects should be used for the first and Arrays for the
second. But Arrays are essentially Objects with a thin layer of
functionality built over. In your solution don't you still run the
risk of new properties conflicting with the existing Object
properties?

Is it a good idea to prefix an underscore to the property names in
order to avoid any conflict?

Varun

Mar 5 '07 #6
On Mar 5, 8:14 pm, "varun" <varungupt...@g mail.comwrote:
On Mar 4, 3:54 pm, "RobG" <r...@iinet.net .auwrote:
[...]
Using anArrayas a plain object is discourage because you run the
risk that one of the properties you add will conflict with an existingArraypr operty, particularly where you have no control over the names
of the properties being added.
Rob,

You're spot on about distinguishing associative arrays from numbered
arrays. Objects should be used for the first and Arrays for the
second. But Arrays are essentially Objects with a thin layer of
functionality built over.
Yes.
In your solution don't you still run the
risk of new properties conflicting with the existing Object
properties?
Yes, but the chances are lower. Object and its prototype have only 8
native properties: prototype, constructor, toString, toLocaleString,
valueOf, hasOwnProperty, hasOwnPropertyO f and propertyIsEnume rable.
Array has a whole bunch more.

Is it a good idea to prefix an underscore to the property names in
order to avoid any conflict?
Sounds OK to me; you could chose any valid character that isn't the
first character of a standard property name, say "X" or "A" or
whatever.

--
Rob

Mar 5 '07 #7
On Mar 5, 12:14 pm, "RobG" <r...@iinet.net .auwrote:
On Mar 5, 8:14 pm, "varun" <varungupt...@g mail.comwrote:
<snip>
>Is it a good idea to prefix an underscore to the property
names in order to avoid any conflict?

Sounds OK to me; you could chose any valid character that isn't the
first character of a standard property name, say "X" or "A" or
whatever.
Underscore is the first character in a set JavaScript(tm) extensions,
such as - __proto__ -, so may not be the best choice. An unusual
character sequence may make a better prefix, possibly including a
space character and something in the unicode range above 256.

Variable prefixes have also been suggested, such as the decimal
characters of a current (at the point of first recording it)
millisecond time. That prefix is extremely unlikely to coincide with
anything unexpected but still exposed in a javascript implementation,
and even if it does it will only do some once over a very long period.

Richard.

Mar 5 '07 #8
Thanks for the responses. I had thought of using the associative array
exclusively, but was curious if my original question was possible.
Sounds like it isn't. My last question concerns associative array use
with AJAX results.

If I try:

featureHash = new Object();
var name = AJAX result
featureHash[name] = name;

If fails, but if I modify the last line to be:

featureHash['"' + name + '"'] = '"' + name + '"';

It is successful. I confirmed my AJAX results are strings with typeof,
so I'm unclear on this one. Not a big deal but I'm interested in the
answer.

Thanks!

Mar 5 '07 #9
VK
On Mar 5, 1:14 pm, "varun" <varungupt...@g mail.comwrote:
But Arrays are essentially Objects with a thin layer of
functionality built over.
Yeah, right. And marijuana is really a tree, not a herb - they just
never let it grow big enough...

Object structure in javascript is expandable at run-time, so you can
use Object instance as dynamic storage of key/value elements (approx.
hash) . It is not an Object exclusive feature, with the same success
you can use any other instance in javascript, say
var hash = new Function;
hash['foo'] = bar;
The object is chosen because of the possibility of {"foo":"bar" }
constructs and because it has the minimum of native methods to worry
of being overriden.

It has nothing to do with the functional distinction of associative
array (map, hash, disctionary) and array.

Mar 6 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2016
by: R. Rajesh Jeba Anbiah | last post by:
I could see that it is possible to have hash array using objects like var hash = {"a" : "1", "b" : "2"}; Couldn't still findout how to declare hash array in Array. var arr = new Array("a" : "1", "b" : "2"); doesn't work. Any hints? TIA -- <?php echo 'Just another PHP saint'; ?> Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/
47
5061
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they are not at all. If you are doing *array", then you have to use only integer values for array index, as it was since ALGOL.
22
4607
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and hash is the base for it. I summarized all points in this article:...
35
6623
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
5
3513
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function accepts a void* key argument, and uses the functions above to store this argument. It returns something (linked to the key) that the caller can store a value in. The actual key argument is always of the same pointer type (as seen in the caller,...
4
13397
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to be read and the string representation of the byte array has to be converted back to the original byte array and converted back to the original string - confused yet? in psuedo
104
16929
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from that array Could you show me a little example how to do this? Thanks.
44
6697
by: gokkog | last post by:
Hi there, There's a classic hash function to hash strings, where MULT is defined as "31": //from programming pearls unsigned int hash(char *ptr) { unsigned int h = 0; unsigned char *p = ptr;
7
11775
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) Your first task is to write a program which reads this file into two parallel arrays in memory. One array contains the titles, and the other array contains the authors. The arrays are 'parallel' in the sense that the n-th element of the authors...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8547
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.