Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added sortable interface
  • Loading branch information
adlawson committed Nov 8, 2011
1 parent 4319bcb commit cf00dee
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 21 deletions.
9 changes: 5 additions & 4 deletions lib/Restack/Dependency/Provider.php
Expand Up @@ -3,6 +3,7 @@
namespace Restack\Dependency;

use Restack\Index;
use Restack\Dependency\Sortable;
use Restack\Exception\CircularDependencyException;
use Restack\Exception\InvalidItemException;

Expand All @@ -28,10 +29,10 @@ class Provider

/**
* Setup the dependency provider
* @param Restack\Index $index
* @param Restack\Dependency\Sortable $index
* @return void
*/
public function __construct(Index $index)
public function __construct(Sortable $index)
{
$this->setIndex($index);
}
Expand Down Expand Up @@ -144,10 +145,10 @@ public function getIndex()
*
* All current dependencies will be cleared
*
* @param Restack\Index $index
* @param Restack\Dependency\Sortable $index
* @return void
*/
public function setIndex($index)
public function setIndex(Sortable $index)
{
$this->clear();
$this->index = $index;
Expand Down
42 changes: 42 additions & 0 deletions lib/Restack/Dependency/Sortable.php
@@ -0,0 +1,42 @@
<?php

namespace Restack\Dependency;

/**
* Dependable datastructure interface
*
* @category Restack
* @package Restack\Dependency
*/
interface Sortable
{
/**
* Search for an item in storage
* @param mixed $item
* @return integer|false Item key or false if not found
*/
public function search($item);

/**
* Get the item index
* @return array
*/
public function getItems();

/**
* Get the priority of an item
* @param mixed $item
* @throws Restack\Exception\InvalidItemException
* @return integer
*/
public function getOrder($item);

/**
* Set the priority of an item
* @param mixed $item
* @param integer $order
* @throws Restack\Exception\InvalidItemException
* @return void
*/
public function setOrder($item, $order);
}
2 changes: 1 addition & 1 deletion lib/Restack/Index.php
Expand Up @@ -21,7 +21,7 @@ class Index implements \Countable, \IteratorAggregate
* Stored items
* @var array
*/
private $items = array();
private $items = array();

/**
* The index state
Expand Down
17 changes: 9 additions & 8 deletions lib/Restack/Queue.php
Expand Up @@ -2,6 +2,7 @@

namespace Restack;

use Restack\Dependency\Sortable;
use Restack\Exception\InvalidItemException;
use SplPriorityQueue;

Expand All @@ -13,7 +14,7 @@
* @category Restack
* @package Restack
*/
class Queue extends Index
class Queue extends Index implements Sortable
{
const DEFAULT_ORDER = 1;

Expand All @@ -24,10 +25,10 @@ class Queue extends Index
private $base = PHP_INT_MAX;

/**
* Map an item to a given priority
* Map an item to a given position
* @var array
*/
private $priorities = array();
private $order = array();

/**
* The queue instance
Expand All @@ -44,8 +45,8 @@ public function clear()
parent::clear();

$this->base = PHP_INT_MAX;
$this->order = array();
$this->queue = null;
$this->priorities = array();
}

/**
Expand Down Expand Up @@ -73,7 +74,7 @@ public function remove($item)

if (false !== $key)
{
unset($this->priorities[$key]);
unset($this->order[$key]);
}

parent::remove($item);
Expand Down Expand Up @@ -103,7 +104,7 @@ public function getOrder($item)
throw new InvalidItemException('Item does not exist in storage');
}

return reset($this->priorities[$key]);
return reset($this->order[$key]);
}

/**
Expand All @@ -122,7 +123,7 @@ public function setOrder($item, $order)
throw new InvalidItemException('Item does not exist in storage');
}

$this->priorities[$key] = array((int) $order, $this->base--);
$this->order[$key] = array((int) $order, $this->base--);
}

/**
Expand All @@ -137,7 +138,7 @@ public function getQueue()

foreach ($this->getItems() as $key => $item)
{
$this->queue->insert($item, $this->priorities[$key]);
$this->queue->insert($item, $this->order[$key]);
}
}

Expand Down
17 changes: 9 additions & 8 deletions lib/Restack/Stack.php
Expand Up @@ -2,6 +2,7 @@

namespace Restack;

use Restack\Dependency\Sortable;
use Restack\Exception\InvalidItemException;
use SplPriorityQueue;

Expand All @@ -13,7 +14,7 @@
* @category Restack
* @package Restack
*/
class Stack extends Index
class Stack extends Index implements Sortable
{
const DEFAULT_ORDER = 1;

Expand All @@ -24,10 +25,10 @@ class Stack extends Index
private $base = PHP_INT_MAX;

/**
* Map an item to a given priority
* Map an item to a given position
* @var array
*/
private $priorities = array();
private $order = array();

/**
* The stack instance
Expand All @@ -44,8 +45,8 @@ public function clear()
parent::clear();

$this->base = PHP_INT_MAX;
$this->order = array();
$this->stack = null;
$this->priorities = array();
}

/**
Expand Down Expand Up @@ -73,7 +74,7 @@ public function remove($item)

if (false !== $key)
{
unset($this->priorities[$key]);
unset($this->order[$key]);
}

parent::remove($item);
Expand Down Expand Up @@ -103,7 +104,7 @@ public function getOrder($item)
throw new InvalidItemException('Item does not exist in storage');
}

return PHP_INT_MAX - reset($this->priorities[$key]);
return PHP_INT_MAX - reset($this->order[$key]);
}

/**
Expand All @@ -122,7 +123,7 @@ public function setOrder($item, $order)
throw new InvalidItemException('Item does not exist in storage');
}

$this->priorities[$key] = array((PHP_INT_MAX - (int) $order), $this->base--);
$this->order[$key] = array((PHP_INT_MAX - (int) $order), $this->base--);
}

/**
Expand All @@ -137,7 +138,7 @@ public function getStack()

foreach ($this->getItems() as $key => $item)
{
$this->stack->insert($item, $this->priorities[$key]);
$this->stack->insert($item, $this->order[$key]);
}
}

Expand Down

0 comments on commit cf00dee

Please sign in to comment.