<?php
/*
 * This file is part of the Easy-CMS project.
 * Easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
	
include_once('config.inc.php');
include_once('classes/utils/Deque.class.php');
include_once('classes/utils/SortedListIterator.class.php');

class SortedList extends Deque {
  var $comparisonField;
  var $order;
	
  function SortedList($field, $order) {
    parent::Deque();
		$this->debug("SortedList.contructor invoked, field=$field, order=$order.", 10);
    $this->setComparisonField($field);
    $this->setOrder($order);
  }

  function setComparisonField($field) {
    $this->comparisonField = $field;
  }

  function setOrder($order) {
    $this->order = $order;
  }
	
  function getComparisonField() {
    return $this->comparisonField;
  }

  function getOrder() {
    return $this->order;
  }

  function getFactor() {
		if($this->order == SORT_ASCENDING)
			return 1;
    return -1;
  }
	
  function push(&$object) {
		if (!is_object($object)) {
			$this->debug("SortedList.push, error : trying to push a non-object (push skipped).", 1);
			return -1;
    }
    $prev = &$this->getRoot();
    $next = &$prev->getNext();

    $nextObject = &$next->getValue();
    $field      = &$this->getComparisonField();
    $order  		= &$this->getOrder();

    while ($nextObject != null && (($this->getFactor()*$object->compareTo($nextObject, $field)) >= 0)) {
      $temp = &$next->getNext();
      $prev = &$next;
      $next = &$temp;
      $nextObject = &$next->getValue();
    }

    $this->insert($object, $prev, $next);
    return 1;
  } 

  function &getIterator() {
		$iterator = &new SortedListIterator($this);
		return $iterator;
  } 
} 

?>