-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4678a37
commit 24f1060
Showing
13 changed files
with
118 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export function copy<T>(set: Set<T>): Set<T>; | ||
export function copy<T>(set: ReadonlySet<T>): ReadonlySet<T>; | ||
|
||
export function copy<T>(set: ReadonlySet<T>): ReadonlySet<T> { | ||
const resultSet = new Set<T>(); | ||
|
||
for (const element of set) { | ||
resultSet.add(element); | ||
} | ||
|
||
return resultSet; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export function filter<T>(set: Set<T>, filterFunction: (element: T) => boolean): Set<T>; | ||
export function filter<T>(set: ReadonlySet<T>, filterFunction: (element: T) => boolean): ReadonlySet<T>; | ||
|
||
export function filter<T>(set: ReadonlySet<T>, filterFunction: (element: T) => boolean): ReadonlySet<T> { | ||
const resultSet = new Set<T>(); | ||
|
||
for (const element of set) { | ||
if (filterFunction(element)) { | ||
resultSet.add(element); | ||
} | ||
} | ||
|
||
return resultSet; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export function map<T, R>(set: Set<T>, mappingFunction: (element: T) => R): Set<R>; | ||
export function map<T, R>(set: ReadonlySet<T>, mappingFunction: (element: T) => R): ReadonlySet<R>; | ||
|
||
export function map<T, R>(set: ReadonlySet<T>, mappingFunction: (element: T) => R): ReadonlySet<R> { | ||
const resultSet = new Set<R>(); | ||
|
||
for (const element of set) { | ||
resultSet.add(mappingFunction(element)); | ||
} | ||
|
||
return resultSet; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { describe, expect, it } from '@jest/globals'; | ||
import { ScaleTestSets } from '../util/scale/scale-test-sets.model'; | ||
import { Timer } from '../util/scale/timer.model'; | ||
|
||
describe('performance metrics', () => { | ||
|
||
describe('copying one set', () => { | ||
const { multiplesOf1 } = ScaleTestSets; | ||
|
||
time('one set ⋅ direct copy', () => new Set(multiplesOf1)); | ||
time('one set ⋅ array copy', () => new Set([ ...multiplesOf1 ])); | ||
time('one set ⋅ adding elements', () => { | ||
const set = new Set<number>(); | ||
for (const element of multiplesOf1) set.add(element); | ||
return set; | ||
}); | ||
}); | ||
|
||
describe('copying two sets', () => { | ||
const { multiplesOf2, multiplesOf2B } = ScaleTestSets; | ||
|
||
time('two sets ⋅ array copy', () => new Set([ ...multiplesOf2, ...multiplesOf2B ])); | ||
time('two sets ⋅ adding elements', () => { | ||
const set = new Set<number>(); | ||
for (const element of multiplesOf2) set.add(element); | ||
for (const element of multiplesOf2B) set.add(element); | ||
return set; | ||
}); | ||
}); | ||
|
||
describe('copying three sets', () => { | ||
const { multiplesOf3, multiplesOf3B, multiplesOf3C } = ScaleTestSets; | ||
|
||
time('three sets ⋅ array copy', () => new Set([ ...multiplesOf3, ...multiplesOf3B, ...multiplesOf3C ])); | ||
time('three sets ⋅ adding elements', () => { | ||
const set = new Set<number>(); | ||
for (const element of multiplesOf3) set.add(element); | ||
for (const element of multiplesOf3B) set.add(element); | ||
for (const element of multiplesOf3C) set.add(element); | ||
return set; | ||
}); | ||
}); | ||
}); | ||
|
||
function time(methodName: string, method: () => Set<number>): void { | ||
it(methodName, () => { | ||
for (let index = 0; index < 50; index++) { | ||
if (index > 0 && index % 10 === 0) Timer.nextLine(methodName); | ||
const result = Timer.time(methodName, method); | ||
expect(result.size).toBe(15_000_000); | ||
} | ||
|
||
Timer.log(methodName); | ||
}); | ||
} |