Debian Codegraph @

Nasal/std/Vector.nas

raw
1 # Copyright (C) 2014 onox
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 #
16 # NOTE! This copyright does *not* cover user models that use these Nasal
17 # services by normal function calls - this is merely considered normal use
18 # of the code, and does *not* fall under the heading of "derived work."
19
20 #load only once (via /Nasal/std.nas) not via C++ module loader
21 if (ishash(globals["std"]) and ishash(std["Vector"]))
22 return;
23
24 var Vector = {
25
26 new: func (vector=nil, name="") {
27 var m = {
28 parents: [Vector],
29 name: name,
30 vector: [],
31 _callback: func,
32 };
33 if (isvec(vector)) {
34 m.vector = vector;
35 }
36 return m;
37 },
38
39 # add callback for writes (insert() and append())
40 # will be called as f(index, item), compare insert() below
41 addCallback: func (f) {
42 if (isfunc(f)) {
43 me._callback = f;
44 return me;
45 }
46 return nil;
47 },
48
49 getName: func () {
50 return me.name;
51 },
52
53 size: func {
54 return size(me.vector);
55 },
56
57 clear: func {
58 me.vector = [];
59 },
60
61 append: func (item) {
62 append(me.vector, item);
63 me._callback(me.size() - 1, item);
64 },
65
66 extend: func (other_vector) {
67 # Extend the vector with another vector, appending the items of
68 # the other vector to this vector
69
70 me.vector = me.vector ~ other_vector;
71 },
72
73 insert: func (index, item) {
74 # Insert the given item at the given index before the old item
75 # at that index. Any index greater than n-1 where n is the size of
76 # the vector will append the given item at the end. Any index smaller
77 # or equal to -n will insert the item at the beginning of the vector.
78 #
79 # For example, if the vector contains ["a", "b", "c"], then the
80 # following operations can happen:
81 #
82 # insert(0, "d") => ["d", "a", "b", "c"]
83 # insert(2, "e") => ["a", "b", "e", "c"]
84 # insert(3, "f") => ["a", "b", "c", "f"]
85 # insert(-3, "g") => ["f", "a", "b", "c"]
86
87 index = math.min(index, me.size());
88 if (index < 0) {
89 index = math.max(0, me.size() + index);
90 }
91 me.vector = subvec(me.vector, 0, index) ~ [item] ~ subvec(me.vector, index);
92 me._callback(index, item);
93 return me.vector;
94 },
95
96 pop: func (index=nil) {
97 # Remove and return the item at the given index. If index
98 # is not given, then it will remove and return the last item.
99 # A negative index represents the position from the end.
100 #
101 # For example, if the vector contains ["a", "b", "c"], then the
102 # following operations can happen:
103 #
104 # pop(0) => "a"
105 # pop(2) => "c"
106 # pop(-1) => "c"
107 # pop(-3) => "a"
108 #
109 # Thus the range is -n .. n-1 where n is the size of the vector.
110 # IndexError is raised if the index is out of range.
111
112 if (index != nil) {
113 if (index < -me.size() or index >= me.size()) {
114 die("IndexError: index out of range");
115 }
116
117 if (index < 0) {
118 index = me.size() + index;
119 }
120 var item = me.vector[index];
121 me.vector = subvec(me.vector, 0, index) ~ subvec(me.vector, index + 1);
122 return item;
123 }
124 else {
125 return pop(me.vector);
126 }
127 },
128
129 index: func (item) {
130 # Return first index of the given item. Raises a ValueError
131 # if the item is not in the vector.
132 var i = vecindex(me.vector, item);
133 if (i == nil) die("ValueError: item not in the vector");
134 return i;
135 },
136
137 contains: func (item) {
138 # Return true if the vector contains the item, false otherwise
139 return vecindex(me.vector, item) != nil;
140 },
141
142 remove: func (item) {
143 # Remove the first occurrence of the given item. Raises a
144 # ValueError if the item is not present in the vector.
145
146 me.pop(me.index(item));
147 }
148
149 };