[luavsq] 汎用のListクラスを実装
@@ -0,0 +1,118 @@ | ||
1 | +--[[ | |
2 | + List.lua | |
3 | + Copyright © 2011 kbinani | |
4 | + | |
5 | + This file is part of luavsq. | |
6 | + | |
7 | + luavsq is free software; you can redistribute it and/or | |
8 | + modify it under the terms of the BSD License. | |
9 | + | |
10 | + luavsq is distributed in the hope that it will be useful, | |
11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
13 | +]] | |
14 | + | |
15 | +if( nil == luavsq )then | |
16 | + luavsq = {}; | |
17 | +end | |
18 | + | |
19 | +if( nil == luavsq.List )then | |
20 | + | |
21 | + luavsq.List = {}; | |
22 | + | |
23 | + function luavsq.List.new() | |
24 | + local this = {}; | |
25 | + this._array = {}; | |
26 | + | |
27 | + --- | |
28 | + -- @return [Iterator<TempoTableEntry>] | |
29 | + function this:iterator() | |
30 | + return luavsq.List.Iterator.new( self ); | |
31 | + end | |
32 | + | |
33 | + --- | |
34 | + -- データ点を時刻順に並べ替えます | |
35 | + -- @return [void] | |
36 | + function this:sort( ... ) | |
37 | + local arguments = { ... }; | |
38 | + local wrappedComparator = nil; | |
39 | + if( #arguments > 0 )then | |
40 | + comparator = arguments[1]; | |
41 | + wrappedComparator = function( a, b ) | |
42 | + local actualA = a["value"]; | |
43 | + local actualB = b["value"]; | |
44 | + return comparator( actualA, actualB ); | |
45 | + end | |
46 | + else | |
47 | + wrappedComparator = function( a, b ) | |
48 | + local actualA = a["value"]; | |
49 | + local actualB = b["value"]; | |
50 | + return actualA < actualB; | |
51 | + end | |
52 | + end | |
53 | + table.sort( self._array, wrappedComparator ); | |
54 | + end | |
55 | + | |
56 | + --- | |
57 | + -- データ点を追加します | |
58 | + -- @param value [TempoTableEntry] | |
59 | + -- @return [void] | |
60 | + function this:add( value ) | |
61 | + table.insert( self._array, { ["value"] = value } ); | |
62 | + end | |
63 | + | |
64 | + --- | |
65 | + -- データ点を追加します | |
66 | + -- @param value [TempoTableEntry] | |
67 | + -- @return [void] | |
68 | + function this:push( value ) | |
69 | + self:add( value ); | |
70 | + end | |
71 | + | |
72 | + --- | |
73 | + -- テンポ・テーブルに登録されているデータ点の個数を調べます | |
74 | + -- @return [int] | |
75 | + function this:size() | |
76 | + return #self._array; | |
77 | + end | |
78 | + | |
79 | + --- | |
80 | + -- 第index番目のデータ点を取得します | |
81 | + -- @param index [int] 0 から始まるインデックス | |
82 | + -- @return [TempoTableEntry] | |
83 | + function this:get( index ) | |
84 | + return self._array[index + 1]["value"]; | |
85 | + end | |
86 | + | |
87 | + --- | |
88 | + -- データ点を設定します | |
89 | + -- @param index [int] | |
90 | + -- @param value [TempoTableEntry] | |
91 | + -- @return [void] | |
92 | + function this:set( index, value ) | |
93 | + self._array[index + 1] = { ["value"] = value }; | |
94 | + end | |
95 | + | |
96 | + return this; | |
97 | + end | |
98 | + | |
99 | + luavsq.List.Iterator = {}; | |
100 | + | |
101 | + function luavsq.List.Iterator.new( list ) | |
102 | + local this = {}; | |
103 | + this._list = list; | |
104 | + this._pos = -1; | |
105 | + | |
106 | + function this:hasNext() | |
107 | + return (0 <= self._pos + 1 and self._pos + 1 < self._list:size()) | |
108 | + end | |
109 | + | |
110 | + function this:next() | |
111 | + self._pos = self._pos + 1; | |
112 | + return self._list:get( self._pos ); | |
113 | + end | |
114 | + | |
115 | + return this; | |
116 | + end | |
117 | + | |
118 | +end |
@@ -14,7 +14,7 @@ | ||
14 | 14 | file=`basename $filePath` |
15 | 15 | { |
16 | 16 | echo "require( \"lunit\" );dofile( \"$luavsqPath\" );"; |
17 | - cat $filePath | sed -e '1d' | sed -e "s/^dofile.*$//g" | sed -e "s/^require.*;$//g"; | |
17 | + cat $filePath | sed -e "s/^dofile.*$//g" | sed -e "s/^require.*;$//g" | sed -e '1d'; | |
18 | 18 | } 1> /tmp/$file |
19 | 19 | tmpTestFiles="$tmpTestFiles /tmp/$file" |
20 | 20 | done |
@@ -0,0 +1,52 @@ | ||
1 | +require( "lunit" ); | |
2 | +dofile( "../List.lua" ); | |
3 | +module( "ListTest", package.seeall, lunit.testcase ); | |
4 | + | |
5 | +function test() | |
6 | + local list = luavsq.List.new(); | |
7 | + assert_equal( 0, list:size() ); | |
8 | + list:add( 1 ); | |
9 | + assert_equal( 1, list:size() ); | |
10 | + list:add( nil ); | |
11 | + assert_equal( 2, list:size() ); | |
12 | + assert_equal( 1, list:get( 0 ) ); | |
13 | + assert_equal( nil, list:get( 1 ) ); | |
14 | + list:set( 1, 2 ); | |
15 | + assert_equal( 2, list:get( 1 ) ); | |
16 | +end | |
17 | + | |
18 | +function testIterator() | |
19 | + local list = luavsq.List.new(); | |
20 | + list:add( 0 ); | |
21 | + list:add( 2 ); | |
22 | + list:add( 1 ); | |
23 | + local i = list:iterator(); | |
24 | + assert_true( i:hasNext() ); | |
25 | + assert_equal( 0, i:next() ); | |
26 | + assert_true( i:hasNext() ); | |
27 | + assert_equal( 2, i:next() ); | |
28 | + assert_true( i:hasNext() ); | |
29 | + assert_equal( 1, i:next() ); | |
30 | + assert_false( i:hasNext() ); | |
31 | +end | |
32 | + | |
33 | +function testSort() | |
34 | + local list = luavsq.List.new(); | |
35 | + list:add( 0 ); | |
36 | + list:add( 2 ); | |
37 | + list:add( 1 ); | |
38 | + list:sort(); | |
39 | + | |
40 | + assert_equal( 0, list:get( 0 ) ); | |
41 | + assert_equal( 1, list:get( 1 ) ); | |
42 | + assert_equal( 2, list:get( 2 ) ); | |
43 | + | |
44 | + local desc = function( a, b ) | |
45 | + return a > b; | |
46 | + end | |
47 | + list:sort( desc ); | |
48 | + | |
49 | + assert_equal( 2, list:get( 0 ) ); | |
50 | + assert_equal( 1, list:get( 1 ) ); | |
51 | + assert_equal( 0, list:get( 2 ) ); | |
52 | +end |