Created
October 11, 2019 23:27
-
-
Save kayabaNerve/73d372abb7f9f5e788768b9a18c6738f to your computer and use it in GitHub Desktop.
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
#Constructor 1. | |
proc newSketcher*[T](): Sketcher[T] {.inline, forceCheck: [].} = | |
@[] | |
#Constructor 2. | |
proc newSketcher*[T]( | |
elements: seq[T] = @[] | |
): Sketcher[T] {.forceCheck: [].} = | |
result = @[] | |
for element in elements: | |
result.add(SketchElement[T]( | |
element: element, | |
significance: 0 | |
)) | |
#Add an element. | |
proc add*[T]( | |
sketcher: Sketcher[T], | |
elem: T, | |
significance: int | |
) {.forceCheck: [].} = | |
sketcher.add(SketchElement( | |
element: elem, | |
significance: significance | |
)) | |
#Convert a Sketcher to a Sketch. | |
proc toSketch[T]( | |
sketcher: Sketcher[T], | |
capacity: int, | |
significant: int, | |
salt: string | |
): tuple[ | |
sketch: Sketch, | |
hashes: Table[uint64, int] | |
] {.forceCheck: [ | |
ValueError | |
].} = | |
#Create the sketch. | |
result.sketch = newSketch(64, 0, capacity) | |
result.hashes = initTable[uint64, int]() | |
var hash: uint64 | |
for e in 0 ..< sketcher.len: | |
#If it's significant, use it. | |
if sketcher[e].significance >= significant: | |
#Hash the element. | |
hash = sketcher[e].element.sketchHash(salt) | |
#If there's a collision, throw. | |
if result.hashes.hasKey(hash): | |
raise newException(ValueError, "Collision found while sketching values.") | |
result.sketch.add(hash) | |
result.hashes[hash] = e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment