2011-12-28 01:23:38 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
File: set.so.php
|
|
|
|
License: GPL
|
|
|
|
Purpose: We should really have a "set" data type. It's too useful.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class set {
|
2011-12-29 01:50:59 +04:00
|
|
|
public function __construct(array $list = array()) {
|
|
|
|
$this->data = array_count_values($list);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function has($item) {
|
|
|
|
return isset($this->data[$item]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add($item) {
|
|
|
|
$this->data[$item] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function del($item) {
|
|
|
|
unset($this->data[$item]);
|
|
|
|
return $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function all() {
|
|
|
|
return array_keys($this->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function one() {
|
|
|
|
return key($this->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function count() {
|
|
|
|
return count($this->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function pop() {
|
|
|
|
return $this->del($this->one());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function union($that) {
|
2011-12-28 01:23:38 +04:00
|
|
|
$progress = false;
|
2011-12-29 01:50:59 +04:00
|
|
|
foreach ($that->all() as $item) {
|
|
|
|
if (!$this->has($item)) {
|
|
|
|
$this->add($item);
|
|
|
|
$progress = true;
|
|
|
|
}
|
2011-12-28 01:23:38 +04:00
|
|
|
}
|
2011-12-29 01:50:59 +04:00
|
|
|
|
2011-12-28 01:23:38 +04:00
|
|
|
return $progress;
|
|
|
|
}
|
2011-12-29 01:50:59 +04:00
|
|
|
|
|
|
|
public function text() {
|
|
|
|
return ' { ' . implode(' ', $this->all()) . ' } ';
|
2011-12-28 01:23:38 +04:00
|
|
|
}
|
|
|
|
}
|