Skip to content

Commit f629701

Browse files
committed
Auto-generated commit
1 parent d42aa08 commit f629701

File tree

5 files changed

+74
-51
lines changed

5 files changed

+74
-51
lines changed

CONTRIBUTORS

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Justin Dennison <[email protected]>
1616
1717
Matt Cochrane <[email protected]>
1818
Milan Raj <[email protected]>
19+
Momtchil Momtchev <[email protected]>
1920
Ognjen Jevremović <[email protected]>
2021
Philipp Burckhardt <[email protected]>
2122
Ricky Reusser <[email protected]>

docs/repl.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
value: any
9898
Fill value.
9999

100-
iterable: Object
100+
iterable: Iterable
101101
Iterable from which to generate an array.
102102

103103
dtype: string (optional)

docs/types/index.d.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ declare function filledarray( dtype?: string ): ArrayOrTypedArray;
7070
*
7171
* @example
7272
* var arr = filledarray( 1.0, 5 );
73-
* // returns <Float64Array>[ 0.0, 0.0, 0.0, 0.0, 0.0 ]
73+
* // returns <Float64Array>[ 1.0, 1.0, 1.0, 1.0, 1.0 ]
7474
*
7575
* @example
7676
* var arr = filledarray( 1.0, 5, 'float32' );
77-
* // returns <Float32Array>[ 0.0, 0.0, 0.0, 0.0, 0.0 ]
77+
* // returns <Float32Array>[ 1.0, 1.0, 1.0, 1.0, 1.0 ]
7878
*/
7979
declare function filledarray( value: any, length: number, dtype?: string ): ArrayOrTypedArray; // tslint:disable-line:max-line-length
8080

