Search the web
Sign In
New User? Sign Up
netlogo-users · NetLogo Users
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Hear how Yahoo! Groups has changed the lives of others. Take me there.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Need advice implementing associative array   Message List  
Reply | Forward Message #2346 of 9893 |
Re: [netlogo-users] Need advice implementing associative array

Jim Lyons wrote:

> I need what amounts to an associative array. As it happens,
> both the key and value are short fixed length strings.
> My need is for something that will perform well with
> roughly 2000 entries. About 10 to 20 agents will have
> their own lists and will be using them constantly.
>
> 1) Make the entries in the array lists of key and value:
> [ [k1 v1] [k2 v2] ... ]
> 2) Make pairs of entries in the list for each key/value pair:
> [ k1 v1 k2 v2 ... ]
> 3) Concatenate each key/value pair into a single string
> and enter into list:
> [ k1v1 k2v2 ... ]
>
> It seems to me that (2) would be the most likely
> to have the best performance. Is this correct, or is
> there a better way to accomplish this?

There is at least one other way, and that is to use two lists.
key-list --> [ k1 k2 k3 k4 ]
value-list --> [ v1 v2 v3 v4 ]

to-report dict-item [ key ]
report item (position key dict-key) dict-value
end

Note that there's no error checking there, and if key doesn't exist in
dict-key a run-time error will occur when item tries to use the value
false to look up dict-value.

It would probably perform better than your method #2, since the list
that is searched is half as long, and no need to + 1. Also, you avoid
the problem (should it ever arise) of having a value that is identical
to a key.

Also, this method allows unmodified use of list primitives first and
last on the value list, and nearly direct use of lput, fput, but-first
and but-last.

Sorting and duplicate removal is possible, using map expressions.

Note that this method does not inherently prevent duplicate keys, and
that in the case of duplicate keys, only the first instance would ever
be returned.

so:
; assume all these are run by turtles
; assume turtles-own [
; dict-key ; list that contains dictionary keys
; dict-value ; list that contains dictionary values

to-report dict-item [ key ]
report item (position key dict-key) dict-value
end

to-report dict-first ;
report first dict-value
end

to-report dict-last
report last dict-value
end

to set-dict-but-first-dict
; performs but-first on both lists
set dict-key but-first dict-key
set dict-value but-first dict-value
end
; but-last is similar


to set-dict-lput [ key value ]
; performs lput on both lists
set dict-key lput key dict-key
set dict-value lput value dict-value
; optional call to procedure to sort or remove duplicates here
end
; fput is similar

to set-dict-sort-dict-value
; merge key and values into one list [ [ k1 v1] [k2 v2 ] ... ]
let combo map [ list ?1 ?2 ] dict-key dict-value
; sort that list by value
set combo sort-by [ last ?1 <= last ?2 ] combo
set dict-key map [ first ? ] combo
set dict-value map [ last ? ] combo
end
; remove duplicates is similar

; I didn't double check my syntax on map and sort-by, so it may be backwards...

> It also happens that the strings are all digits. Would it be better to
> convert them to numbers and store and retrieve them that way? I would
> have to convert them back to strings (or digits) for use. Do all
> numbers, even small integers, take up 8 bytes each? If so, it seems
> strings would be the way to go.

May be. Also, there may be some other ways to do this, even better...

Hope this helps!

~~James.

On Thu, 4 Nov 2004 22:33:12 -0500, Jim Lyons <jimlyons@...> wrote:
>
>
> I need what amounts to an associative array. As it happens, both the
> key and value are short fixed length strings. My need is for something
> that will perform well with roughly 2000 entries. About 10 to 20 agents
> will have their own lists and will be using them constantly.
>
> It seems I have three ways to implement it.
>
>
>
> Any insight appreciated.
>
> Jim Lyons
>
>
>
> Yahoo! Groups Links
>
>
>
>
>



Fri Nov 5, 2004 11:30 am

jps_netlogo
Offline Offline
Send Email Send Email

Forward
Message #2346 of 9893 |
Expand Messages Author Sort by Date

I need what amounts to an associative array. As it happens, both the key and value are short fixed length strings. My need is for something that will perform...
Jim Lyons
jimlyons37
Offline Send Email
Nov 5, 2004
4:22 am

... There is at least one other way, and that is to use two lists. key-list --> [ k1 k2 k3 k4 ] value-list --> [ v1 v2 v3 v4 ] to-report dict-item [ key ] ...
James Steiner
jps_netlogo
Offline Send Email
Nov 5, 2004
1:50 pm

... Jim> I need what amounts to an associative array. As it happens, both Jim> the key and value are short fixed length strings. My need is for Jim> something...
Seth Tisue
sethtisue
Offline Send Email
Nov 5, 2004
9:32 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help