array-kit 0.2.8 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/array-kit.js CHANGED
@@ -36,11 +36,12 @@ const arrayKit = {
36
36
  deleteValue: require( './deleteValue.js' )
37
37
  } ;
38
38
 
39
- module.exports = arrayKit ;
39
+ Object.assign( arrayKit , require( './sum.js' ) ) ;
40
40
 
41
41
  arrayKit.randomInteger = ( min , max ) => min + Math.floor( ( max - min + 1 ) * Math.random() ) ;
42
-
43
42
  arrayKit.random = arrayKit.randomElement = array => array[ Math.floor( array.length * Math.random() ) ] ;
44
43
  arrayKit.shuffle = array => arrayKit.sample( array , array.length , true ) ;
45
44
  arrayKit.randomSampleSize = ( array , min , max , inPlace ) => arrayKit.sample( array , arrayKit.randomInteger( min , max ) , inPlace ) ;
46
45
 
46
+ module.exports = arrayKit ;
47
+
package/lib/range.js CHANGED
@@ -41,38 +41,35 @@
41
41
  * end `number` the values end at this number (excluded)
42
42
  * step `number` (default: 1) the value of the increment
43
43
  */
44
- module.exports = function( start , end , step ) {
44
+ function range( start , end , step , inclusive = false ) {
45
+ if ( ! start === undefined ) { return [] ; }
46
+
45
47
  // Fast exit / common path first
46
- if ( arguments.length === 1 ) {
47
- const length = start ;
48
+ if ( end === undefined ) {
49
+ // .range( length ) syntax
50
+ let length = start ;
51
+ if ( inclusive ) { length ++ ; }
48
52
  const output = new Array( length ) ;
49
53
  for ( let i = 0 ; i < length ; i ++ ) { output[ i ] = i ; }
50
54
  return output ;
51
55
  }
52
56
 
53
- if ( ! arguments.length ) { return [] ; }
54
-
55
- if ( ! step ) {
56
- step = start <= end ? 1 : - 1 ;
57
- }
58
-
59
- const length = Math.ceil( ( end - start ) / step ) ;
60
-
61
- if ( length <= 0 ) { return [] ; }
57
+ if ( ! step ) { step = start <= end ? 1 : - 1 ; }
62
58
 
59
+ let length = Math.ceil( ( end - start ) / step ) ;
60
+ if ( inclusive && start + step * length === end ) { length ++ ; }
63
61
  const output = new Array( length ) ;
64
62
 
65
- if ( step > 0 ) {
66
- for ( let i = 0 , v = start ; v < end ; i ++ , v += step ) {
67
- output[ i ] = v ;
68
- }
69
- }
70
- else {
71
- for ( let i = 0 , v = start ; v > end ; i ++ , v += step ) {
72
- output[ i ] = v ;
73
- }
63
+ // This is better than incrementing the value with the step.
64
+ // Arguments start, end and step could be float, so it avoids (some) float precision loss.
65
+ for ( let i = 0 ; i < length ; i ++ ) {
66
+ output[ i ] = start + i * step ;
74
67
  }
75
68
 
76
69
  return output ;
77
- } ;
70
+ }
71
+
72
+ module.exports = range ;
73
+
74
+ range.inclusive = ( start , end , step ) => range( start , end , step , true ) ;
78
75
 
package/lib/sum.js ADDED
@@ -0,0 +1,109 @@
1
+ /*
2
+ Array Kit
3
+
4
+ Copyright (c) 2014 - 2020 Cédric Ronvel
5
+
6
+ The MIT License (MIT)
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in all
16
+ copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ SOFTWARE.
25
+ */
26
+
27
+ "use strict" ;
28
+
29
+
30
+
31
+ /*
32
+ The naive sum, works for arrays of limited size, or array of integers.
33
+ */
34
+ exports.sum = ( array , start = 0 , end = array.length ) => {
35
+ let sum = 0 ;
36
+ for ( let i = start ; i < end ; i ++ ) { sum += array[ i ] ; }
37
+ return sum ;
38
+ } ;
39
+
40
+
41
+
42
+ /*
43
+ This is the Neumaier algorithm, which compensate lost of precision of summing a lot of floats.
44
+ */
45
+ exports.floatSum = ( array , start = 0 , end = array.length ) => {
46
+ let sum = 0 ;
47
+ let errorSum = 0 ;
48
+
49
+ for ( let i = start ; i < end ; i ++ ) {
50
+ const value = array[ i ] ;
51
+ const nextSum = sum + value ;
52
+
53
+ // This is the tricky and sensible part, do not alter it, the order / priority of operation matters.
54
+ // It reverses the summation to get the error.
55
+ // Since errors are tiny, and we sum only tiny numbers here, we ensure good precision for the error sum.
56
+ if ( Math.abs( sum ) >= Math.abs( value ) ) {
57
+ errorSum += ( sum - nextSum ) + value ;
58
+ }
59
+ else {
60
+ errorSum += ( value - nextSum ) + sum ;
61
+ }
62
+
63
+ sum = nextSum ;
64
+ }
65
+
66
+ // The cumulated errors are added at the end
67
+ return sum + errorSum ;
68
+ } ;
69
+
70
+
71
+
72
+ /*
73
+ This compute the sum chunk by chunk, avoiding summing numbers that are too big with numbers that are too small.
74
+ The sum of sums itself can be computed recursively (but coded by iteration) if the sub-sums count is also greater
75
+ than the chunkSize.
76
+
77
+ It should be theorically better then floatSum() for very large arrays, but it's hard to test it.
78
+ */
79
+ exports.chunkedFloatSum = ( array , maxChunkSize = 1024 , start = 0 , end = array.length ) => {
80
+ let currentArray = array ;
81
+ let currentStart = start ;
82
+ let currentEnd = end ;
83
+
84
+ while ( currentEnd - currentStart >= maxChunkSize ) {
85
+ const chunkCount = Math.ceil( ( currentEnd - currentStart ) / maxChunkSize ) ;
86
+ const chunkSize = Math.ceil( ( currentEnd - currentStart ) / chunkCount ) ;
87
+ const sumArray = new Array( chunkCount ) ;
88
+
89
+ for ( let chunkIndex = 0 ; chunkIndex < chunkCount ; chunkIndex ++ ) {
90
+ sumArray[ chunkIndex ] = exports.floatSum(
91
+ currentArray ,
92
+ currentStart + chunkIndex * chunkSize ,
93
+ Math.min( currentStart + ( chunkIndex + 1 ) * chunkSize , currentEnd )
94
+ ) ;
95
+ }
96
+
97
+ currentArray = sumArray ;
98
+ currentStart = 0 ;
99
+ currentEnd = currentArray.length ;
100
+ }
101
+
102
+ return exports.floatSum( currentArray , currentStart , currentEnd ) ;
103
+ } ;
104
+
105
+
106
+
107
+ exports.mean = ( array , start = 0 , end = array.length ) => exports.sum( array , start , end ) / ( end - start ) ;
108
+ exports.floatMean = ( array , start = 0 , end = array.length ) => exports.floatSum( array , start , end ) / ( end - start ) ;
109
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "array-kit",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "description": "An array manipulation toolbox.",
5
5
  "main": "lib/array-kit.js",
6
6
  "directories": {