1 | //This file contains miscellaneous support functions for the abducibles generation experiment definition. |
---|
2 | //These include write-to-file function, a sorting algorithm and some combinatorial functions. |
---|
3 | |
---|
4 | |
---|
5 | function writeInfo(file_name,info) |
---|
6 | { |
---|
7 | var f=File.appendDirect("" + file_name,"description"); |
---|
8 | f.writeString(info); |
---|
9 | f.close(); |
---|
10 | } |
---|
11 | |
---|
12 | function quickSort(listOfNumbers,sorted) |
---|
13 | { |
---|
14 | var l=0,r=1; |
---|
15 | var stack = Vector.new(); |
---|
16 | stack.add(listOfNumbers); |
---|
17 | while(stack.size > 0) |
---|
18 | { |
---|
19 | if(stack[0].size > 1) |
---|
20 | { |
---|
21 | while(l < r) |
---|
22 | { |
---|
23 | l=1; |
---|
24 | r = stack[0].size - 1; |
---|
25 | for(l; l < stack[0].size - 1; l++) |
---|
26 | if(stack[0][0] <= stack[0][l]) |
---|
27 | break; |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | for(r; r > -1; r--) |
---|
32 | if(stack[0][0] > stack[0][r]) |
---|
33 | break; |
---|
34 | |
---|
35 | if(l < r) |
---|
36 | { |
---|
37 | var temp_l = stack[0][l]; |
---|
38 | stack[0].set(l, stack[0][r]); |
---|
39 | stack[0].set(r, temp_l); |
---|
40 | } |
---|
41 | |
---|
42 | if(l == stack[0].size - 1) |
---|
43 | { |
---|
44 | if(stack[0][0] > stack[0][l]) |
---|
45 | { |
---|
46 | var temp_l = stack[0][0]; |
---|
47 | stack[0].set(0, stack[0][l]); |
---|
48 | stack[0].set(l, temp_l); |
---|
49 | } |
---|
50 | } |
---|
51 | } |
---|
52 | var left = Vector.new(); |
---|
53 | var right = Vector.new(); |
---|
54 | var i; |
---|
55 | |
---|
56 | for(i = 0; i < l; i++) |
---|
57 | left.add(stack[0][i]); |
---|
58 | for(i = l; i < stack[0].size; i++) |
---|
59 | right.add(stack[0][i]); |
---|
60 | |
---|
61 | stack.remove(0); |
---|
62 | listInsert(stack,left,0); |
---|
63 | listInsert(stack,right,1); |
---|
64 | l = 0; |
---|
65 | r = stack[0].size; |
---|
66 | } |
---|
67 | else |
---|
68 | { |
---|
69 | sorted.add(stack[0][0]); |
---|
70 | stack.remove(0); |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | function generateBinaryCombinations(size) |
---|
77 | { |
---|
78 | var stack = Vector.new(); |
---|
79 | var i; |
---|
80 | |
---|
81 | stack.add(0); |
---|
82 | var counter = 0; |
---|
83 | var finish = -1; |
---|
84 | var combinations = Vector.new(); |
---|
85 | while(stack.size > 0) |
---|
86 | { |
---|
87 | var z_counter = size - 1; |
---|
88 | if(stack.size < size) |
---|
89 | { |
---|
90 | stack.add(0); |
---|
91 | continue; |
---|
92 | } |
---|
93 | else |
---|
94 | { |
---|
95 | if(stack.find(1) != -1) |
---|
96 | { |
---|
97 | counter++; |
---|
98 | var temp_bin = Vector.new(); |
---|
99 | for(i = 0; i < stack.size; i++) |
---|
100 | temp_bin.add(stack[i]); |
---|
101 | |
---|
102 | combinations.add(temp_bin); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | if(stack.get(size - 1) == 0) |
---|
107 | { |
---|
108 | stack.set(size - 1, 1); |
---|
109 | continue; |
---|
110 | } |
---|
111 | |
---|
112 | if(stack.get(size - 1) == 1) |
---|
113 | { |
---|
114 | while(stack.get(z_counter) != 0 && z_counter > 0) |
---|
115 | z_counter--; |
---|
116 | |
---|
117 | stack.set(z_counter, 1); |
---|
118 | |
---|
119 | while(z_counter != (stack.size - 1)) |
---|
120 | stack.remove(stack.size - 1); |
---|
121 | |
---|
122 | if(stack.get(0) == 1 && stack.size == 1) |
---|
123 | |
---|
124 | finish++; |
---|
125 | |
---|
126 | if(finish == 1) |
---|
127 | stack.clear(); |
---|
128 | |
---|
129 | } |
---|
130 | } |
---|
131 | return combinations; |
---|
132 | } |
---|