-
Notifications
You must be signed in to change notification settings - Fork 0
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
a94da2f
commit aad7a7b
Showing
31 changed files
with
983 additions
and
312 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Bloomn | ||
{ | ||
public static class BloomFilter | ||
{ | ||
public static IBloomFilterBuilder<TKey> Builder<TKey>() => new BloomFilterBuilder<TKey>(new BloomFilterOptions<TKey>()); | ||
} | ||
} |
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,97 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Bloomn | ||
{ | ||
public class BloomFilterDimensionsBuilder | ||
{ | ||
public double? FalsePositiveProbability { get; set; } | ||
public int? Capacity { get; set; } | ||
public int? BitCount { get; set; } | ||
public int? HashCount { get; set; } | ||
|
||
[MemberNotNullWhen(true, nameof(FalsePositiveProbability))] | ||
[MemberNotNullWhen(true, nameof(Capacity))] | ||
[MemberNotNullWhen(true, nameof(BitCount))] | ||
[MemberNotNullWhen(true, nameof(HashCount))] | ||
public bool FullySpecified => FalsePositiveProbability != null && Capacity != null && BitCount != null && HashCount != null; | ||
|
||
public bool Buildable => | ||
Capacity.HasValue && FalsePositiveProbability.HasValue | ||
|| FalsePositiveProbability.HasValue && BitCount.HasValue | ||
|| Capacity.HasValue && FalsePositiveProbability.HasValue | ||
|| Capacity.HasValue && BitCount.HasValue | ||
|| FalsePositiveProbability.HasValue && BitCount.HasValue; | ||
|
||
public BloomFilterDimensions Build() | ||
{ | ||
// Create a copy to mutate during building | ||
return new BloomFilterDimensionsBuilder() | ||
{ | ||
FalsePositiveProbability = this.FalsePositiveProbability, | ||
Capacity = this.Capacity, | ||
BitCount = this.BitCount, | ||
HashCount = this.HashCount | ||
}.ReallyBuild(); | ||
} | ||
|
||
private BloomFilterDimensions ReallyBuild() | ||
{ | ||
if (!Buildable) | ||
{ | ||
throw new InvalidOperationException("Not enough parameters are set."); | ||
} | ||
|
||
var makingProgress = true; | ||
while (!FullySpecified && makingProgress) | ||
{ | ||
makingProgress = false; | ||
if (!HashCount.HasValue && Capacity.HasValue && BitCount.HasValue) | ||
{ | ||
HashCount = BloomFilterDimensions.Equations.k(BitCount.Value, Capacity.Value); | ||
makingProgress = true; | ||
continue; | ||
} | ||
|
||
if (!HashCount.HasValue && FalsePositiveProbability.HasValue) | ||
{ | ||
HashCount = BloomFilterDimensions.Equations.k(FalsePositiveProbability.Value); | ||
makingProgress = true; | ||
continue; | ||
} | ||
|
||
if (!BitCount.HasValue && Capacity.HasValue && FalsePositiveProbability.HasValue) | ||
{ | ||
BitCount = BloomFilterDimensions.Equations.m(Capacity.Value, FalsePositiveProbability.Value); | ||
makingProgress = true; | ||
continue; | ||
} | ||
|
||
if (!Capacity.HasValue && BitCount.HasValue && HashCount.HasValue && FalsePositiveProbability.HasValue) | ||
{ | ||
Capacity = BloomFilterDimensions.Equations.n(BitCount.Value, HashCount.Value, FalsePositiveProbability.Value); | ||
makingProgress = true; | ||
continue; | ||
} | ||
|
||
if (!FalsePositiveProbability.HasValue && BitCount.HasValue && Capacity.HasValue && HashCount.HasValue) | ||
{ | ||
FalsePositiveProbability = BloomFilterDimensions.Equations.p(BitCount.Value, Capacity.Value, HashCount.Value); | ||
makingProgress = true; | ||
} | ||
} | ||
|
||
if (!FullySpecified) | ||
{ | ||
throw new InvalidOperationException($"Could not compute dimensions using provided values: {this}"); | ||
} | ||
|
||
return new BloomFilterDimensions(FalsePositiveProbability.Value, Capacity.Value, BitCount.Value, HashCount.Value); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return $"{nameof(FalsePositiveProbability)}: {FalsePositiveProbability}, {nameof(Capacity)}: {Capacity}, {nameof(BitCount)}: {BitCount}, {nameof(HashCount)}: {HashCount}"; | ||
} | ||
} | ||
} |
Oops, something went wrong.