Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / bench_complex_sort.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2006-2008 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17
18 interface Elt
19 fun val1: Int is abstract
20 fun val2: Int do return val1
21 end
22
23 class A
24 super Elt
25 var a: Int
26 redef fun val1: Int do return a
27
28 redef fun to_s do return "Aa{a}"
29 end
30
31 class Elt2
32 super Elt
33 var b: Int
34 redef fun val1: Int do return b/2
35 redef fun val2: Int do return b
36 end
37
38 class B
39 super Elt2
40 redef fun to_s do return "Bb{b}"
41 end
42
43 class C
44 super Elt
45 var c: Int
46 var d: Int
47 redef fun val1: Int do return c
48 redef fun val2: Int do return d
49
50 redef fun to_s do return "Cc{c}d{d}"
51 end
52
53 class D
54 super A
55 super Elt2
56 redef fun val1: Int do return a
57 redef fun val2: Int do return b
58
59 autoinit a=, b=
60
61 redef fun to_s do return "Da{a}b{b}"
62 end
63
64 class E
65 super Elt2
66 redef fun val1: Int do return 5
67
68 redef fun to_s do return "Eb{b}"
69 end
70
71 class EltComparator
72 super Comparator
73 redef type COMPARED: Elt
74 redef fun compare(a, b)
75 do
76 if is_val1 then
77 return a.val1 <=> b.val1
78 else
79 return a.val2 <=> b.val2
80 end
81 end
82
83 fun toggle
84 do
85 is_val1 = not is_val1
86 end
87
88 var is_val1: Bool = false
89
90 init do end
91 end
92
93 fun generator: Elt
94 do
95 var r = 5.rand
96 if r == 0 then
97 return new A(10.rand)
98 else if r == 1 then
99 return new B(10.rand)
100 else if r == 2 then
101 return new C(10.rand, 10.rand)
102 else if r == 3 then
103 return new D(10.rand, 10.rand)
104 else
105 return new E(10.rand)
106 end
107 end
108
109 srand_from(0)
110 var n = 20
111
112 if not args.is_empty then
113 n = args.first.to_i
114 end
115
116 var array = new Array[Elt].with_capacity(n)
117 for i in [0..n[ do
118 array.push(generator)
119 end
120
121 print array.join(", ")
122
123 var comparator = new EltComparator
124 for i in [0..n[ do
125 comparator.sort(array)
126 comparator.toggle
127 end
128
129 print array.join(", ")