Talk:Data Structures

From Kolmafia
Revision as of 16:12, 11 March 2010 by imported>Grotfang
Jump to navigation Jump to search

I thought that the Records section would've been helpful in figuring out multidimensional maps -- maybe not; Veracity might've gone a bit too far with that example (which I understand, as she was trying to cover all her bases with one single unifying example). --Heeheehee 06:42, 10 March 2010 (UTC)

An attempt at explaining maps

A map consists of two types of component.

  • The key
  • The value

A normal map (without using a record) can have multiple keys, but only one value. A map is formed by specifying a normal datatype for the value and including a key, then giving the map a name:

datatype [key] name;

Eg.

string [int] message;

This in itself is a useful feature, but there are many situations where you may wish to associate two values. For example, in the message map above, I may wish to specify both a message and a name to send it to. There are two ways I could deal with this problem. The first would be to create a second map with a corresponding key. I could then assume that message[key] should be send to recipient[key]. While this works, it is inefficient and untidy, as well as inflexible.

Mafia supports the ability to customise maps using record. Record tells mafia what each entry in a map means, thereby enabling us to have more than one value to the map. The best way to visualise maps is to see a row on a page:

key   value

We can extend our keys already by specifying multiples:

value [key1 , key2] name;

Produces:

key1   key2   value

What record lets us do is add more values:

key   value1   value2

Each line in a record is the next value and has to include the datatype of that value. Before we specified the value's datatype when we named the map (since there was only one value to assign a datatype to). Now we have to make a record of ALL the datatypes that will come up, and then when we make our map we will use the record name in place of a single datatype:

record kmailer{
   string message;
   string recipient;
}
kmailer [int] my_messages;

The example above creates the following map:

key   value1    value2
int   message   recipient

So far, so simple. The next bit is where it gets interesting. We can no longer assign a value to the map as a whole. Without a record, we can do the following:

string [int] message;
message[1] = "Hi there!";

This makes the string "Hi there!" our value for key = 1:

key   value
1   Hi there!

With a record, we can no longer assign a value to a key alone; we have more than one value and we need to specify which one it is we want to assign something to. So using the record above, to assign a message we would have to do the following:

record kmailer{
   string message;
   string recipient;
}
kmailer [int] my_messages;
my_messages[1].message = "Hi there!";
my_messages[1].recipient = "Grotfang";

Both values are assigned to our "1" key, but we must specify the value we are assigning. The following details what is going on in the map:

key   value1   value2
int   message   recipient
1   Hi there!   Grotfang

--Grotfang 16:12, 11 March 2010 (UTC)