Skip to content

Commit

Permalink
renamed simplifyCallback to optimizeCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
dvd101x committed Sep 8, 2024
1 parent 5ac2341 commit f466e29
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/function/matrix/filter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { simplifyCallback } from '../../utils/simplifyCallback.js'
import { optimizeCallback } from '../../utils/optimizeCallback.js'
import { filter, filterRegExp } from '../../utils/array.js'
import { factory } from '../../utils/factory.js'

Expand Down Expand Up @@ -58,9 +58,9 @@ export const createFilter = /* #__PURE__ */ factory(name, dependencies, ({ typed
* @private
*/
function _filterCallback (x, callback) {
const simpleCallback = simplifyCallback(callback, x, 'filter')
const fastCallback = optimizeCallback(callback, x, 'filter')
return filter(x, function (value, index, array) {
// invoke the callback function with the right number of arguments
return simpleCallback(value, [index], array)
return fastCallback(value, [index], array)
})
}
4 changes: 2 additions & 2 deletions src/function/matrix/forEach.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { simplifyCallback } from '../../utils/simplifyCallback.js'
import { optimizeCallback } from '../../utils/optimizeCallback.js'
import { factory } from '../../utils/factory.js'
import { recurse } from '../../utils/array.js'

Expand Down Expand Up @@ -45,5 +45,5 @@ export const createForEach = /* #__PURE__ */ factory(name, dependencies, ({ type
* @private
*/
function _forEach (array, callback) {
recurse(array, [], array, simplifyCallback(callback, array, name))
recurse(array, [], array, optimizeCallback(callback, array, name))
}
4 changes: 2 additions & 2 deletions src/function/matrix/map.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { simplifyCallback } from '../../utils/simplifyCallback.js'
import { optimizeCallback } from '../../utils/optimizeCallback.js'
import { arraySize, broadcastSizes, broadcastTo, get, recurse } from '../../utils/array.js'
import { factory } from '../../utils/factory.js'

Expand Down Expand Up @@ -151,6 +151,6 @@ export const createMap = /* #__PURE__ */ factory(name, dependencies, ({ typed })
* @private
*/
function _mapArray (array, callback) {
return recurse(array, [], array, simplifyCallback(callback, array, name))
return recurse(array, [], array, optimizeCallback(callback, array, name))
}
})
10 changes: 5 additions & 5 deletions src/type/matrix/DenseMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { isInteger } from '../../utils/number.js'
import { clone, deepStrictEqual } from '../../utils/object.js'
import { DimensionError } from '../../error/DimensionError.js'
import { factory } from '../../utils/factory.js'
import { simplifyCallback } from '../../utils/simplifyCallback.js'
import { optimizeCallback } from '../../utils/optimizeCallback.js'

const name = 'DenseMatrix'
const dependencies = [
Expand Down Expand Up @@ -537,11 +537,11 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
DenseMatrix.prototype.map = function (callback) {
// matrix instance
const me = this
const simpleCallback = simplifyCallback(callback, me._data, 'map')
const fastCallback = optimizeCallback(callback, me._data, 'map')

// determine the new datatype when the original matrix has datatype defined
// TODO: should be done in matrix constructor instead
const data = recurse(me._data, [], me, simpleCallback)
const data = recurse(me._data, [], me, fastCallback)
const datatype = me._datatype !== undefined
? getArrayDataType(data, typeOf)
: undefined
Expand All @@ -558,8 +558,8 @@ export const createDenseMatrixClass = /* #__PURE__ */ factory(name, dependencies
DenseMatrix.prototype.forEach = function (callback) {
// matrix instance
const me = this
const simpleCallback = simplifyCallback(callback, me._data, 'forEach')
recurse(this._data, [], me, simpleCallback)
const fastCallback = optimizeCallback(callback, me._data, 'forEach')
recurse(this._data, [], me, fastCallback)
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/type/matrix/SparseMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { clone, deepStrictEqual } from '../../utils/object.js'
import { arraySize, getArrayDataType, processSizesWildcard, unsqueeze, validateIndex } from '../../utils/array.js'
import { factory } from '../../utils/factory.js'
import { DimensionError } from '../../error/DimensionError.js'
import { simplifyCallback } from '../../utils/simplifyCallback.js'
import { optimizeCallback } from '../../utils/optimizeCallback.js'

const name = 'SparseMatrix'
const dependencies = [
Expand Down Expand Up @@ -853,11 +853,11 @@ export const createSparseMatrixClass = /* #__PURE__ */ factory(name, dependencie
// rows and columns
const rows = this._size[0]
const columns = this._size[1]
const simpleCallback = simplifyCallback(callback, me, 'map')
const fastCallback = optimizeCallback(callback, me, 'map')
// invoke callback
const invoke = function (v, i, j) {
// invoke callback
return simpleCallback(v, [i, j], me)
return fastCallback(v, [i, j], me)
}
// invoke _map
return _map(this, 0, rows - 1, 0, columns - 1, invoke, skipZeros)
Expand Down Expand Up @@ -962,7 +962,7 @@ export const createSparseMatrixClass = /* #__PURE__ */ factory(name, dependencie
// rows and columns
const rows = this._size[0]
const columns = this._size[1]
const simpleCallback = simplifyCallback(callback, me, 'forEach')
const fastCallback = optimizeCallback(callback, me, 'forEach')
// loop columns
for (let j = 0; j < columns; j++) {
// k0 <= k < k1 where k0 = _ptr[j] && k1 = _ptr[j+1]
Expand All @@ -976,7 +976,7 @@ export const createSparseMatrixClass = /* #__PURE__ */ factory(name, dependencie
const i = this._index[k]

// value @ k
simpleCallback(this._values[k], [i, j], me)
fastCallback(this._values[k], [i, j], me)
}
} else {
// create a cache holding all defined values
Expand All @@ -990,7 +990,7 @@ export const createSparseMatrixClass = /* #__PURE__ */ factory(name, dependencie
// and either read the value or zero
for (let i = 0; i < rows; i++) {
const value = (i in values) ? values[i] : 0
simpleCallback(value, [i, j], me)
fastCallback(value, [i, j], me)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import { typeOf as _typeOf } from './is.js'
* @param {string} name The name of the function that is using the callback.
* @returns {Function} Returns a simplified version of the callback function.
*/
export function simplifyCallback (callback, array, name) {
export function optimizeCallback (callback, array, name) {
if (typed.isTypedFunction(callback)) {
const firstIndex = (array.isMatrix ? array.size() : arraySize(array)).map(() => 0)
const firstValue = array.isMatrix ? array.get(firstIndex) : get(array, firstIndex)
const hasSingleSignature = Object.keys(callback.signatures).length === 1
const numberOfArguments = _findNumberOfArguments(callback, firstValue, firstIndex, array)
const simpleCallback = hasSingleSignature ? Object.values(callback.signatures)[0] : callback
const fastCallback = hasSingleSignature ? Object.values(callback.signatures)[0] : callback
if (numberOfArguments >= 1 && numberOfArguments <= 3) {
return (...args) => _tryFunctionWithArgs(simpleCallback, args.slice(0, numberOfArguments), name, callback.name)
return (...args) => _tryFunctionWithArgs(fastCallback, args.slice(0, numberOfArguments), name, callback.name)
}
return (...args) => _tryFunctionWithArgs(simpleCallback, args, name, callback.name)
return (...args) => _tryFunctionWithArgs(fastCallback, args, name, callback.name)
}
return callback
}
Expand Down

0 comments on commit f466e29

Please sign in to comment.