Difference between revisions of "Talk:Data Structures"

From Kolmafia
Jump to navigation Jump to search
imported>Grotfang
(Description of map)
imported>Grotfang
m
Line 8: Line 8:
 
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:
 
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;
+
<pre>datatype [key] name;</pre>
  
Eg. string [int] message;
+
Eg.
 +
<pre>string [int] message;</pre>
  
 
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.
 
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.
Line 16: Line 17:
 
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:
 
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
+
<pre>key  value</pre>
  
 
We can extend our keys already by specifying multiples:
 
We can extend our keys already by specifying multiples:
  
key1  key2  value
+
<pre>key1  key2  value</pre>
  
 
What record lets us do is add more values:
 
What record lets us do is add more values:
  
key  value1  value2
+
<pre>key  value1  value2</pre>
  
 
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:
 
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{
+
<pre>record kmailer{
 
   string message;
 
   string message;
 
   string recipient;
 
   string recipient;
 
}
 
}
kmailer [int] my_messages;
+
kmailer [int] my_messages;</pre>
  
 
The example above creates the following map:
 
The example above creates the following map:
  
key  value1    value2
+
<pre>key  value1    value2
int  message  recipient
+
int  message  recipient</pre>

Revision as of 15:53, 11 March 2010

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 slice/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:

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