5 function DbaListSet(&$dbh)
10 function create_sequence($seq)
14 if (!$dbh->exists('max_key')) {
15 // echo "initializing DbaListSet";
16 // FIXME: check to see if it's really empty?
17 $dbh->insert('max_key', 0);
20 $key = "s" . urlencode($seq);
21 assert(intval($key) == 0 && !strstr($key, ':'));
22 if (!$dbh->exists($key))
23 $dbh->insert($key, "$key:$key:");
26 function delete_sequence($seq)
28 $key = "s" . urlencode($seq);
29 for ($i = $this->firstkey($seq); $i; $i = $next) {
30 $next = $this->next($i);
33 $this->_dbh->delete($key);
36 function firstkey($seq)
38 $key = "s" . urlencode($seq);
39 list(, $next) = explode(':', $this->_dbh->fetch($key), 3);
43 function lastkey($seq)
45 $key = "s" . urlencode($seq);
46 list($prev) = explode(':', $this->_dbh->fetch($key), 3);
53 list(, $next,) = explode(':', $this->_dbh->fetch(intval($i)), 3);
59 list($prev, ,) = explode(':', $this->_dbh->fetch(intval($i)), 3);
66 return $i && $this->_dbh->exists($i);
71 list(, , $data) = explode(':', $this->_dbh->fetch(intval($i)), 3);
75 function replace($i, $data)
78 list($prev, $next,) = explode(':', $dbh->fetch(intval($i)), 3);
79 $dbh->replace($i, "$prev:$next:$data");
82 function insert_before($i, $data)
85 return $this->_insert_before_nc($i, $data);
88 function insert_after($i, $data)
91 return $this->_insert_after_nc($i, $data);
94 function append($seq, $data)
96 $key = "s" . urlencode($seq);
97 $this->_insert_before_nc($key, $data);
100 function prepend($seq, $data)
102 $key = "s" . urlencode($seq);
103 $this->_insert_after_nc($key, $data);
106 function _insert_before_nc($i, &$data)
108 $newkey = $this->_new_key();
109 $old_prev = $this->_setprev($i, $newkey);
110 $this->_setnext($old_prev, $newkey);
111 $this->_dbh->insert($newkey, "$old_prev:$i:$data");
115 function _insert_after_nc($i, &$data)
117 $newkey = $this->_new_key();
118 $old_next = $this->_setnext($i, $newkey);
119 $this->_setprev($old_next, $newkey);
120 $this->_dbh->insert($newkey, "$i:$old_next:$data");
127 list($prev, $next) = explode(':', $dbh->fetch(intval($i)), 3);
128 $this->_setnext($prev, $next);
129 $this->_setprev($next, $prev);
130 $dbh->delete(intval($i));
136 $new_key = $dbh->fetch('max_key') + 1;
137 $dbh->replace('max_key', $new_key);
141 function _setprev($i, $new_prev)
144 list($old_prev, $next, $data) = explode(':', $dbh->fetch($i), 3);
145 $dbh->replace($i, "$new_prev:$next:$data");
149 function _setnext($i, $new_next)
152 list($prev, $old_next, $data) = explode(':', $dbh->fetch($i), 3);
153 $dbh->replace($i, "$prev:$new_next:$data");
162 // c-hanging-comment-ender-p: nil
163 // indent-tabs-mode: nil