/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var baseCtor = require( '@stdlib/ndarray/base/ctor' );
var defaults = require( './defaults.json' );
var getKey = require( './key.js' );
var cache = require( './cache.js' );


// VARIABLES //

var CACHE = cache();


// MAIN //

/**
* Returns a memoized ndarray constructor.
*
* @param {string} dtype - data type
* @param {PositiveInteger} ndims - number of dimensions
* @param {Options} [options] - function options
* @param {boolean} [options.codegen=true] - boolean indicating whether to use code generation
* @returns {Function} memoized ndarray constructor
*
* @example
* var f1 = ctor( 'float64', 3 );
* // returns <Function>
*
* var f2 = ctor( 'float64', 3 );
* // returns <Function>
*
* var bool = ( f1 === f2 );
* // returns true
*
* var buffer = [ 1, 2, 3, 4, 5, 6 ];
* var shape = [ 3, 2 ];
* var strides = [ 2, 1 ];
* var offset = 0;
*
* var out = f1( buffer, shape, strides, offset, 'row-major' );
*/
function ctor( dtype, ndims, options ) {
	var ctors;
	var opts;
	var key;
	var len;
	var f;
	var i;

	opts = {};
	if ( arguments.length > 2 ) {
		opts.codegen = ( options.codegen === void 0 ) ? defaults.codegen : options.codegen; // eslint-disable-line max-len
	} else {
		opts.codegen = defaults.codegen;
	}
	key = getKey( dtype, opts );
	ctors = CACHE[ key ];

	// If the constructor has not already been created, create it; otherwise, return a memoized constructor...
	len = ctors.length;
	if ( ndims <= len ) {
		f = ctors[ ndims-1 ];
		if ( f === false ) {
			f = baseCtor( dtype, ndims, opts );
			ctors[ ndims-1 ] = f;
		}
	} else {
		for ( i = len+1; i < ndims; i++ ) {
			ctors.push( false );
		}
		f = baseCtor( dtype, ndims, opts );
		ctors.push( f );
	}
	return f;
}


// EXPORTS //

module.exports = ctor;

/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var baseCtor = require( '@stdlib/ndarray/base/ctor' );
var defaults = require( './defaults.json' );
var getKey = require( './key.js' );
var cache = require( './cache.js' );


// VARIABLES //

var CACHE = cache();


// MAIN //

/**
* Returns a memoized ndarray constructor.
*
* @param {string} dtype - data type
* @param {PositiveInteger} ndims - number of dimensions
* @param {Options} [options] - function options
* @param {boolean} [options.codegen=true] - boolean indicating whether to use code generation
* @returns {Function} memoized ndarray constructor
*
* @example
* var f1 = ctor( 'float64', 3 );
* // returns <Function>
*
* var f2 = ctor( 'float64', 3 );
* // returns <Function>
*
* var bool = ( f1 === f2 );
* // returns true
*
* var buffer = [ 1, 2, 3, 4, 5, 6 ];
* var shape = [ 3, 2 ];
* var strides = [ 2, 1 ];
* var offset = 0;
*
* var out = f1( buffer, shape, strides, offset, 'row-major' );
*/
function ctor( dtype, ndims, options ) {
	var ctors;
	var opts;
	var key;
	var len;
	var f;
	var i;

	opts = {};
	if ( arguments.length > 2 ) {
		opts.codegen = ( options.codegen === void 0 ) ? defaults.codegen : options.codegen; // eslint-disable-line max-len
	} else {
		opts.codegen = defaults.codegen;
	}
	key = getKey( dtype, opts );
	ctors = CACHE[ key ];

	// If the constructor has not already been created, create it; otherwise, return a memoized constructor...
	len = ctors.length;
	if ( ndims <= len ) {
		f = ctors[ ndims-1 ];
		if ( f === false ) {
			f = baseCtor( dtype, ndims, opts );
			ctors[ ndims-1 ] = f;
		}
	} else {
		f = baseCtor( dtype, ndims, opts );
		for ( i = len+1; i < ndims; i++ ) {
			ctors.push( false );
		}
		ctors.push( f );
	}
	return f;
}


// EXPORTS //

module.exports = ctor;

Reorder expressions