@@ -154,7 +154,6 @@ declare function filledarray( value: any, iterable: IterableIterator, dtype?: st
154154
* var buf = new ArrayBuffer( 32 );
155155
* var arr = filledarray( 1.0, buf, 8, 2, 'float32' );
156156
* // returns <Float32Array>[ 1.0, 1.0 ]
157-
*
158157
*/
159158
declare function filledarray( value: any, buffer: ArrayBuffer, byteOffset: number, length: number, dtype?: string ): TypedArray; // tslint:disable-line:max-line-length
160159

@@ -185,7 +184,6 @@ declare function filledarray( value: any, buffer: ArrayBuffer, byteOffset: numbe
185184
* var buf = new ArrayBuffer( 32 );
186185
* var arr = filledarray( 1.0, buf, 8, 'float32' );
187186
* // returns <Float32Array>[ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ]
188-
*
189187
*/
190188
declare function filledarray( value: any, buffer: ArrayBuffer, byteOffset: number, dtype?: string ): TypedArray; // tslint:disable-line:max-line-length
191189

@@ -215,7 +213,6 @@ declare function filledarray( value: any, buffer: ArrayBuffer, byteOffset: numbe
215213
* var buf = new ArrayBuffer( 32 );
216214
* var arr = filledarray( 1.0, buf, 'float32' );
217215
* // returns <Float32Array>[ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ]
218-
*
219216
*/
220217
declare function filledarray( value: any, buffer: ArrayBuffer, dtype?: string ): TypedArray; // tslint:disable-line:max-line-length unified-signatures
221218

docs/types/test.ts

+17-22
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function next(): any {
8181
filledarray( 1.0, buf, 8, 2, 'int16' ); // $ExpectType TypedArray
8282
}
8383

84-
// The compiler throws an error if the function is not provided a valid length, typed array, array-like object, or `ArrayBuffer` argument...
84+
// The compiler throws an error if the function is not provided a valid length, typed array, array-like object, `ArrayBuffer`, or iterable argument...
8585
{
8686
filledarray( 1.0, false ); // $ExpectError
8787
filledarray( 1.0, true ); // $ExpectError
@@ -146,6 +146,13 @@ function next(): any {
146146
filledarray( 1.0, buf, {} ); // $ExpectError
147147
filledarray( 1.0, buf, ( x: number ): number => x ); // $ExpectError
148148

149+
filledarray( 1.0, buf, false, 2 ); // $ExpectError
150+
filledarray( 1.0, buf, true, 2 ); // $ExpectError
151+
filledarray( 1.0, buf, null, 2 ); // $ExpectError
152+
filledarray( 1.0, buf, [], 2 ); // $ExpectError
153+
filledarray( 1.0, buf, {}, 2 ); // $ExpectError
154+
filledarray( 1.0, buf, ( x: number ): number => x, 2 ); // $ExpectError
155+
149156
filledarray( 1.0, buf, '5', 'float64' ); // $ExpectError
150157
filledarray( 1.0, buf, false, 'float64' ); // $ExpectError
151158
filledarray( 1.0, buf, true, 'float64' ); // $ExpectError
@@ -154,6 +161,15 @@ function next(): any {
154161
filledarray( 1.0, buf, [], 'float64' ); // $ExpectError
155162
filledarray( 1.0, buf, {}, 'float64' ); // $ExpectError
156163
filledarray( 1.0, buf, ( x: number ): number => x, 'float64' ); // $ExpectError
164+
165+
filledarray( 1.0, buf, '5', 2, 'float64' ); // $ExpectError
166+
filledarray( 1.0, buf, false, 2, 'float64' ); // $ExpectError
167+
filledarray( 1.0, buf, true, 2, 'float64' ); // $ExpectError
168+
filledarray( 1.0, buf, null, 2, 'float64' ); // $ExpectError
169+
filledarray( 1.0, buf, undefined, 2, 'float64' ); // $ExpectError
170+
filledarray( 1.0, buf, [], 2, 'float64' ); // $ExpectError
171+
filledarray( 1.0, buf, {}, 2, 'float64' ); // $ExpectError
172+
filledarray( 1.0, buf, ( x: number ): number => x, 2, 'float64' ); // $ExpectError
157173
}
158174

159175
// The compiler throws an error if the function is provided a view length argument which is not a number...
@@ -177,27 +193,6 @@ function next(): any {
177193
filledarray( 1.0, buf, 8, ( x: number ): number => x, 'float64' ); // $ExpectError
178194
}
179195

180-
// The compiler throws an error if the function is not provided a third argument which is a number...
181-
{
182-
const buf = new ArrayBuffer( 32 );
183-
184-
filledarray( 1.0, buf, false ); // $ExpectError
185-
filledarray( 1.0, buf, true ); // $ExpectError
186-
filledarray( 1.0, buf, null ); // $ExpectError
187-
filledarray( 1.0, buf, [] ); // $ExpectError
188-
filledarray( 1.0, buf, {} ); // $ExpectError
189-
filledarray( 1.0, buf, ( x: number ): number => x ); // $ExpectError
190-
191-
filledarray( 1.0, buf, '5', 'float64' ); // $ExpectError
192-
filledarray( 1.0, buf, false, 'float64' ); // $ExpectError
193-
filledarray( 1.0, buf, true, 'float64' ); // $ExpectError
194-
filledarray( 1.0, buf, null, 'float64' ); // $ExpectError
195-
filledarray( 1.0, buf, undefined, 'float64' ); // $ExpectError
196-
filledarray( 1.0, buf, [], 'float64' ); // $ExpectError
197-
filledarray( 1.0, buf, {}, 'float64' ); // $ExpectError
198-
filledarray( 1.0, buf, ( x: number ): number => x, 'float64' ); // $ExpectError
199-
}
200-
201196
// The compiler throws an error if the function is provided too many arguments...
202197
{
203198
const buf = new ArrayBuffer( 32 );

lib/main.js

+53-23
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,66 @@ var ITERATOR_SYMBOL = require( '@stdlib/symbol-iterator' );
3838
var HAS_ITERATOR_SYMBOL = hasIteratorSymbolSupport();
3939

4040

41+
// FUNCTIONS //
42+
43+
/**
44+
* Creates a filled "generic" array.
45+
*
46+
* @private
47+
* @param {NonNegativeInteger} len - array length
48+
* @param {*} value - fill value
49+
* @returns {Array} filled array
50+
*/
51+
function filled( len, value ) {
52+
var arr;
53+
var i;
54+
55+
// Manually push elements in order to ensure "fast" elements...
56+
arr = [];
57+
for ( i = 0; i < len; i++ ) {
58+
arr.push( value );
59+
}
60+
return arr;
61+
}
62+
63+
/**
64+
* Creates a filled "generic" array from an iterator.
65+
*
66+
* @private
67+
* @param {Iterator} it - iterator
68+
* @param {*} value - fill value
69+
* @returns {Array} filled array
70+
*/
71+
function filledIterator( it, value ) {
72+
var arr;
73+
var v;
74+
75+
arr = [];
76+
while ( true ) {
77+
v = it.next();
78+
if ( hasOwnProp( v, 'value' ) ) {
79+
arr.push( value );
80+
}
81+
if ( v.done ) {
82+
break;
83+
}
84+
}
85+
return arr;
86+
}
87+
88+
4189
// MAIN //
4290

4391
/**
4492
* Creates a filled array.
4593
*
4694
* @param {*} [value] - fill value
47-
* @param {(NonNegativeInteger|TypedArray|ArrayLikeObject|ArrayBuffer)} [arg] - a length, typed array, array-like object, or buffer
95+
* @param {(NonNegativeInteger|TypedArray|ArrayLikeObject|ArrayBuffer|Iterable)} [arg] - a length, typed array, array-like object, buffer, or iterable
4896
* @param {NonNegativeInteger} [byteOffset=0] - byte offset
4997
* @param {NonNegativeInteger} [length] - view length
5098
* @param {string} [dtype="float64"] - data type
5199
* @throws {TypeError} must provide a recognized data type
52-
* @throws {TypeError} must provide a length, typed array, array-like object, or buffer
100+
* @throws {TypeError} must provide a length, typed array, array-like object, buffer, or iterable
53101
* @throws {Error} creating a generic array from an `ArrayBuffer` is not supported
54102
* @returns {(TypedArray|Array)} array or typed array
55103
*
@@ -137,8 +185,6 @@ function filledarray() {
137185
var arr;
138186
var len;
139187
var arg;
140-
var v;
141-
var i;
142188

143189
nargs = arguments.length;
144190
nargs -= 1;
@@ -165,13 +211,7 @@ function filledarray() {
165211
len = arg.length;
166212
}
167213
if ( len !== void 0 ) {
168-
arr = [];
169-
170-
// Manually push elements in order to ensure "fast" elements...
171-
for ( i = 0; i < len; i++ ) {
172-
arr.push( value );
173-
}
174-
return arr;
214+
return filled( len, value );
175215
}
176216
if ( isArrayBuffer( arg ) ) {
177217
throw new Error( 'invalid arguments. Creating a generic array from an ArrayBuffer is not supported.' );
@@ -187,17 +227,7 @@ function filledarray() {
187227
if ( !isFunction( arg.next ) ) {
188228
throw new TypeError( 'invalid argument. Must provide a length, typed array, array-like object, or an iterable.' );
189229
}
190-
arr = [];
191-
while ( true ) {
192-
v = arg.next();
193-
if ( hasOwnProp( v, 'value' ) ) {
194-
arr.push( value );
195-
}
196-
if ( v.done ) {
197-
break;
198-
}
199-
}
200-
return arr;
230+
return filledIterator( arg, value );
201231
}
202232
throw new TypeError( 'invalid argument. Must provide a length, typed array, array-like object, or an iterable. Value: `'+arg+'`.' );
203233
} else if ( isArrayBuffer( arg ) ) {
@@ -216,7 +246,7 @@ function filledarray() {
216246
arr = new ctor( arguments[1], arguments[2], arguments[3] );
217247
}
218248
value = arguments[ 0 ];
219-
if ( arr.length > 0 && value !== 0 ) { // i.e., nonzero
249+
if ( arr.length > 0 && value !== 0 ) { // we only need to fill a typed array if the fill value isn't zero, as typed arrays are always zero-initialized
220250
gfill( arr.length, value, arr, 1 );
221251
}
222252
return arr;

0 commit comments

Comments
 (0)