Gcoder.Collections 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Gcoder.Collections --version 1.0.2
                    
NuGet\Install-Package Gcoder.Collections -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="Gcoder.Collections" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Gcoder.Collections" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Gcoder.Collections" />
                    
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 Gcoder.Collections --version 1.0.2
                    
#r "nuget: Gcoder.Collections, 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.
#:package Gcoder.Collections@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Gcoder.Collections&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Gcoder.Collections&version=1.0.2
                    
Install as a Cake Tool

Gcoder.Collections

Thư viện collection hiệu năng cao cho .NET 9, cung cấp hai cấu trúc dữ liệu chuyên biệt: Sorted Dictionary (giống Redis ZSET) và Timed Collection (time wheel-based expiration).

Tính năng

1. ISortedDictionary<TKey, TValue>

Sorted dictionary sử dụng Skip List + Hash Map bên trong, mô phỏng Redis Sorted Set (ZSET). Hỗ trợ đầy đủ thao tác rank, range by score, range by rank.

Thao tác Độ phức tạp
Add / Remove O(log n)
GetScore / TryGetScore / ContainsKey O(1)
GetRank / GetReverseRank O(log n)
Min / Max O(log n)
RangeByScore / RangeByRank O(log n + k)
CountByScore O(log n)
RemoveRangeByScore / RemoveRangeByRank O(log n + k)
Count / Keys O(1)

Ví dụ:

using Gcoder.Collections;

var dict = ISortedDictionary<string, int>.Create();

dict.Add("player1", 1500);
dict.Add("player2", 2000);
dict.Add("player3", 1200);

// Lấy top 2 người chơi điểm cao nhất
var top2 = dict.RangeByRank(0, 1); // (player3,1200), (player1,1500)

// Đếm số người chơi trong khoảng điểm [1200, 1800]
int count = dict.CountByScore(1200, 1800); // 2

// Xoá người chơi có điểm < 1500
int removed = dict.RemoveRangeByScore(int.MinValue, 1499); // 1

2. ITimedCollection<TKey, TValue>

Time wheel-based collection với cơ chế tự động hết hạn (expiration). Sử dụng Hierarchical Time Wheel 3 tầng, hỗ trợ hàng triệu object với O(1) per operation.

Tham số Giá trị
Tick 10ms
Wheel 0 (ms) 256 slots x 10ms = 2.56s
Wheel 1 (sec) 64 slots x 2.56s = ~2.73 phút
Wheel 2 (min) 64 slots x 2.73m = ~2.9 giờ
Max expiration Có thể kéo dài hơn 2.9 giờ
Thao tác Độ phức tạp
AddOrUpdate O(1)
Remove O(1)
Tick O(1) per expired object
Expire callback O(1) mỗi object

2 phiên bản:

a) Phiên bản Thread-Safe (NewTimeSortSet)
  • Sử dụng ReaderWriterLockSlim + background timer thread 10ms
  • An toàn cho đa luồng
  • OnExpired / OnRemoved fire từ background thread
  • Tick() không có tác dụng (no-op) — timer đã tự chạy
using Gcoder.Collections;

using var collection = ITimedCollection<string, string>.NewTimeSortSet();

// Callback khi object hết hạn
collection.OnExpired += items =>
{
    foreach (var (key, value) in items)
        Console.WriteLine($"Expired: {key}");
};

// Thêm object hết hạn sau 5 giây
collection.AddOrUpdate("session:123", "data", TimeSpan.FromSeconds(5));

// Hoặc dùng absolute time
collection.AddOrUpdate("session:456", "data",
    DateTimeOffset.UtcNow.AddSeconds(10));

// Xoá chủ động (kích hoạt OnRemoved)
collection.Remove("session:123");
b) Phiên bản Single-Thread (NewTimeSortSetSingleThread)
  • Không ReaderWriterLockSlim, không background timer → zero lock overhead
  • Expiration lười (lazy): kiểm tra trên mỗi AddOrUpdate / Remove / Count / Tick
  • OnExpired / OnRemoved fire đồng bộ trên thread gọi
  • Chỉ dùng khi toàn bộ truy cập từ 1 thread
  • Cần gọi Tick() định kỳ (vd: mỗi frame trong game loop) nếu collection có thể idle lâu
using Gcoder.Collections;

using var collection = ITimedCollection<string, string>.NewTimeSortSetSingleThread();

collection.OnExpired += items =>
{
    foreach (var (key, value) in items)
        Console.WriteLine($"Expired: {key}");
};

collection.AddOrUpdate("session:123", "data", TimeSpan.FromSeconds(5));

// Trong game loop / update loop
while (running)
{
    collection.Tick(); // ← đẩy wheel, dọn expire mà không cần add/remove
    // ... logic game
}

So sánh 2 phiên bản:

Thread-Safe Single-Thread
Lock ReaderWriterLockSlim Không
Timer Background thread 10ms Lazy (trên thread gọi)
Tick() No-op Tích cực đẩy wheel
OnExpired context Background thread Thread gọi
Hiệu suất đơn luồng Trung bình Tối ưu
Đa luồng

Cài đặt

dotnet add package Gcoder.Collections --version 1.0.2

Hoặc tham khảo source trực tiếp từ GitHub.

Yêu cầu

  • .NET 9+

Thread Safety

  • ISortedDictionary (DictionaryScore): Không thread-safe. Cần lock bên ngoài nếu truy cập đa luồng.
  • ITimedCollection (NewTimeSortSet): thread-safe. Dùng ReaderWriterLockSlim + background timer.
  • ITimedCollection (NewTimeSortSetSingleThread): Không thread-safe. Tối ưu đơn luồng, lazy expiration.

License

MIT

Product Compatible and additional computed target framework versions.
.NET 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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Gcoder.Collections:

Package Downloads
PubSubLib

Package Description

Natify

Micro-batching library for NATS messaging with deduplication, TTL-based dedup wheel, reliable ACK/Retry, and zero-copy serialization.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.3.0 150 7/14/2026
1.2.0 94 7/10/2026
1.1.1 101 7/10/2026
1.0.2 96 7/10/2026
1.0.1 88 7/10/2026
1.0.0 92 7/10/2026