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);
52 list(, $next,) = explode(':', $this->_dbh->fetch(intval($i)), 3);
58 list($prev, ,) = explode(':', $this->_dbh->fetch(intval($i)), 3);
65 return $i && $this->_dbh->exists($i);
70 list(, , $data) = explode(':', $this->_dbh->fetch(intval($i)), 3);
74 function replace($i, $data)
77 list($prev, $next,) = explode(':', $dbh->fetch(intval($i)), 3);
78 $dbh->replace($i, "$prev:$next:$data");
81 function insert_before($i, $data)
84 return $this->_insert_before_nc($i, $data);
87 function insert_after($i, $data)
90 return $this->_insert_after_nc($i, $data);
93 function append($seq, $data)
95 $key = "s" . urlencode($seq);
96 $this->_insert_before_nc($key, $data);
99 function prepend($seq, $data)
101 $key = "s" . urlencode($seq);
102 $this->_insert_after_nc($key, $data);
105 function _insert_before_nc($i, &$data)
107 $newkey = $this->_new_key();
108 $old_prev = $this->_setprev($i, $newkey);
109 $this->_setnext($old_prev, $newkey);
110 $this->_dbh->insert($newkey, "$old_prev:$i:$data");
114 function _insert_after_nc($i, &$data)
116 $newkey = $this->_new_key();
117 $old_next = $this->_setnext($i, $newkey);
118 $this->_setprev($old_next, $newkey);
119 $this->_dbh->insert($newkey, "$i:$old_next:$data");
126 list($prev, $next) = explode(':', $dbh->fetch(intval($i)), 3);
127 $this->_setnext($prev, $next);
128 $this->_setprev($next, $prev);
129 $dbh->delete(intval($i));
135 $new_key = $dbh->fetch('max_key') + 1;
136 $dbh->replace('max_key', $new_key);
140 function _setprev($i, $new_prev)
143 list($old_prev, $next, $data) = explode(':', $dbh->fetch($i), 3);
144 $dbh->replace($i, "$new_prev:$next:$data");
148 function _setnext($i, $new_next)
151 list($prev, $old_next, $data) = explode(':', $dbh->fetch($i), 3);
152 $dbh->replace($i, "$prev:$new_next:$data");
161 // c-hanging-comment-ender-p: nil
162 // indent-tabs-mode: nil