~ubuntu-branches/ubuntu/hardy/steam/hardy

« back to all changes in this revision

Viewing changes to server/libraries/Visconte.pmod/MVList.pike

  • Committer: Bazaar Package Importer
  • Author(s): Alain Schroeder
  • Date: 2006-11-21 16:03:12 UTC
  • mfrom: (2.1.4 feisty)
  • Revision ID: james.westby@ubuntu.com-20061121160312-nf96y6nihzsyd2uv
Tags: 2.2.31-3
Add patch to prevent inconsistent data after shutdown.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * <p>Class MVList provides a simple multi value list, 
 
3
 * instead of a mapping there is no limitation on one single index value pair</p>
 
4
 */
 
5
 
 
6
 
 
7
/**
 
8
 * <p>the list</p>
 
9
 */
 
10
    private mapping(string:array(string)) list = ([]);
 
11
   
 
12
    
 
13
 
 
14
/**
 
15
 * <p>adds a index value pair. If the index exists, the value will be appendended</p>
 
16
 * 
 
17
 * 
 
18
 * @param index - the index for the multivalues 
 
19
 * @param value - the value for the index, either string or an array,  
 
20
 */
 
21
    public void  add(string index, string|array(string) value) 
 
22
    {  
 
23
           
 
24
            
 
25
            if(has_index(list,index))
 
26
            {
 
27
                    array valuelist = list[index];
 
28
                    if(arrayp(value))
 
29
                            valuelist+=value;
 
30
                    else if(stringp(value))
 
31
                            valuelist+=({ value });
 
32
                    
 
33
                    list[index]=valuelist;
 
34
            }
 
35
            else
 
36
            {
 
37
                    if(arrayp(value))
 
38
                           list+=([ index : value ]);
 
39
                    else if(stringp(value))
 
40
                           list+=([ index : ({ value }) ]);
 
41
            }    
 
42
    }
 
43
    
 
44
 
 
45
/**
 
46
 * <p>remove a value from the list</p>
 
47
 * 
 
48
 * 
 
49
 * @param index - the index of the value to remove
 
50
 * @param value - the value to remove
 
51
 * @retrun success
 
52
 */
 
53
    public int  remove(string index, string value) 
 
54
    {               
 
55
               if(has_index(list,index))
 
56
               {
 
57
                    array valuelist = list[index];
 
58
                    if(has_value(valuelist,value))
 
59
                    {
 
60
                            valuelist-=({ value });
 
61
                            list[index]=valuelist;
 
62
                            
 
63
                            return 1;
 
64
                    }
 
65
                    
 
66
                    return 0;
 
67
               }
 
68
               
 
69
               return 0;
 
70
               
 
71
    }
 
72
    
 
73
/**
 
74
 * <p>replaces a index</p>
 
75
 * 
 
76
 * 
 
77
 * @param string index_old - the index to replace
 
78
 * @param string index_new - the new index
 
79
 * @return int
 
80
 */
 
81
    public int  replace(string index_old, string index_new) 
 
82
    {               
 
83
               if(has_index(list,index_old))
 
84
               {
 
85
                    array valuelist = list[index_old];
 
86
                    m_delete(list, index_old);
 
87
                    add(index_new,valuelist);
 
88
                    return 1;
 
89
               }
 
90
               
 
91
               return 0;
 
92
               
 
93
    }
 
94
/**
 
95
 * <p>checks if a index - value pair exists in the list</p>
 
96
 * 
 
97
 * 
 
98
 * @param index - the index
 
99
 * @param value - the value
 
100
 * @return success
 
101
 */
 
102
    public int  has_index_value(string index, string value) 
 
103
    {               
 
104
               if(has_index(list,index))
 
105
               {
 
106
                    array valuelist = list[index];
 
107
                    if(has_value(valuelist,value))
 
108
                    {
 
109
                            return 1;
 
110
                    }
 
111
                    
 
112
                    return 0;
 
113
               }
 
114
               
 
115
               return 0;
 
116
    }
 
117
   
 
118
/**
 
119
 * <p>removes an index and all connected values from the list</p>
 
120
 * 
 
121
 * 
 
122
 * @param index - the index to remove
 
123
 * @return success
 
124
 */
 
125
    public int  remove_index(string index) 
 
126
    {     
 
127
             if(has_index(list,index))
 
128
             {  
 
129
                     return m_delete(list, index);;
 
130
             }
 
131
    }
 
132
                     
 
133
/**
 
134
 * <p>get all values of a given index</p>
 
135
 * 
 
136
 * 
 
137
 * @param index - the index of the values
 
138
 * @return values as array
 
139
 */
 
140
    public array  get(string index) 
 
141
    {   
 
142
            if(has_index(list,index))
 
143
            {
 
144
                    return list[index];
 
145
            }
 
146
            else
 
147
                    return ({});
 
148
    }
 
149
    
 
150
    
 
151
/**
 
152
 * <p>checks if a index exists in the list</p>
 
153
 * 
 
154
 * 
 
155
 * @param index - the index to check
 
156
 * @return success
 
157
 */
 
158
    public int  provides_index(string index) 
 
159
    {  
 
160
          return  has_index(list,index);
 
161
    }
 
162
    
 
163
    
 
164
/**
 
165
 * <p>returns the list in the form of mapping(string:array(string))</p>
 
166
 * 
 
167
 * 
 
168
 * @return - the list as mapping 
 
169
 */
 
170
    public mapping  get_list()
 
171
    {
 
172
            return list;
 
173
    }
 
174
    
 
175
    
 
176
/**
 
177
 * <p>get all indexes of the list</p>
 
178
 * 
 
179
 * 
 
180
 * @return array with all identifiers (indexes) 
 
181
 */
 
182
    public array get_indexes()
 
183
    {
 
184
            return indices(list);
 
185
    }