fast-unique-numbers
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -1,1 +0,1 @@ | ||
export declare const generateUniqueNumber: (set: Set<number>) => number; | ||
export declare const generateUniqueNumber: (collection: Map<number, any> | Set<number>) => number; |
@@ -7,4 +7,4 @@ const LAST_NUMBER_WEAK_MAP = new WeakMap(); | ||
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; | ||
export const generateUniqueNumber = (set) => { | ||
const lastNumber = LAST_NUMBER_WEAK_MAP.get(set); | ||
export const generateUniqueNumber = (collection) => { | ||
const lastNumber = LAST_NUMBER_WEAK_MAP.get(collection); | ||
/* | ||
@@ -18,25 +18,25 @@ * Let's try the cheapest algorithm first. It might fail to produce a new | ||
/* | ||
* If there are less than half of 2 ** 31 numbers stored in the set, the | ||
* chance to generate a new random number in the range from 0 to 2 ** 31 is | ||
* at least 50%. It's benifitial to use only SMIs because they perform much | ||
* better in any environment based on V8. | ||
* If there are less than half of 2 ** 31 numbers stored in the collection, | ||
* the chance to generate a new random number in the range from 0 to 2 ** 31 | ||
* is at least 50%. It's benifitial to use only SMIs because they perform | ||
* much better in any environment based on V8. | ||
*/ | ||
if (set.size < 1073741824) { | ||
while (set.has(nextNumber)) { | ||
if (collection.size < 1073741824) { | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * 2147483648); | ||
} | ||
LAST_NUMBER_WEAK_MAP.set(set, nextNumber); | ||
LAST_NUMBER_WEAK_MAP.set(collection, nextNumber); | ||
return nextNumber; | ||
} | ||
// Quickly check if there is a theoretical chance to generate a new number. | ||
if (set.size > MAX_SAFE_INTEGER) { | ||
throw new Error('Congratulations, you created a set of unique numbers which uses all available integers!'); | ||
if (collection.size > MAX_SAFE_INTEGER) { | ||
throw new Error('Congratulations, you created a collection of unique numbers which uses all available integers!'); | ||
} | ||
// Otherwise use the full scale of safely usable integers. | ||
while (set.has(nextNumber)) { | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * MAX_SAFE_INTEGER); | ||
} | ||
LAST_NUMBER_WEAK_MAP.set(set, nextNumber); | ||
LAST_NUMBER_WEAK_MAP.set(collection, nextNumber); | ||
return nextNumber; | ||
}; | ||
//# sourceMappingURL=/build/es2015/helpers/generate-unique-number.js.map |
@@ -13,4 +13,4 @@ (function (global, factory) { | ||
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; | ||
var generateUniqueNumber = function generateUniqueNumber(set) { | ||
var lastNumber = LAST_NUMBER_WEAK_MAP.get(set); | ||
var generateUniqueNumber = function generateUniqueNumber(collection) { | ||
var lastNumber = LAST_NUMBER_WEAK_MAP.get(collection); | ||
/* | ||
@@ -24,23 +24,23 @@ * Let's try the cheapest algorithm first. It might fail to produce a new | ||
/* | ||
* If there are less than half of 2 ** 31 numbers stored in the set, the | ||
* chance to generate a new random number in the range from 0 to 2 ** 31 is | ||
* at least 50%. It's benifitial to use only SMIs because they perform much | ||
* better in any environment based on V8. | ||
* If there are less than half of 2 ** 31 numbers stored in the collection, | ||
* the chance to generate a new random number in the range from 0 to 2 ** 31 | ||
* is at least 50%. It's benifitial to use only SMIs because they perform | ||
* much better in any environment based on V8. | ||
*/ | ||
if (set.size < 1073741824) { | ||
while (set.has(nextNumber)) { | ||
if (collection.size < 1073741824) { | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * 2147483648); | ||
} | ||
LAST_NUMBER_WEAK_MAP.set(set, nextNumber); | ||
LAST_NUMBER_WEAK_MAP.set(collection, nextNumber); | ||
return nextNumber; | ||
} | ||
// Quickly check if there is a theoretical chance to generate a new number. | ||
if (set.size > MAX_SAFE_INTEGER) { | ||
throw new Error('Congratulations, you created a set of unique numbers which uses all available integers!'); | ||
if (collection.size > MAX_SAFE_INTEGER) { | ||
throw new Error('Congratulations, you created a collection of unique numbers which uses all available integers!'); | ||
} | ||
// Otherwise use the full scale of safely usable integers. | ||
while (set.has(nextNumber)) { | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * MAX_SAFE_INTEGER); | ||
} | ||
LAST_NUMBER_WEAK_MAP.set(set, nextNumber); | ||
LAST_NUMBER_WEAK_MAP.set(collection, nextNumber); | ||
return nextNumber; | ||
@@ -47,0 +47,0 @@ }; |
@@ -7,4 +7,4 @@ var LAST_NUMBER_WEAK_MAP = new WeakMap(); | ||
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; | ||
export var generateUniqueNumber = function (set) { | ||
var lastNumber = LAST_NUMBER_WEAK_MAP.get(set); | ||
export var generateUniqueNumber = function (collection) { | ||
var lastNumber = LAST_NUMBER_WEAK_MAP.get(collection); | ||
/* | ||
@@ -18,25 +18,25 @@ * Let's try the cheapest algorithm first. It might fail to produce a new | ||
/* | ||
* If there are less than half of 2 ** 31 numbers stored in the set, the | ||
* chance to generate a new random number in the range from 0 to 2 ** 31 is | ||
* at least 50%. It's benifitial to use only SMIs because they perform much | ||
* better in any environment based on V8. | ||
* If there are less than half of 2 ** 31 numbers stored in the collection, | ||
* the chance to generate a new random number in the range from 0 to 2 ** 31 | ||
* is at least 50%. It's benifitial to use only SMIs because they perform | ||
* much better in any environment based on V8. | ||
*/ | ||
if (set.size < 1073741824) { | ||
while (set.has(nextNumber)) { | ||
if (collection.size < 1073741824) { | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * 2147483648); | ||
} | ||
LAST_NUMBER_WEAK_MAP.set(set, nextNumber); | ||
LAST_NUMBER_WEAK_MAP.set(collection, nextNumber); | ||
return nextNumber; | ||
} | ||
// Quickly check if there is a theoretical chance to generate a new number. | ||
if (set.size > MAX_SAFE_INTEGER) { | ||
throw new Error('Congratulations, you created a set of unique numbers which uses all available integers!'); | ||
if (collection.size > MAX_SAFE_INTEGER) { | ||
throw new Error('Congratulations, you created a collection of unique numbers which uses all available integers!'); | ||
} | ||
// Otherwise use the full scale of safely usable integers. | ||
while (set.has(nextNumber)) { | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * MAX_SAFE_INTEGER); | ||
} | ||
LAST_NUMBER_WEAK_MAP.set(set, nextNumber); | ||
LAST_NUMBER_WEAK_MAP.set(collection, nextNumber); | ||
return nextNumber; | ||
}; | ||
//# sourceMappingURL=/build/es2015/helpers/generate-unique-number.js.map |
@@ -12,4 +12,4 @@ 'use strict'; | ||
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; | ||
const generateUniqueNumber = exports.generateUniqueNumber = set => { | ||
const lastNumber = LAST_NUMBER_WEAK_MAP.get(set); | ||
const generateUniqueNumber = exports.generateUniqueNumber = collection => { | ||
const lastNumber = LAST_NUMBER_WEAK_MAP.get(collection); | ||
/* | ||
@@ -23,25 +23,25 @@ * Let's try the cheapest algorithm first. It might fail to produce a new | ||
/* | ||
* If there are less than half of 2 ** 31 numbers stored in the set, the | ||
* chance to generate a new random number in the range from 0 to 2 ** 31 is | ||
* at least 50%. It's benifitial to use only SMIs because they perform much | ||
* better in any environment based on V8. | ||
* If there are less than half of 2 ** 31 numbers stored in the collection, | ||
* the chance to generate a new random number in the range from 0 to 2 ** 31 | ||
* is at least 50%. It's benifitial to use only SMIs because they perform | ||
* much better in any environment based on V8. | ||
*/ | ||
if (set.size < 1073741824) { | ||
while (set.has(nextNumber)) { | ||
if (collection.size < 1073741824) { | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * 2147483648); | ||
} | ||
LAST_NUMBER_WEAK_MAP.set(set, nextNumber); | ||
LAST_NUMBER_WEAK_MAP.set(collection, nextNumber); | ||
return nextNumber; | ||
} | ||
// Quickly check if there is a theoretical chance to generate a new number. | ||
if (set.size > MAX_SAFE_INTEGER) { | ||
throw new Error('Congratulations, you created a set of unique numbers which uses all available integers!'); | ||
if (collection.size > MAX_SAFE_INTEGER) { | ||
throw new Error('Congratulations, you created a collection of unique numbers which uses all available integers!'); | ||
} | ||
// Otherwise use the full scale of safely usable integers. | ||
while (set.has(nextNumber)) { | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * MAX_SAFE_INTEGER); | ||
} | ||
LAST_NUMBER_WEAK_MAP.set(set, nextNumber); | ||
LAST_NUMBER_WEAK_MAP.set(collection, nextNumber); | ||
return nextNumber; | ||
}; | ||
//# sourceMappingURL=/build/es2015/helpers/generate-unique-number.js.map |
@@ -53,3 +53,3 @@ { | ||
"tslint": "^5.8.0", | ||
"tslint-config-holy-grail": "^18.0.2", | ||
"tslint-config-holy-grail": "^18.0.3", | ||
"typescript": "^2.6.2", | ||
@@ -83,3 +83,3 @@ "webpack": "^3.9.1" | ||
"types": "build/es2015/module.d.ts", | ||
"version": "1.0.0" | ||
"version": "1.1.0" | ||
} |
@@ -1,2 +0,2 @@ | ||
const LAST_NUMBER_WEAK_MAP = new WeakMap<Set<number>, number>(); | ||
const LAST_NUMBER_WEAK_MAP = new WeakMap<(Map<number, any> | Set<number>), number>(); | ||
/* | ||
@@ -8,4 +8,4 @@ * The value of the constant Number.MAX_SAFE_INTEGER equals (2 ** 53 - 1) but it | ||
export const generateUniqueNumber = (set: Set<number>): number => { | ||
const lastNumber = LAST_NUMBER_WEAK_MAP.get(set); | ||
export const generateUniqueNumber = (collection: (Map<number, any> | Set<number>)): number => { | ||
const lastNumber = LAST_NUMBER_WEAK_MAP.get(collection); | ||
@@ -21,13 +21,13 @@ /* | ||
/* | ||
* If there are less than half of 2 ** 31 numbers stored in the set, the | ||
* chance to generate a new random number in the range from 0 to 2 ** 31 is | ||
* at least 50%. It's benifitial to use only SMIs because they perform much | ||
* better in any environment based on V8. | ||
* If there are less than half of 2 ** 31 numbers stored in the collection, | ||
* the chance to generate a new random number in the range from 0 to 2 ** 31 | ||
* is at least 50%. It's benifitial to use only SMIs because they perform | ||
* much better in any environment based on V8. | ||
*/ | ||
if (set.size < 1073741824) { | ||
while (set.has(nextNumber)) { | ||
if (collection.size < 1073741824) { | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * 2147483648); | ||
} | ||
LAST_NUMBER_WEAK_MAP.set(set, nextNumber); | ||
LAST_NUMBER_WEAK_MAP.set(collection, nextNumber); | ||
@@ -38,14 +38,14 @@ return nextNumber; | ||
// Quickly check if there is a theoretical chance to generate a new number. | ||
if (set.size > MAX_SAFE_INTEGER) { | ||
throw new Error('Congratulations, you created a set of unique numbers which uses all available integers!'); | ||
if (collection.size > MAX_SAFE_INTEGER) { | ||
throw new Error('Congratulations, you created a collection of unique numbers which uses all available integers!'); | ||
} | ||
// Otherwise use the full scale of safely usable integers. | ||
while (set.has(nextNumber)) { | ||
while (collection.has(nextNumber)) { | ||
nextNumber = Math.floor(Math.random() * MAX_SAFE_INTEGER); | ||
} | ||
LAST_NUMBER_WEAK_MAP.set(set, nextNumber); | ||
LAST_NUMBER_WEAK_MAP.set(collection, nextNumber); | ||
return nextNumber; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19259