@@ -11,7 +11,6 @@ This Source Code Form is subject to the terms of the
1111using OneScript . Values ;
1212using ScriptEngine . Machine ;
1313using ScriptEngine . Machine . Contexts ;
14- using System ;
1514using System . Collections . Generic ;
1615
1716namespace OneScript . StandardLibrary . Collections
@@ -95,10 +94,7 @@ public override IEnumerator<IValue> GetEnumerator()
9594 [ ContextMethod ( "Добавить" , "Add" ) ]
9695 public void Add ( IValue value = null )
9796 {
98- if ( value == null )
99- _values . Add ( ValueFactory . Create ( ) ) ;
100- else
101- _values . Add ( value ) ;
97+ _values . Add ( value ?? ValueFactory . Create ( ) ) ;
10298 }
10399
104100 [ ContextMethod ( "Вставить" , "Insert" ) ]
@@ -110,24 +106,14 @@ public void Insert(int index, IValue value = null)
110106 if ( index > _values . Count )
111107 Extend ( index - _values . Count ) ;
112108
113- if ( value == null )
114- _values . Insert ( index , ValueFactory . Create ( ) ) ;
115- else
116- _values . Insert ( index , value ) ;
109+ _values . Insert ( index , value ?? ValueFactory . Create ( ) ) ;
117110 }
118111
119112 [ ContextMethod ( "Найти" , "Find" ) ]
120113 public IValue Find ( IValue what )
121114 {
122115 var idx = _values . FindIndex ( x => x . StrictEquals ( what ) ) ;
123- if ( idx < 0 )
124- {
125- return ValueFactory . Create ( ) ;
126- }
127- else
128- {
129- return ValueFactory . Create ( idx ) ;
130- }
116+ return idx >= 0 ? ValueFactory . Create ( idx ) : ValueFactory . Create ( ) ;
131117 }
132118
133119 [ ContextMethod ( "Удалить" , "Delete" ) ]
@@ -171,15 +157,17 @@ private void Extend(int count)
171157 }
172158 }
173159
174- private static void FillArray ( ArrayImpl array , int bound )
160+ private static ArrayImpl CreateArray ( int bound )
175161 {
162+ var array = new ArrayImpl ( bound ) ;
176163 for ( int i = 0 ; i < bound ; i ++ )
177164 {
178165 array . _values . Add ( ValueFactory . Create ( ) ) ;
179166 }
167+ return array ;
180168 }
181169
182- private static IValue CloneArray ( ArrayImpl cloneable )
170+ private static ArrayImpl CloneArray ( ArrayImpl cloneable )
183171 {
184172 ArrayImpl clone = new ArrayImpl ( cloneable . _values . Count ) ;
185173 foreach ( var item in cloneable . _values )
@@ -209,24 +197,23 @@ public static ArrayImpl Constructor(IValue[] dimensions)
209197 }
210198
211199 // fail fast
200+ int size = 0 ;
212201 for ( int dim = 0 ; dim < dimensions . Length ; dim ++ )
213202 {
214203 if ( dimensions [ dim ] == null )
215204 throw RuntimeException . InvalidNthArgumentType ( dim + 1 ) ;
216205
217- if ( ( int ) dimensions [ dim ] . AsNumber ( ) <= 0 )
206+ size = ( int ) dimensions [ dim ] . AsNumber ( ) ;
207+ if ( size <= 0 )
218208 throw RuntimeException . InvalidNthArgumentValue ( dim + 1 ) ;
219209 }
220210
221- int bound = ( int ) dimensions [ ^ 1 ] . AsNumber ( ) ;
222- var newInst = new ArrayImpl ( bound ) ;
223- FillArray ( newInst , bound ) ;
211+ var newInst = CreateArray ( size ) ; // длина по последней размерности
224212
225- ArrayImpl nested ;
226213 for ( int dim = dimensions . Length - 2 ; dim >= 0 ; dim -- ) // если размерность >= 2
227214 {
228- nested = newInst ;
229- bound = ( int ) dimensions [ dim ] . AsNumber ( ) ;
215+ ArrayImpl nested = newInst ;
216+ int bound = ( int ) dimensions [ dim ] . AsNumber ( ) ;
230217
231218 newInst = new ArrayImpl ( bound ) ;
232219 for ( int i = 0 ; i < bound ; i ++ )
0 commit comments