source: experiments/frams/logic/data/scripts/lists_processing.inc @ 169

Last change on this file since 169 was 169, checked in by Maciej Komosinski, 10 years ago

A library of logic functions, an experiment definition that generates abductive hypotheses, and a script that runs sample experiments

File size: 2.7 KB
Line 
1//This file contains list manipulation functions.
2
3function verifyPlainListsIdentity(list1, list2)
4{
5       
6        if(list1.size != list2.size)
7                return 0;
8        else
9        {
10                var i;
11               
12                for(i = 0; i < list1.size; i++)
13                {
14                        if(list1[i] != list2[i])
15                                return 0;
16                }
17               
18                return 1;
19                               
20        }
21}
22
23function findPlainList(vector, list)
24{
25        var i;
26       
27        for(i = 0; i < vector.size; i++)
28        {
29                if(verifyPlainListsIdentity(vector[i], list) == 1)
30                        return i;
31        }
32       
33        return -1;
34}
35
36function copyPlainList(list)
37{
38        var i;
39        var newList = Vector.new();
40       
41        for(i = 0; i < list.size; i++)
42                newList.add(list[i]);
43               
44        return newList;
45}
46
47function deepCopyList(arg)
48{
49  if (typeof(arg) == "Vector")
50  {
51    var i, v = Vector.new();
52    for (i = 0;i < arg.size;i++)
53      v.add(deepCopyList(arg[i]));
54    return v;
55  }
56  else
57    return arg;
58}
59
60function plainListRight(list, howMany)
61{
62        var right = Vector.new();
63        var i;
64       
65        for(i = list.size-howMany; i < list.size; i++)
66                right.add(list[i]);
67       
68        return right;
69}
70
71function plainListLeft(list, howMany)
72{
73        var left = Vector.new();
74        var i;
75       
76        for(i = 0; i <= howMany; i++)
77        {
78                left.add(list[i]);
79        }
80       
81        return left;
82}
83
84function plainListSubList(list, start, stop)
85{
86        var i;
87        var subList = Vector.new();
88       
89        for(i = start; i <= stop; i++)
90        {
91                subList.add(list[i]);
92        }
93       
94        return subList;
95}
96
97function plainListReplace(list, index, replaceList)
98{
99        var new = Vector.new();
100        var i;
101       
102        for(i = 0; i < index; i++)
103        {
104                new.add(list[i]);
105        }
106       
107        for(i = 0; i < replaceList.size; i++)
108        {
109                new.add(replaceList[i]);
110        }
111       
112        for(i = index + 1; i < list.size; i++)
113        {
114                new.add(list[i]);
115        }
116       
117        return new;
118       
119}
120
121function mergePlainLists(list1, list2)
122{
123        var i;
124        var new = Vector.new();
125       
126        for(i = 0; i < list1.size; i++)
127                new.add(list1[i]);
128               
129        for(i = 0; i < list2.size; i++)
130                new.add(list2[i]);
131               
132        return new;
133}
134
135function plainListCopyInsert(list, index, elementsList)
136{
137        var new = Vector.new();
138        var i;
139       
140        for(i = 0; i < index; i++)
141        {
142                new.add(list[i]);
143        }
144       
145        for(i = 0; i < elementsList.size; i++)
146        {
147                new.add(elementsList[i]);
148        }
149       
150        for(i = index; i < list.size; i++)
151        {
152                new.add(list[i]);
153        }
154       
155        return new;
156}
157
158function listRemove(indexes, list)
159{
160        var listDeepCopy = deepCopyList(list);
161       
162        var i;
163       
164        for(i = 0; i < indexes.size; i++)
165        {
166                listDeepCopy.set(indexes[i],null);
167        }
168       
169        for(i = 0; i < listDeepCopy.size; i++)
170                if(listDeepCopy[i] == null)
171                {
172                        listDeepCopy.remove(i);
173                        i--;
174                }
175               
176        return listDeepCopy;
177}
178
179function listInsert(list,element,position)
180{
181        if(position >= list.size)
182                list.set(position, element);
183        else
184        {
185                var temp = Vector.new();
186                var i;
187                for(i = position; i < list.size; i++)
188                        temp.add(list[i]);
189                       
190                list.set(position,element);
191               
192                for(i = 0; i < temp.size; i++)
193                        list.set(position + 1 + i,temp[i]);
194        }
195       
196}
Note: See TracBrowser for help on using the repository browser.