| | 154 | == Mutable Version == |
| | 155 | |
| | 156 | |
| | 157 | === Sequences === |
| | 158 | |
| | 159 | This is an abstract datatype for a mutable sequence of elements of the same type. |
| | 160 | |
| | 161 | New type: `$mseq`. Functions: |
| | 162 | |
| | 163 | {{{ |
| | 164 | /* Creates a new mutable sequence allocated in the given |
| | 165 | * scope's heap. The type of the elements that this sequence |
| | 166 | * can hold is specified. The sequence is initially empty. |
| | 167 | * A handle to the new sequence is returned. |
| | 168 | */ |
| | 169 | $mseq $mseq_create($scope scope, $type type); |
| | 170 | |
| | 171 | /* Forms a new mutable sequence by copying the data from an |
| | 172 | * existing sequence of values. The parameter ptr points to the |
| | 173 | * element in the existing sequence of adjacent values in memory. |
| | 174 | * The number of elements is n, and the type of each element is type. |
| | 175 | * The new sequence is allocated in the heap of the specified scope. |
| | 176 | * A handle to the new sequence is returned. |
| | 177 | */ |
| | 178 | $mseq $mseq_create_from_array($scope scope, $type type, void * ptr, int n); |
| | 179 | |
| | 180 | /* Creates a new mutable sequence by copying a subsequence |
| | 181 | * of data from an existing mutable sequence. The elements |
| | 182 | * at indexes start, state+1, ..., stop-1 are copied from the original |
| | 183 | * to create the new sequence, where they will have indexes 0, 1, ..., |
| | 184 | * stop-start-1. The length of the new sequence will be stop-start, |
| | 185 | * unless stop-start is negative, in which case the length will be 0. |
| | 186 | */ |
| | 187 | $mseq $mseq_create_subseq($scope scope, $mseq original, int start int stop); |
| | 188 | |
| | 189 | /* Destroys the mseq specified by the given handle. |
| | 190 | * All memory associated with the mseq is deallocated. |
| | 191 | * The handle then becomes an undefined value. |
| | 192 | * The sequence does not have to be empty when this is called. |
| | 193 | */ |
| | 194 | void $mseq_destroy($mseq mseq); |
| | 195 | |
| | 196 | /* Returns the type of an element that goes in this seq */ |
| | 197 | $type $mseq_get_element_type($mseq mseq); |
| | 198 | |
| | 199 | /* Adds the specified element to the mseq. |
| | 200 | * A runtime exception is generated if the thing pointed |
| | 201 | * to by eltptr doesn't have the right type for the mseq. |
| | 202 | */ |
| | 203 | void $mseq_add($mseq mseq, void * eltptr); |
| | 204 | |
| | 205 | /* Removes the element at position index from the mseq |
| | 206 | * and shifts the subsequence elements down. |
| | 207 | * If result is not NULL, the value removed is written to the memory |
| | 208 | * pointed to by result. If the result is NULL, this is not done. |
| | 209 | */ |
| | 210 | void $mseq_remove($mseq mseq, int index, void * result); |
| | 211 | |
| | 212 | /* Replaces the element at position index with whatever |
| | 213 | * object is pointed to by ptr. |
| | 214 | * If old is not NULL, the old entry at index is copied to the |
| | 215 | * memory specified by old. If old is NULL, this is not done. |
| | 216 | */ |
| | 217 | void $mseq_set($mseq mseq, int index, void * ptr, void * old); |
| | 218 | |
| | 219 | /* Returns the length of the mseq (i.e., the number of elements) */ |
| | 220 | int $mseq_get_length($mseq mseq); |
| | 221 | |
| | 222 | /* Inserts the specified element at the given index. |
| | 223 | * Subsequent elements are shifted up.n |
| | 224 | * The parameter eltptr is a pointer to the element to insert. */ |
| | 225 | void $mseq_insert($mseq mseq, int index, void * eltptr); |
| | 226 | |
| | 227 | /* Gets the element at position index and stores it in wherever |
| | 228 | * ptr points to. */ |
| | 229 | void $mseq_get($mseq mseq, int index, void * ptr); |
| | 230 | |
| | 231 | /* Adds all the elements of s2 to s1. The |
| | 232 | * elements from s2 are copied and appended to the |
| | 233 | * end of s1. The sequence s2 itself is not modified. |
| | 234 | */ |
| | 235 | $mseq $mseq_append($mseq s1, $mseq s2); |
| | 236 | }}} |
| | 237 | |
| | 238 | |
| | 239 | === Sets === |
| | 240 | |
| | 241 | New type: `$set`. Functions: |
| | 242 | |
| | 243 | {{{ |
| | 244 | /* Returns the empty set whose elements (if it had any) |
| | 245 | * would have the given type. The type becomes associated |
| | 246 | * with the set. */ |
| | 247 | $set $set_empty($type type); |
| | 248 | |
| | 249 | /* Returns the cardinality of the set */ |
| | 250 | int $set_get_size($set set); |
| | 251 | |
| | 252 | /* Returns the type of an element that goes in this set */ |
| | 253 | $type $set_get_element_type($set set); |
| | 254 | |
| | 255 | /* Returns the set that is like the given one but with one more |
| | 256 | * element added. A runtime exception is generated if the thing pointed |
| | 257 | * to by eltptr doesn't have the right type for s. */ |
| | 258 | $set $set_add($set set, void * eltptr); |
| | 259 | |
| | 260 | /* Returns the set that is like the given one but with the specified element |
| | 261 | * removed. If the set does not contain the element the set returned |
| | 262 | * will equal the given one. */ |
| | 263 | $set $set_remove($set set, void * eltptr); |
| | 264 | |
| | 265 | /* Does the set contain the specified element? */ |
| | 266 | _Bool $set_contains($set set, void * eltptr); |
| | 267 | |
| | 268 | $set $set_union($set s1, $set s2); |
| | 269 | |
| | 270 | $set $set_intersection($set s1, $set s2); |
| | 271 | |
| | 272 | $set $set_difference($set s1, $set s2); |
| | 273 | |
| | 274 | /* Returns the singleton set consisting of the single element |
| | 275 | * specified by the pointer and the type */ |
| | 276 | $set $set_single($type type, void * eltptr); |
| | 277 | |
| | 278 | /* Returns a sequence consisting of the elements of the set */ |
| | 279 | $seq $set_to_seq($set set); |
| | 280 | }}} |
| | 281 | |
| | 282 | |
| | 283 | === Maps === |
| | 284 | |
| | 285 | `$map` |
| | 286 | |
| | 287 | {{{ |
| | 288 | /* Returns the empty map with the given key type and |
| | 289 | * value type. |
| | 290 | */ |
| | 291 | $map $map_empty($type keyType, $type valType); |
| | 292 | |
| | 293 | /* Returns the map obtained by starting with the given |
| | 294 | * map, removing the entry with the specified key (if |
| | 295 | * one exists) and then adding the specified key-value |
| | 296 | * pair. If that key-value pair already exists in the given map, |
| | 297 | * the map returned is identical to the given on. |
| | 298 | */ |
| | 299 | $map $map_put($map map, void * keyPtr, void * valPtr); |
| | 300 | |
| | 301 | /* If an entry with the specified key exists in the map, it |
| | 302 | * the corresponding value is copied into the region specified |
| | 303 | * by the resultPtr. Otherwise, a no-op. |
| | 304 | */ |
| | 305 | void $map_get($map map, void * keyPtr, void * resultPtr); |
| | 306 | |
| | 307 | /* Returns the map obtained by removing an entry |
| | 308 | * with the specified key, if one exists. |
| | 309 | */ |
| | 310 | $map $map_removeKey($map map, void * keyPtr); |
| | 311 | |
| | 312 | /* Returns the set of keys in the given map. */ |
| | 313 | $set $map_get_keySet($map map); |
| | 314 | |
| | 315 | /* Returns the set of ordered pairs of the form (key,val). |
| | 316 | * Each ordered pair has a struct type consisting of two fields |
| | 317 | * named key and val. The type of the key field is the keyType; |
| | 318 | * the type of the val field is the valType. |
| | 319 | */ |
| | 320 | $set $map_get_entrySet($map map); |
| | 321 | }}} |
| | 322 | |