KrapivinHashTable 1.0.2

dotnet add package KrapivinHashTable --version 1.0.2
                    
NuGet\Install-Package KrapivinHashTable -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="KrapivinHashTable" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="KrapivinHashTable" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="KrapivinHashTable" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add KrapivinHashTable --version 1.0.2
                    
#r "nuget: KrapivinHashTable, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=KrapivinHashTable&version=1.0.2
                    
Install KrapivinHashTable as a Cake Addin
#tool nuget:?package=KrapivinHashTable&version=1.0.2
                    
Install KrapivinHashTable as a Cake Tool

KrapivinHashTable

A high-performance, segmented hash table implementation for .NET with quadratic probing and efficient collision resolution.

Features

  • Generic implementation supporting any non-null key and value types
  • Segmented design for improved cache locality
  • Efficient quadratic probing for collision resolution
  • MurmurHash3 for high-quality hash distribution
  • Logical deletion to maintain probe sequences
  • Customizable capacity and segment sizes
  • Support for custom equality comparers

Installation

NuGet Package Manager

Install-Package KrapivinHashTable

.NET CLI

dotnet add package KrapivinHashTable

Quick Start

// Create a new hash table with default settings
var hashTable = new KrapivinHashTable<string, int>();

// Insert key-value pairs
hashTable.Insert("one", 1);
hashTable.Insert("two", 2);
hashTable.Insert("three", 3);

// Retrieve values
int value;
if (hashTable.TryGet("two", out value))
{
    Console.WriteLine($"Found: {value}");  // Outputs: Found: 2
}

// Alternative retrieval method
int anotherValue = hashTable.Get("three");  // Returns 3
int notFound = hashTable.Get("four");       // Returns default(int) which is 0

// Delete a key-value pair
bool deleted = hashTable.Delete("one");     // Returns true

Configuration Options

The KrapivinHashTable constructor accepts several parameters to customize the table's behavior:

public KrapivinHashTable(
    int initialCapacity = 1024,             // Total number of slots
    int segmentSize = 32,                   // Size of each segment
    IEqualityComparer<TKey>? comparer = null // Custom equality comparer
)
  • initialCapacity: The total number of slots in the hash table. Default is 1024.
  • segmentSize: The size of each segment. This affects the search locality. Default is 32.
  • comparer: A custom equality comparer for the key type. If null, the default comparer for the key type is used.

Technical Details

Segmented Design

KrapivinHashTable uses a segmented approach where the hash table is divided into segments of fixed size. When searching for a key, the hash function first identifies the appropriate segment, and then the search is initially confined to that segment. This improves cache locality and performance.

Collision Resolution

Collisions are resolved using quadratic probing with the formula (n² + n)/2, which provides a good distribution and helps avoid clustering. When a segment becomes full, the search extends beyond the segment using the same quadratic probing sequence.

Hash Function

The library uses MurmurHash3, a high-quality non-cryptographic hash function that provides excellent distribution with minimal collisions.

Hash distribution for non-string types

  • .NET's GetHashCode() implementations are already designed to provide good distribution for hash tables
  • For most practical use cases, this distribution is sufficient
  • The additional MurmurHash3 pass might provide marginally better distribution in some edge cases, but at a significant performance cost
  • When dealing with large datasets, the performance gain from avoiding allocations likely outweighs any theoretical distribution improvements

Performance Considerations

  • The table automatically prevents insertions when the load factor exceeds 90% to maintain good performance.
  • Logical deletion is used to maintain probe sequences, which means deleted slots can be reused for new insertions.
  • The library is optimized for lookup operations, making it suitable for caching scenarios.

Use Cases

  • In-memory caching
  • Fast lookup tables
  • Symbol tables in compilers and interpreters
  • Counting unique elements
  • Implementing sets and dictionaries

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 378 8 days ago
1.0.0 183 a month ago

Initial release