Skip to content

Commit 4a4b5ec

Browse files
committed
refactor: implement a FIFO array and refactor push logic
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 68986f4 commit 4a4b5ec

File tree

34 files changed

+1565
-276
lines changed

34 files changed

+1565
-276
lines changed

lib/node_modules/@stdlib/plot/table/unicode/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ var borders = table.borders;
276276
Data buffer size. If set, this specifies the maximum number of rows which can be rendered. Once an internal data buffer is full, each new row results in the oldest row being removed.
277277

278278
```javascript
279+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
280+
279281
var table = new UnicodeTable();
280282

281283
// Set:
@@ -287,12 +289,12 @@ var size = table.bufferSize;
287289

288290
table.data = [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ];
289291

290-
var data = table.data;
292+
var data = ndarray2array( table.data );
291293
// returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
292294

293295
table.push( [ 7, 8 ] );
294296

295-
data = table.data;
297+
data = ndarray2array( table.data );
296298
// returns [ [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ]
297299
```
298300

@@ -419,6 +421,8 @@ var data = ndarray2array( table.data );
419421
// returns [ [ 1, 2 ], [ 3, 4 ] ]
420422
```
421423

424+
The returned [ndarray][@stdlib/ndarray/ctor] is a **read-only** view of table data.
425+
422426
<a name="prop-headers"></a>
423427

424428
#### UnicodeTable.prototype.headers
@@ -828,7 +832,9 @@ Renders a Unicode table.
828832
var headers = [ 'col1', 'col2', 'col3' ];
829833
var data = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ];
830834

831-
var table = new UnicodeTable( data, headers );
835+
var table = new UnicodeTable( data, {
836+
'headers': headers
837+
});
832838

833839
var str = table.render();
834840
// returns '...'

lib/node_modules/@stdlib/plot/table/unicode/examples/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,38 @@ var table = new UnicodeTable( data, {
4545
var str = table.render();
4646
console.log( str );
4747
// => '...'
48+
49+
// Push new data to the table:
50+
table.push( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] );
51+
52+
// Re-render the table:
53+
str = table.render();
54+
console.log( str );
55+
// => '...'
56+
57+
// Limit the number of table rows:
58+
table.bufferSize = 10;
59+
60+
// Re-render the table:
61+
str = table.render();
62+
console.log( str );
63+
// => '...'
64+
65+
// Push new data to the table:
66+
table.push( [ 6.0, 7.0, 8.0, 9.0, 10.0 ] );
67+
68+
// Re-render the table:
69+
str = table.render();
70+
console.log( str );
71+
// => '...'
72+
73+
// Increase the number of allowed table rows:
74+
table.bufferSize = 15;
75+
76+
// Push new data to the table:
77+
table.push( [ 11.0, 12.0, 13.0, 14.0, 15.0 ] );
78+
79+
// Re-render the table:
80+
str = table.render();
81+
console.log( str );
82+
// => '...'
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
24+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
25+
var FifoArray = require( './fifo.js' );
26+
27+
28+
// VARIABLES //
29+
30+
var DTYPE = 'generic';
31+
var ORDER = 'row-major';
32+
33+
34+
// MAIN //
35+
36+
/**
37+
* Creates a two-dimensional ndarray having a specified data buffer and shape.
38+
*
39+
* @private
40+
* @param {Collection} buffer - data buffer
41+
* @param {NonNegativeIntegerArray} shape - shape
42+
* @returns {ndarray} two-dimensional ndarray
43+
*/
44+
function array2matrix( buffer, shape ) {
45+
return new ndarray( DTYPE, new FifoArray( buffer ), shape, shape2strides( shape, ORDER ), 0, ORDER ); // eslint-disable-line max-len
46+
}
47+
48+
49+
// EXPORTS //
50+
51+
module.exports = array2matrix;
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
24+
var flatten2d = require( '@stdlib/array/base/flatten2d' );
25+
var shape = require( '@stdlib/array/shape' );
26+
var objectKeys = require( '@stdlib/utils/keys' );
27+
var array2matrix = require( './array2matrix.js' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Converts table data provided as an array-like object.
34+
*
35+
* @private
36+
* @param {Collection} data - input data
37+
* @param {(ObjectArray|null)} headers - table headers
38+
* @throws {TypeError} invalid input data
39+
* @returns {Object} table data
40+
*/
41+
function convertCollection( data, headers ) {
42+
var keys;
43+
var out;
44+
var flg;
45+
var obj;
46+
var sh;
47+
var d;
48+
var i;
49+
var j;
50+
var k;
51+
52+
obj = {
53+
'data': null,
54+
'headers': null,
55+
'code': ''
56+
};
57+
58+
sh = shape( data );
59+
60+
// If we were provided a nested array, flatten in row-major order...
61+
if ( sh.length >= 2 ) {
62+
sh = [ sh[ 0 ], sh[ 1 ] ]; // only concern ourselves with the first two dimensions
63+
obj.data = array2matrix( flatten2d( data, sh, false ), sh );
64+
return obj;
65+
}
66+
// If we were provided a one-dimensional array, assume that we were provided a list of objects...
67+
68+
// If we don't already have headers, infer the headers from the first array element...
69+
if ( headers === null ) {
70+
keys = objectKeys( data[ 0 ] );
71+
flg = true;
72+
} else {
73+
keys = headers;
74+
}
75+
out = [];
76+
for ( i = 0; i < sh[ 0 ]; i++ ) {
77+
d = data[ i ];
78+
for ( j = 0; j < keys.length; j++ ) {
79+
k = keys[ j ];
80+
if ( hasOwnProp( d, k ) ) {
81+
out.push( d[ k ] );
82+
} else {
83+
obj.headers = keys;
84+
obj.code = 'ERR_MISSING_COLUMNS';
85+
return obj;
86+
}
87+
}
88+
}
89+
obj.data = array2matrix( out, [ sh[ 0 ], keys.length ] );
90+
obj.headers = ( flg ) ? keys : null;
91+
return obj;
92+
}
93+
94+
95+
// EXPORTS //
96+
97+
module.exports = convertCollection;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var isCollection = require( '@stdlib/assert/is-collection' );
24+
var objectKeys = require( '@stdlib/utils/keys' );
25+
var array2matrix = require( './array2matrix.js' );
26+
27+
28+
// MAIN //
29+
30+
/**
31+
* Converts table data provided as a dictionary of columns.
32+
*
33+
* @private
34+
* @param {*} data - input data
35+
* @param {(ObjectArray|null)} headers - table headers
36+
* @throws {TypeError} invalid input data
37+
* @returns {Object} table data
38+
*/
39+
function convertDictionary( data, headers ) {
40+
var keys;
41+
var out;
42+
var flg;
43+
var obj;
44+
var col;
45+
var M;
46+
var N;
47+
var i;
48+
var j;
49+
var k;
50+
51+
obj = {
52+
'data': null,
53+
'headers': null,
54+
'code': ''
55+
};
56+
57+
// If we don't already have headers, infer the headers from the provided object...
58+
if ( headers === null ) {
59+
keys = objectKeys( data );
60+
flg = true;
61+
} else {
62+
keys = headers;
63+
}
64+
// If provided an empty object (or an object without enumerable properties), likely a user error...
65+
if ( keys.length === 0 ) {
66+
obj.code = 'ERR_EMPTY_OBJECT';
67+
return obj;
68+
}
69+
// Validate that property value is a collection (column) of data having the same length...
70+
col = data[ keys[ 0 ] ];
71+
if ( !isCollection( col ) ) {
72+
obj.code = 'ERR_INVALID_OBJECT';
73+
return obj;
74+
}
75+
M = col.length;
76+
N = keys.length;
77+
for ( i = 1; i < N; i++ ) {
78+
col = data[ keys[ i ] ];
79+
if ( !isCollection( col ) ) {
80+
obj.code = 'ERR_INVALID_OBJECT';
81+
return obj;
82+
}
83+
if ( col.length !== M ) {
84+
obj.code = 'ERR_UNEQUAL_COLUMN_LENGTHS';
85+
return obj;
86+
}
87+
}
88+
// Flatten the dictionary of columns into a row-major linear buffer...
89+
out = [];
90+
for ( i = 0; i < M; i++ ) {
91+
for ( j = 0; j < N; j++ ) {
92+
k = keys[ j ];
93+
out.push( data[ k ][ i ] );
94+
}
95+
}
96+
obj.data = array2matrix( out, [ M, N ] );
97+
obj.headers = ( flg ) ? keys : null;
98+
return obj;
99+
}
100+
101+
102+
// EXPORTS //
103+
104+
module.exports = convertDictionary;

0 commit comments

Comments
 (0)