Menu

[r51]: / trunk / lime / set.so.php  Maximize  Restore  History

Download this file

30 lines (27 with data), 829 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
/*
File: set.so.php
License: GPL
Purpose: We should really have a "set" data type. It's too useful.
*/
class set {
function __construct($list=array()) { $this->data = array_count_values($list); }
function has($item) { return isset($this->data[$item]); }
function add($item) { $this->data[$item] = true; }
function del($item) { unset($this->data[$item]); return $item;}
function all() { return array_keys($this->data); }
function one() { return key($this->data); }
function count() { return count($this->data); }
function pop() { return $this->del($this->one()); }
function union($that) {
$progress = false;
foreach ($that->all() as $item) if (!$this->has($item)) {
$this->add($item);
$progress = true;
}
return $progress;
}
function text() {
return ' { '.implode(' ', $this->all()).' } ';
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.