Primus2\Falcraft: New Data Type (map)

IMPORTANT!

This code is obsolete and has been moved to another programming project, asherwunk/phabstractic.  For more current information on this data type please see this post.

The Map data type is based on associative arrays.  An associative array is, as stated in Wikipedia:

In computer science, an associative arraymapsymbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears just once in the collection.

Operations associated with this data type allow:

  • the addition of pairs to the collection
  • the removal of pairs from the collection
  • the modification of the values of existing pairs
  • the lookup of the value associated with a particular key

The dictionary problem is a classic computer science problem: the task of designing a data structure that maintains a set of data during ‘search’ ‘delete’ and ‘insert’ operations.[3] A standard solution to the dictionary problem is a hash table; in some cases it is also possible to solve the problem using directly addressed arraysbinary search trees, or other more specialized structures.

Associative Arrays

Associative arrays in scripting languages are often a very complex adaptation of a hash.  They hide a lot of required implementation behind the scenes so all you have to be concerned about is the key and the value.

However, in many languages, including PHP, an associative array’s keys are limited.

Expanding the Keys

In PHP, keys in an array can only be a string or an integer.  However, in many general symbolic programming situations, you may want to be able to associate one piece of data with another.  You can establish a link between to pieces of data by setting one as a key and one as a value in an associative array.  The Map data type allows you to use any data type as a key and a value.  Internally, the keys and values are stored as separate arrays:

The $keys array is an array of arrays. A little complex, but not really. Each element is a two element array, one being the key, and the next being the index of the value in the $values array. These elements can be indexed through the class constants:

As you can see in the constructor the indices into the $values array are stored alongside the keys in $keys:

The Map class can be successfully used in a foreach loop, which is very very useful. This is done because the Map class implements the \Iterator interface. (It seems at the moment that the Map class doesn’t declare the interface explicitly. This may change later if necessary.) These functions are defined as thus:

The basic functions necessary for operating a Map follow (not using it as a normal array) [exists, add, index, replace, set, remove, find]:

The really useful stuff comes in the form of the static methods [difference, intersect, combine, countValues, merge]. Below is a description as commented in the code of each function:

Three of these functions are variadic. Because of this the arguments must be normalized so that they are all Map objects. Because of cloning/referencing you may end up with keys you don’t suspect. For clarity here is the function that normalizes all the arguments to a Map:

The one function that is present in this class that is not present, as far as I know, in PHP is the countValues static function. This is an interesting function where the values are used as keys and the values of those keys are the number of times those values occurred in the original Map.

For example, something like the following:

Would turn into:

Artificial Intelligence

This data structure allows a programmatic link between two pieces of data. This is useful in a situation where there is associations in knowledge representation for instance. Using references, you can link multiple objects together, although a tree structure would be better for that. This data structure will be used in the universal event system later as a conduit.

photo credit: Floresta Amazonica – Amazonia – Rio Solimões – Brasil. via photopin (license)

Liked it? Take a second to support kadar on Patreon!

kadar

I'm just a wunk, trying to enjoy life. I am a cofounder of http//originalpursuitssoc.com/ and I like computers, code, creativity, and friends.

You may also like...

Leave a Reply

%d bloggers like this: