TPriorityMap implements a collection that takes key-value pairs with a priority to allow key-value pairs to be ordered. This ordering is important when flattening the map. When flattening the map, if some key-value pairs are required to be before or after others, use this class to keep order to your map. You can access, add or remove an item with a key by using {@link itemAt}, {@link add}, and {@link remove}. These functions can optionally take a priority parameter to allow access to specific priorities. TPriorityMap is functionally backward compatible with {@link TMap}. To get the number of the items in the map, use {@link getCount}. TPriorityMap can also be used like a regular array as follows, $map[$key]=$value; // add a key-value pair unset($map[$key]); // remove the value with the specified key if(isset($map[$key])) // if the map contains the key foreach($map as $key=>$value) // traverse the items in the map $n=count($map); // returns the number of items in the map Using standard array access method like these will always use the default priority. An item that doesn't specify a priority will receive the default priority. The default priority is set during the instantiation of a new TPriorityMap. If no custom default priority is specified, the standard default priority of 10 is used. Priorities with significant digits below precision will be rounded. A priority may also be a numeric with decimals. This is set during the instantiation of a new TPriorityMap. The default is 8 decimal places for a priority. If a negative number is used, rounding occurs into the integer space rather than in the decimal space. See {@link round}.
Since: 3.2a
Author: Brad Anderson (javalizard@mac.com)
Inheritance: extends TMap
示例#1
0
 public function testCopyAndMergeWithPriorities()
 {
     $this->setUpPriorities();
     $map1 = new TPriorityMap();
     $map1->add('key1', $this->item1);
     $map1->add('keyc', 'valuec');
     $map1->copyFrom($this->map);
     $this->assertEquals(5, $map1->getCount());
     $array = $map1->toArray();
     $ordered_keys = array_keys($array);
     $this->assertEquals('key3', $ordered_keys[0]);
     $this->assertEquals('key5', $ordered_keys[1]);
     $this->assertEquals('key1', $ordered_keys[2]);
     $this->assertEquals('key2', $ordered_keys[3]);
     $this->assertEquals('key4', $ordered_keys[4]);
     $ordered_values = array_values($array);
     $this->assertEquals($this->item3, $ordered_values[0]);
     $this->assertEquals($this->item5, $ordered_values[1]);
     $this->assertEquals($this->item1, $ordered_values[2]);
     $this->assertEquals($this->item2, $ordered_values[3]);
     $this->assertEquals($this->item4, $ordered_values[4]);
     $map2 = new TPriorityMap();
     $map2->add('startkey', 'startvalue', -1000);
     $map2->add('key5', 'value5', 40);
     $map2->add('endkey', 'endvalue', 1000);
     $map2->mergeWith($this->map);
     $this->assertEquals(7, $map2->getCount());
     $array = $map2->toArray();
     $ordered_keys = array_keys($array);
     $this->assertEquals('startkey', $ordered_keys[0]);
     $this->assertEquals('key3', $ordered_keys[1]);
     $this->assertEquals('key5', $ordered_keys[2]);
     $this->assertEquals('key1', $ordered_keys[3]);
     $this->assertEquals('key2', $ordered_keys[4]);
     $this->assertEquals('key4', $ordered_keys[5]);
     $this->assertEquals('endkey', $ordered_keys[6]);
     $ordered_values = array_values($array);
     $this->assertEquals('startvalue', $ordered_values[0]);
     $this->assertEquals($this->item3, $ordered_values[1]);
     $this->assertEquals($this->item5, $ordered_values[2]);
     $this->assertEquals($this->item1, $ordered_values[3]);
     $this->assertEquals($this->item2, $ordered_values[4]);
     $this->assertEquals($this->item4, $ordered_values[5]);
     $this->assertEquals('endvalue', $ordered_values[6]);
     $this->assertEquals(1, $map2->priorityAt('key5'));
     $this->assertEquals(1, $map2->priorityOf($this->item5));
 }