]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Collections/TPriorityList.php
6a66b5bec5573050ae97cbd91f6401a1cdcc356a
[bacula/bacula] / gui / baculum / framework / Collections / TPriorityList.php
1 <?php
2 /**
3  * TPriorityList, TPriorityListIterator classes
4  *
5  * @author Brad Anderson <javalizard@gmail.com>
6  * @link http://www.pradosoft.com/
7  * @copyright Copyright &copy; 2005-2014 PradoSoft
8  * @license http://www.pradosoft.com/license/
9  * @package System.Collections
10  */
11
12 /**
13  * TPriorityList class
14  *
15  * TPriorityList implements a priority ordered list collection class.  It allows you to specify
16  * any numeric for priorities down to a specific precision.  The lower the numeric, the high the priority of the item in the
17  * list.  Thus -10 has a higher priority than -5, 0, 10 (the default), 18, 10005, etc.  Per {@link round}, precision may be negative and
18  * thus rounding can go by 10, 100, 1000, etc, instead of just .1, .01, .001, etc. The default precision allows for 8 decimal
19  * places. There is also a default priority of 10, if no different default priority is specified or no item specific priority is indicated.
20  * If you replace TList with this class it will  work exactly the same with items inserted set to the default priority, until you start
21  * using different priorities than the default priority.
22  *
23  * As you access the PHP array features of this class, it flattens and caches the results.  If at all possible, this
24  * will keep the cache fresh even when manipulated.  If this is not possible the cache is cleared.
25  * When an array of items are needed and the cache is outdated, the cache is recreated from the items and their priorities
26  *
27  * You can access, append, insert, remove an item by using
28  * {@link itemAt}, {@link add}, {@link insertAt}, and {@link remove}.
29  * To get the number of the items in the list, use {@link getCount}.
30  * TPriorityList can also be used like a regular array as follows,
31  * <code>
32  * $list[]=$item;  // append with the default priority.  It may not be the last item if other items in the list are prioritized after the default priority
33  * $list[$index]=$item; // $index must be between 0 and $list->Count-1.  This sets the element regardless of priority.  Priority stays the same.
34  * $list[$index]=$item; // $index is $list->Count.  This appends the item to the end of the list with the same priority as the last item in the list.
35  * unset($list[$index]); // remove the item at $index
36  * if(isset($list[$index])) // if the list has an item at $index
37  * foreach($list as $index=>$item) // traverse each item in the list in proper priority order and add/insert order
38  * $n=count($list); // returns the number of items in the list
39  * </code>
40  *
41  * To extend TPriorityList for doing your own operations with each addition or removal,
42  * override {@link insertAtIndexInPriority()} and {@link removeAtIndexInPriority()} and then call the parent.
43  *
44  * @author Brad Anderson <javalizard@gmail.com>
45  * @package System.Collections
46  * @since 3.2a
47  */
48 class TPriorityList extends TList
49 {
50         /**
51          * @var array internal data storage
52          */
53         private $_d=array();
54         /**
55          * @var boolean indicates if the _d is currently ordered.
56          */
57         private $_o=false;
58         /**
59          * @var array cached flattened internal data storage
60          */
61         private $_fd=null;
62         /**
63          * @var integer number of items contain within the list
64          */
65         private $_c=0;
66         /**
67          * @var numeric the default priority of items without specified priorities
68          */
69         private $_dp=10;
70         /**
71          * @var integer the precision of the numeric priorities within this priority list.
72          */
73         private $_p=8;
74
75         /**
76          * Constructor.
77          * Initializes the list with an array or an iterable object.
78          * @param array|Iterator the intial data. Default is null, meaning no initial data.
79          * @param boolean whether the list is read-only
80          * @param numeric the default priority of items without specified priorities.
81          * @param integer the precision of the numeric priorities
82          * @throws TInvalidDataTypeException If data is not null and is neither an array nor an iterator.
83          */
84         public function __construct($data=null,$readOnly=false,$defaultPriority=10,$precision=8)
85         {
86                 parent::__construct();
87                 if($data!==null)
88                         $this->copyFrom($data);
89                 $this->setReadOnly($readOnly);
90                 $this->setPrecision($precision);
91                 $this->setDefaultPriority($defaultPriority);
92         }
93
94         /**
95          * Returns the number of items in the list.
96          * This method is required by Countable interface.
97          * @return integer number of items in the list.
98          */
99         public function count()
100         {
101                 return $this->getCount();
102         }
103
104         /**
105          * Returns the total number of items in the list
106          * @return integer the number of items in the list
107          */
108         public function getCount()
109         {
110                 return $this->_c;
111         }
112
113         /**
114          * Gets the number of items at a priority within the list
115          * @param numeric optional priority at which to count items.  if no parameter, it will be set to the default {@link getDefaultPriority}
116          * @return integer the number of items in the list at the specified priority
117          */
118         public function getPriorityCount($priority=null)
119         {
120                 if($priority===null)
121                         $priority=$this->getDefaultPriority();
122                 $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p);
123
124                 if(!isset($this->_d[$priority]) || !is_array($this->_d[$priority]))
125                         return false;
126                 return count($this->_d[$priority]);
127         }
128
129         /**
130          * @return numeric gets the default priority of inserted items without a specified priority
131          */
132         public function getDefaultPriority()
133         {
134                 return $this->_dp;
135         }
136
137         /**
138          * This must be called internally or when instantiated.
139          * @param numeric sets the default priority of inserted items without a specified priority
140          */
141         protected function setDefaultPriority($value)
142         {
143                 $this->_dp=(string)round(TPropertyValue::ensureFloat($value),$this->_p);
144         }
145
146         /**
147          * @return integer The precision of numeric priorities, defaults to 8
148          */
149         public function getPrecision()
150         {
151                 return $this->_p;
152         }
153
154         /**
155          * This must be called internally or when instantiated.
156          * @param integer The precision of numeric priorities.
157          */
158         protected function setPrecision($value)
159         {
160                 $this->_p=TPropertyValue::ensureInteger($value);
161         }
162
163         /**
164          * Returns an iterator for traversing the items in the list.
165          * This method is required by the interface IteratorAggregate.
166          * @return Iterator an iterator for traversing the items in the list.
167          */
168         public function getIterator()
169         {
170                 return new ArrayIterator($this->flattenPriorities());
171         }
172
173         /**
174          * This returns a list of the priorities within this list, ordered lowest to highest.
175          * @return array the array of priority numerics in decreasing priority order
176          */
177         public function getPriorities()
178         {
179                 $this->sortPriorities();
180                 return array_keys($this->_d);
181         }
182
183
184         /**
185          * This orders the priority list internally.
186          */
187         protected function sortPriorities() {
188                 if(!$this->_o) {
189                         ksort($this->_d,SORT_NUMERIC);
190                         $this->_o=true;
191                 }
192         }
193
194         /**
195          * This flattens the priority list into a flat array [0,...,n-1]
196          * @return array array of items in the list in priority and index order
197          */
198         protected function flattenPriorities() {
199                 if(is_array($this->_fd))
200                         return $this->_fd;
201
202                 $this->sortPriorities();
203                 $this->_fd=array();
204                 foreach($this->_d as $priority => $itemsatpriority)
205                         $this->_fd=array_merge($this->_fd,$itemsatpriority);
206                 return $this->_fd;
207         }
208
209
210         /**
211          * Returns the item at the index of a flattened priority list.
212          * {@link offsetGet} calls this method.
213          * @param integer the index of the item to get
214          * @return mixed the element at the offset
215          * @throws TInvalidDataValueException Issued when the index is invalid
216          */
217         public function itemAt($index)
218         {
219                 if($index>=0&&$index<$this->getCount()) {
220                         $arr=$this->flattenPriorities();
221                         return $arr[$index];
222                 } else
223                         throw new TInvalidDataValueException('list_index_invalid',$index);
224         }
225
226         /**
227          * Gets all the items at a specific priority.
228          * @param numeric priority of the items to get.  Defaults to null, filled in with the default priority, if left blank.
229          * @return array all items at priority in index order, null if there are no items at that priority
230          */
231         public function itemsAtPriority($priority=null)
232         {
233                 if($priority===null)
234                         $priority=$this->getDefaultPriority();
235                 $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p);
236
237                 return isset($this->_d[$priority])?$this->_d[$priority]:null;
238         }
239
240         /**
241          * Returns the item at an index within a priority
242          * @param integer the index into the list of items at priority
243          * @param numeric the priority which to index.  no parameter or null will result in the default priority
244          * @return mixed the element at the offset, false if no element is found at the offset
245          */
246         public function itemAtIndexInPriority($index,$priority=null)
247         {
248                 if($priority===null)
249                         $priority=$this->getDefaultPriority();
250                 $priority=(string)round(TPropertyValue::ensureFloat($priority), $this->_p);
251
252                 return !isset($this->_d[$priority])?false:(
253                                 isset($this->_d[$priority][$index])?$this->_d[$priority][$index]:false
254                         );
255         }
256
257         /**
258          * Appends an item into the list at the end of the specified priority.  The position of the added item may
259          * not be at the end of the list.
260          * @param mixed item to add into the list at priority
261          * @param numeric priority blank or null for the default priority
262          * @return int the index within the flattened array
263          * @throws TInvalidOperationException if the map is read-only
264          */
265         public function add($item,$priority=null)
266         {
267                 if($this->getReadOnly())
268                         throw new TInvalidOperationException('list_readonly',get_class($this));
269
270                 return $this->insertAtIndexInPriority($item,false,$priority,true);
271         }
272
273         /**
274          * Inserts an item at an index.  It reads the priority of the item at index within the flattened list
275          * and then inserts the item at that priority-index.
276          * @param integer the specified position in the flattened list.
277          * @param mixed new item to add
278          * @throws TInvalidDataValueException If the index specified exceeds the bound
279          * @throws TInvalidOperationException if the list is read-only
280          */
281         public function insertAt($index,$item)
282         {
283                 if($this->getReadOnly())
284                         throw new TInvalidOperationException('list_readonly',get_class($this));
285
286                 if(($priority=$this->priorityAt($index,true))!==false)
287                         $this->insertAtIndexInPriority($item,$priority[1],$priority[0]);
288                 else
289                         throw new TInvalidDataValueException('list_index_invalid',$index);
290         }
291
292         /**
293          * Inserts an item at the specified index within a priority.  Override and call this method to
294          * insert your own functionality.
295          * @param mixed item to add within the list.
296          * @param integer index within the priority to add the item, defaults to false which appends the item at the priority
297          * @param numeric priority priority of the item.  defaults to null, which sets it to the default priority
298          * @param boolean preserveCache specifies if this is a special quick function or not. This defaults to false.
299          * @throws TInvalidDataValueException If the index specified exceeds the bound
300          * @throws TInvalidOperationException if the list is read-only
301          */
302         public function insertAtIndexInPriority($item,$index=false,$priority=null,$preserveCache=false)
303         {
304                 if($this->getReadOnly())
305                         throw new TInvalidOperationException('list_readonly',get_class($this));
306
307                 if($priority===null)
308                         $priority=$this->getDefaultPriority();
309                 $priority=(string)round(TPropertyValue::ensureFloat($priority), $this->_p);
310
311                 if($preserveCache) {
312                         $this->sortPriorities();
313                         $cc=0;
314                         foreach($this->_d as $prioritykey=>$items)
315                                 if($prioritykey>=$priority)
316                                         break;
317                                 else
318                                         $cc+=count($items);
319
320                         if($index===false&&isset($this->_d[$priority])) {
321                                 $c=count($this->_d[$priority]);
322                                 $c+=$cc;
323                                 $this->_d[$priority][]=$item;
324                         } else if(isset($this->_d[$priority])) {
325                                 $c=$index+$cc;
326                                 array_splice($this->_d[$priority],$index,0,array($item));
327                         } else {
328                                 $c = $cc;
329                                 $this->_o = false;
330                                 $this->_d[$priority]=array($item);
331                         }
332
333                         if($this->_fd&&is_array($this->_fd)) // if there is a flattened array cache
334                                 array_splice($this->_fd,$c,0,array($item));
335                 } else {
336                         $c=null;
337                         if($index===false&&isset($this->_d[$priority])) {
338                                 $cc=count($this->_d[$priority]);
339                                 $this->_d[$priority][]=$item;
340                         } else if(isset($this->_d[$priority])) {
341                                 $cc=$index;
342                                 array_splice($this->_d[$priority],$index,0,array($item));
343                         } else {
344                                 $cc=0;
345                                 $this->_o=false;
346                                 $this->_d[$priority]=array($item);
347                         }
348                         if($this->_fd&&is_array($this->_fd)&&count($this->_d)==1)
349                                 array_splice($this->_fd,$cc,0,array($item));
350                         else
351                                 $this->_fd=null;
352                 }
353
354                 $this->_c++;
355
356                 return $c;
357
358         }
359
360
361         /**
362          * Removes an item from the priority list.
363          * The list will search for the item.  The first matching item found will be removed from the list.
364          * @param mixed item the item to be removed.
365          * @param numeric priority of item to remove. without this parameter it defaults to false.
366          * A value of false means any priority. null will be filled in with the default priority.
367          * @return integer index within the flattened list at which the item is being removed
368          * @throws TInvalidDataValueException If the item does not exist
369          */
370         public function remove($item,$priority=false)
371         {
372                 if($this->getReadOnly())
373                         throw new TInvalidOperationException('list_readonly',get_class($this));
374
375                 if(($p=$this->priorityOf($item,true))!==false)
376                 {
377                         if($priority!==false) {
378                                 if($priority===null)
379                                         $priority=$this->getDefaultPriority();
380                                 $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p);
381
382                                 if($p[0]!=$priority)
383                                         throw new TInvalidDataValueException('list_item_inexistent');
384                         }
385                         $this->removeAtIndexInPriority($p[1],$p[0]);
386                         return $p[2];
387                 }
388                 else
389                         throw new TInvalidDataValueException('list_item_inexistent');
390         }
391
392         /**
393          * Removes an item at the specified index in the flattened list.
394          * @param integer index of the item to be removed.
395          * @return mixed the removed item.
396          * @throws TInvalidDataValueException If the index specified exceeds the bound
397          * @throws TInvalidOperationException if the list is read-only
398          */
399         public function removeAt($index)
400         {
401                 if($this->getReadOnly())
402                         throw new TInvalidOperationException('list_readonly',get_class($this));
403
404                 if(($priority=$this->priorityAt($index, true))!==false)
405                         return $this->removeAtIndexInPriority($priority[1],$priority[0]);
406                 throw new TInvalidDataValueException('list_index_invalid',$index);
407         }
408
409         /**
410          * Removes the item at a specific index within a priority.  Override
411          * and call this method to insert your own functionality.
412          * @param integer index of item to remove within the priority.
413          * @param numeric priority of the item to remove, defaults to null, or left blank, it is then set to the default priority
414          * @return mixed the removed item.
415          * @throws TInvalidDataValueException If the item does not exist
416          */
417         public function removeAtIndexInPriority($index, $priority=null)
418         {
419                 if($this->getReadOnly())
420                         throw new TInvalidOperationException('list_readonly',get_class($this));
421
422                 if($priority===null)
423                         $priority=$this->getDefaultPriority();
424                 $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p);
425
426                 if(!isset($this->_d[$priority])||$index<0||$index>=count($this->_d[$priority]))
427                         throw new TInvalidDataValueException('list_item_inexistent');
428
429                 // $value is an array of elements removed, only one
430                 $value=array_splice($this->_d[$priority],$index,1);
431                 $value=$value[0];
432
433                 if(!count($this->_d[$priority]))
434                         unset($this->_d[$priority]);
435
436                 $this->_c--;
437                 $this->_fd=null;
438                 return $value;
439         }
440
441         /**
442          * Removes all items in the priority list by calling removeAtIndexInPriority from the last item to the first.
443          */
444         public function clear()
445         {
446                 if($this->getReadOnly())
447                         throw new TInvalidOperationException('list_readonly',get_class($this));
448
449                 $d=array_reverse($this->_d,true);
450                 foreach($this->_d as $priority=>$items) {
451                         for($index=count($items)-1;$index>=0;$index--)
452                                 $this->removeAtIndexInPriority($index,$priority);
453                         unset($this->_d[$priority]);
454                 }
455         }
456
457         /**
458          * @param mixed item
459          * @return boolean whether the list contains the item
460          */
461         public function contains($item)
462         {
463                 return $this->indexOf($item)>=0;
464         }
465
466         /**
467          * @param mixed item
468          * @return integer the index of the item in the flattened list (0 based), -1 if not found.
469          */
470         public function indexOf($item)
471         {
472                 if(($index=array_search($item,$this->flattenPriorities(),true))===false)
473                         return -1;
474                 else
475                         return $index;
476         }
477
478         /**
479          * Returns the priority of a particular item
480          * @param mixed the item to look for within the list
481          * @param boolean withindex this specifies if the full positional data of the item within the list is returned.
482          *              This defaults to false, if no parameter is provided, so only provides the priority number of the item by default.
483          * @return numeric|array the priority of the item in the list, false if not found.
484          *   if withindex is true, an array is returned of [0 => $priority, 1 => $priorityIndex, 2 => flattenedIndex,
485          * 'priority' => $priority, 'index' => $priorityIndex, 'absindex' => flattenedIndex]
486          */
487         public function priorityOf($item,$withindex = false)
488         {
489                 $this->sortPriorities();
490
491                 $absindex = 0;
492                 foreach($this->_d as $priority=>$items) {
493                         if(($index=array_search($item,$items,true))!==false) {
494                                 $absindex+=$index;
495                                 return $withindex?array($priority,$index,$absindex,
496                                                 'priority'=>$priority,'index'=>$index,'absindex'=>$absindex):$priority;
497                         } else
498                                 $absindex+=count($items);
499                 }
500
501                 return false;
502         }
503
504         /**
505          * Retutrns the priority of an item at a particular flattened index.
506          * @param integer index of the item within the list
507          * @param boolean withindex this specifies if the full positional data of the item within the list is returned.
508          *              This defaults to false, if no parameter is provided, so only provides the priority number of the item by default.
509          * @return numeric|array the priority of the item in the list, false if not found.
510          *   if withindex is true, an array is returned of [0 => $priority, 1 => $priorityIndex, 2 => flattenedIndex,
511          * 'priority' => $priority, 'index' => $priorityIndex, 'absindex' => flattenedIndex]
512          */
513         public function priorityAt($index,$withindex = false)
514         {
515                 if($index<0||$index>=$this->getCount())
516                         throw new TInvalidDataValueException('list_index_invalid',$index);
517
518                 $absindex=$index;
519                 $this->sortPriorities();
520                 foreach($this->_d as $priority=>$items) {
521                         if($index>=($c=count($items)))
522                                 $index-=$c;
523                         else
524                                 return $withindex?array($priority,$index,$absindex,
525                                                 'priority'=>$priority,'index'=>$index,'absindex'=>$absindex):$priority;
526                 }
527                 return false;
528         }
529
530         /**
531          * This inserts an item before another item within the list.  It uses the same priority as the
532          * found index item and places the new item before it.
533          * @param mixed indexitem the item to index
534          * @param mixed the item to add before indexitem
535          * @return integer where the item has been inserted in the flattened list
536          * @throws TInvalidDataValueException If the item does not exist
537          */
538         public function insertBefore($indexitem, $item)
539         {
540                 if($this->getReadOnly())
541                         throw new TInvalidOperationException('list_readonly',get_class($this));
542
543                 if(($priority=$this->priorityOf($indexitem,true))===false)
544                         throw new TInvalidDataValueException('list_item_inexistent');
545
546                 $this->insertAtIndexInPriority($item,$priority[1],$priority[0]);
547
548                 return $priority[2];
549         }
550
551         /**
552          * This inserts an item after another item within the list.  It uses the same priority as the
553          * found index item and places the new item after it.
554          * @param mixed indexitem the item to index
555          * @param mixed the item to add after indexitem
556          * @return integer where the item has been inserted in the flattened list
557          * @throws TInvalidDataValueException If the item does not exist
558          */
559         public function insertAfter($indexitem, $item)
560         {
561                 if($this->getReadOnly())
562                         throw new TInvalidOperationException('list_readonly',get_class($this));
563
564                 if(($priority=$this->priorityOf($indexitem,true))===false)
565                         throw new TInvalidDataValueException('list_item_inexistent');
566
567                 $this->insertAtIndexInPriority($item,$priority[1]+1,$priority[0]);
568
569                 return $priority[2]+1;
570         }
571
572         /**
573          * @return array the priority list of items in array
574          */
575         public function toArray()
576         {
577                 return $this->flattenPriorities();
578         }
579
580         /**
581          * @return array the array of priorities keys with values of arrays of items.  The priorities are sorted so important priorities, lower numerics, are first.
582          */
583         public function toPriorityArray()
584         {
585                 $this->sortPriorities();
586                 return $this->_d;
587         }
588
589         /**
590          * Combines the map elements which have a priority below the parameter value
591          * @param numeric the cut-off priority.  All items of priority less than this are returned.
592          * @param boolean whether or not the input cut-off priority is inclusive.  Default: false, not inclusive.
593          * @return array the array of priorities keys with values of arrays of items that are below a specified priority.
594          *  The priorities are sorted so important priorities, lower numerics, are first.
595          */
596         public function toArrayBelowPriority($priority,$inclusive=false)
597         {
598                 $this->sortPriorities();
599                 $items=array();
600                 foreach($this->_d as $itemspriority=>$itemsatpriority)
601                 {
602                         if((!$inclusive&&$itemspriority>=$priority)||$itemspriority>$priority)
603                                 break;
604                         $items=array_merge($items,$itemsatpriority);
605                 }
606                 return $items;
607         }
608
609         /**
610          * Combines the map elements which have a priority above the parameter value
611          * @param numeric the cut-off priority.  All items of priority greater than this are returned.
612          * @param boolean whether or not the input cut-off priority is inclusive.  Default: true, inclusive.
613          * @return array the array of priorities keys with values of arrays of items that are above a specified priority.
614          *  The priorities are sorted so important priorities, lower numerics, are first.
615          */
616         public function toArrayAbovePriority($priority,$inclusive=true)
617         {
618                 $this->sortPriorities();
619                 $items=array();
620                 foreach($this->_d as $itemspriority=>$itemsatpriority)
621                 {
622                         if((!$inclusive&&$itemspriority<=$priority)||$itemspriority<$priority)
623                                 continue;
624                         $items=array_merge($items,$itemsatpriority);
625                 }
626                 return $items;
627         }
628
629
630         /**
631          * Copies iterable data into the priority list.
632          * Note, existing data in the map will be cleared first.
633          * @param mixed the data to be copied from, must be an array or object implementing Traversable
634          * @throws TInvalidDataTypeException If data is neither an array nor an iterator.
635          */
636         public function copyFrom($data)
637         {
638                 if($data instanceof TPriorityList)
639                 {
640                         if($this->getCount()>0)
641                                 $this->clear();
642                         foreach($data->getPriorities() as $priority)
643                         {
644                                 foreach($data->itemsAtPriority($priority) as $index=>$item)
645                                         $this->insertAtIndexInPriority($item,$index,$priority);
646                         }
647                 } else if(is_array($data)||$data instanceof Traversable) {
648                         if($this->getCount()>0)
649                                 $this->clear();
650                         foreach($data as $key=>$item)
651                                 $this->add($item);
652                 } else if($data!==null)
653                         throw new TInvalidDataTypeException('map_data_not_iterable');
654         }
655
656         /**
657          * Merges iterable data into the priority list.
658          * New data will be appended to the end of the existing data.  If another TPriorityList is merged,
659          * the incoming parameter items will be appended at the priorities they are present.  These items will be added
660          * to the end of the existing items with equal priorities, if there are any.
661          * @param mixed the data to be merged with, must be an array or object implementing Traversable
662          * @throws TInvalidDataTypeException If data is neither an array nor an iterator.
663          */
664         public function mergeWith($data)
665         {
666                 if($data instanceof TPriorityList)
667                 {
668                         foreach($data->getPriorities() as $priority)
669                         {
670                                 foreach($data->itemsAtPriority($priority) as $index=>$item)
671                                         $this->insertAtIndexInPriority($item,false,$priority);
672                         }
673                 }
674                 else if(is_array($data)||$data instanceof Traversable)
675                 {
676                         foreach($data as $priority=>$item)
677                                 $this->add($item);
678
679                 }
680                 else if($data!==null)
681                         throw new TInvalidDataTypeException('map_data_not_iterable');
682         }
683
684         /**
685          * Returns whether there is an element at the specified offset.
686          * This method is required by the interface ArrayAccess.
687          * @param mixed the offset to check on
688          * @return boolean
689          */
690         public function offsetExists($offset)
691         {
692                 return ($offset>=0&&$offset<$this->getCount());
693         }
694
695         /**
696          * Returns the element at the specified offset.
697          * This method is required by the interface ArrayAccess.
698          * @param integer the offset to retrieve element.
699          * @return mixed the element at the offset, null if no element is found at the offset
700          */
701         public function offsetGet($offset)
702         {
703                 return $this->itemAt($offset);
704         }
705
706         /**
707          * Sets the element at the specified offset. This method is required by the interface ArrayAccess.
708          * Setting elements in a priority list is not straight forword when appending and setting at the
709          * end boundary.  When appending without an offset (a null offset), the item will be added at
710          * the default priority.  The item may not be the last item in the list.  When appending with an
711          * offset equal to the count of the list, the item will get be appended with the last items priority.
712          *
713          * All together, when setting the location of an item, the item stays in that location, but appending
714          * an item into a priority list doesn't mean the item is at the end of the list.
715          * @param integer the offset to set element
716          * @param mixed the element value
717          */
718         public function offsetSet($offset,$item)
719         {
720                 if($offset===null)
721                         return $this->add($item);
722                 if($offset===$this->getCount()) {
723                         $priority=$this->priorityAt($offset-1,true);
724                         $priority[1]++;
725                 } else {
726                         $priority=$this->priorityAt($offset,true);
727                         $this->removeAtIndexInPriority($priority[1],$priority[0]);
728                 }
729                 $this->insertAtIndexInPriority($item,$priority[1],$priority[0]);
730         }
731
732         /**
733          * Unsets the element at the specified offset.
734          * This method is required by the interface ArrayAccess.
735          * @param mixed the offset to unset element
736          */
737         public function offsetUnset($offset)
738         {
739                 $this->removeAt($offset);
740         }
741 }