File: DbaListSet.php

package info (click to toggle)
phpwiki 1.3.14-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 15,716 kB
  • ctags: 23,548
  • sloc: php: 88,295; sql: 1,476; sh: 1,378; perl: 765; makefile: 602; awk: 28
file content (147 lines) | stat: -rwxr-xr-x 4,000 bytes parent folder | download | duplicates (4)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php rcs_id('$Id: DbaListSet.php,v 1.3 2004/11/21 11:59:14 rurban Exp $');

class DbaListSet
{
    function DbaListSet(&$dbh) {
        $this->_dbh = &$dbh;
    }

    function create_sequence($seq) {
        $dbh = &$this->_dbh;

        if (!$dbh->exists('max_key')) {
            // echo "initializing DbaListSet";
            // FIXME: check to see if it's really empty?
            $dbh->insert('max_key', 0);
        }

        $key = "s" . urlencode($seq);
        assert(intval($key) == 0 && !strstr($key, ':'));
        if (!$dbh->exists($key))
            $dbh->insert($key, "$key:$key:");
    }

    function delete_sequence($seq) {
        $key = "s" . urlencode($seq);
        for ($i = $this->firstkey($seq); $i; $i = $next) {
            $next = $this->next($i);
            $this->delete($i);
        }
        $this->_dbh->delete($key);
    }

    function firstkey($seq) {
        $key = "s" . urlencode($seq);
        list(, $next) =  explode(':', $this->_dbh->fetch($key), 3);
        return intval($next);
    }

    function lastkey($seq) {
        $key = "s" . urlencode($seq);
        list($prev) =  explode(':', $this->_dbh->fetch($key), 3);
        return intval($prev);
    }


    function next($i) {
        list( , $next, ) = explode(':', $this->_dbh->fetch(intval($i)), 3);
        return intval($next);
    }

    function prev(&$i) {
        list( $prev , , ) = explode(':', $this->_dbh->fetch(intval($i)), 3);
        return intval($prev);
    }
    
    function exists($i) {
        $i = intval($i);
        return $i && $this->_dbh->exists($i);
    }

    function fetch($i) {
        list(, , $data) = explode(':', $this->_dbh->fetch(intval($i)), 3);
        return $data;
    }

    function replace($i, $data) {
        $dbh = &$this->_dbh;
        list($prev, $next,) = explode(':', $dbh->fetch(intval($i)), 3);
        $dbh->replace($i, "$prev:$next:$data");
    }
    
    function insert_before($i, $data) {
        assert(intval($i));
        return $this->_insert_before_nc($i, $data);
    }

    function insert_after($i, $data) {
        assert(intval($i));
        return $this->_insert_after_nc($i, $data);
    }
    
    function append($seq, $data) {
        $key = "s" . urlencode($seq);
        $this->_insert_before_nc($key, $data);
    }

    function prepend($seq, $data) {
        $key = "s" . urlencode($seq);
        $this->_insert_after_nc($key, $data);
    }
    
    function _insert_before_nc($i, &$data) {
        $newkey = $this->_new_key();
        $old_prev = $this->_setprev($i, $newkey);
        $this->_setnext($old_prev, $newkey);
        $this->_dbh->insert($newkey, "$old_prev:$i:$data");
        return $newkey;
    }

    function _insert_after_nc($i, &$data) {
        $newkey = $this->_new_key();
        $old_next = $this->_setnext($i, $newkey);
        $this->_setprev($old_next, $newkey);
        $this->_dbh->insert($newkey, "$i:$old_next:$data");
        return $newkey;
    }

    function delete($i) {
        $dbh = &$this->_dbh;
        list($prev, $next) = explode(':', $dbh->fetch(intval($i)), 3);
        $this->_setnext($prev, $next);
        $this->_setprev($next, $prev);
        $dbh->delete(intval($i));
    }

    function _new_key() {
        $dbh = &$this->_dbh;
        $new_key = $dbh->fetch('max_key') + 1;
        $dbh->replace('max_key', $new_key);
        return $new_key;
    }

    function _setprev($i, $new_prev) {
        $dbh = &$this->_dbh;
        list($old_prev, $next, $data) = explode(':', $dbh->fetch($i), 3);
        $dbh->replace($i, "$new_prev:$next:$data");
        return $old_prev;
    }

    function _setnext($i, $new_next) {
        $dbh = &$this->_dbh;
        list($prev, $old_next, $data) = explode(':', $dbh->fetch($i), 3);
        $dbh->replace($i, "$prev:$new_next:$data");
        return $old_next;
    }
}


// For emacs users
// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>