update NOTICE and LICENSE
[nit.git] / tests / test_operator_brackets.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-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 # This module tests multivalue brackets
18
19 class Matrice
20 # A matrice of integers
21
22 fun [](x: Int, y: Int): Int
23 # The integer at (x,y)
24 do
25 return _tab[x][y]
26 end
27
28 fun []=(x: Int, y: Int, v: Int)
29 # Put v in (x, y)
30 do
31 # Buld more arrays if needed
32 if x >= _tab.length then
33 var i = _tab.length
34 while i <= x do
35 _tab[i] = new Array[Int]
36 i = i + 1
37 end
38 end
39 # Put the value
40 var row = _tab[x]
41 if y > row.length then
42 var j = row.length
43 while j < y do
44 row[j] = 0
45 j = j + 1
46 end
47 end
48 row[y] = v
49 end
50
51 private
52 var _tab: Array[Array[Int]] # An array of array to store items
53
54
55 init
56 # Build an empty matrice
57 do
58 _tab = new Array[Array[Int]]
59 end
60 end
61
62
63 # Main program
64
65 var m = new Matrice
66 m[1,1] = 11
67 m[2,1] = 21
68 m[5,5] = 55
69 printn(m[1,1])
70 printn(m[2,1])
71 printn(m[5,5])