|
1 | 1 | using System.Numerics; |
| 2 | +using Chaos.Common.Abstractions; |
| 3 | +using Chaos.Extensions.Common; |
2 | 4 |
|
3 | 5 | namespace Darkages.Common; |
4 | 6 |
|
5 | | -/// <summary> |
6 | | -/// Generates short-lived random numeric IDs and keeps a small history |
7 | | -/// to greatly reduce the chance of immediate repeats. |
8 | | -/// |
9 | | -/// Supports ONLY integral types: |
10 | | -/// sbyte, byte, short, ushort, int, uint, long, ulong. |
11 | | -/// </summary> |
12 | | -/// <typeparam name="T">The numeric type to generate.</typeparam> |
13 | | -public sealed class EphemeralRandomIdGenerator<T> where T : INumber<T> |
| 7 | +public sealed class EphemeralRandomIdGenerator<T> : IIdGenerator<T> where T : INumber<T> |
14 | 8 | { |
15 | | - private const int HistorySize = ushort.MaxValue; |
16 | | - private readonly T[] _history = new T[HistorySize]; |
17 | | - private int _index; |
18 | | - private static readonly Func<T> _nextRandom = CreateGenerator(); |
19 | | - private static readonly EqualityComparer<T> _cmp = EqualityComparer<T>.Default; |
| 9 | + private const int HISTORY_SIZE = byte.MaxValue; |
| 10 | + private readonly T[] IdHistory = new T[HISTORY_SIZE]; |
| 11 | + private int Index; |
20 | 12 |
|
21 | | - /// <summary> |
22 | | - /// Shared singleton instance |
23 | | - /// </summary> |
24 | | - public static EphemeralRandomIdGenerator<T> Shared { get; } = new(); |
25 | | - |
26 | | - /// <summary> |
27 | | - /// Returns the next random ID |
28 | | - /// </summary> |
| 13 | + /// <inheritdoc /> |
29 | 14 | public T NextId |
30 | 15 | { |
31 | 16 | get |
32 | 17 | { |
| 18 | + var index = Interlocked.Increment(ref Index) % HISTORY_SIZE; |
33 | 19 | T id; |
34 | 20 |
|
35 | | - // Avoid immediate repeats within the last HistorySize values. |
36 | 21 | do |
37 | 22 | { |
38 | | - id = _nextRandom(); |
39 | | - } while (IsDuplicate(id)); |
40 | | - |
41 | | - var slot = Interlocked.Increment(ref _index) % HistorySize; |
42 | | - _history[slot] = id; |
| 23 | + id = Random.Shared.Next<T>(); |
| 24 | + IdHistory[index] = id; |
| 25 | + } while (IdHistory.Count(num => num == id) > 1); |
43 | 26 |
|
44 | 27 | return id; |
45 | 28 | } |
46 | 29 | } |
47 | 30 |
|
48 | | - private bool IsDuplicate(T value) |
49 | | - { |
50 | | - for (var i = 0; i < HistorySize; i++) |
51 | | - { |
52 | | - if (_cmp.Equals(_history[i], value)) |
53 | | - return true; |
54 | | - } |
55 | | - |
56 | | - return false; |
57 | | - } |
58 | | - |
59 | | - /// <summary> |
60 | | - /// Build a per-type random generator once, then reuse. |
61 | | - /// </summary> |
62 | | - private static Func<T> CreateGenerator() |
63 | | - { |
64 | | - var t = typeof(T); |
65 | | - |
66 | | - return t switch |
67 | | - { |
68 | | - // signed |
69 | | - var _ when t == typeof(sbyte) => () => |
70 | | - { |
71 | | - var value = (sbyte)Random.Shared.Next(sbyte.MinValue, sbyte.MaxValue + 1); |
72 | | - return (T)(object)value; |
73 | | - } |
74 | | - , |
75 | | - |
76 | | - var _ when t == typeof(short) => () => |
77 | | - { |
78 | | - var value = (short)Random.Shared.Next(short.MinValue, short.MaxValue + 1); |
79 | | - return (T)(object)value; |
80 | | - } |
81 | | - , |
82 | | - |
83 | | - var _ when t == typeof(int) => () => |
84 | | - { |
85 | | - var value = Random.Shared.Next(int.MinValue, int.MaxValue); // max is exclusive |
86 | | - return (T)(object)value; |
87 | | - } |
88 | | - , |
89 | | - |
90 | | - var _ when t == typeof(long) => () => |
91 | | - { |
92 | | - var value = Random.Shared.NextInt64(long.MinValue, long.MaxValue); |
93 | | - return (T)(object)value; |
94 | | - } |
95 | | - , |
96 | | - |
97 | | - // unsigned |
98 | | - var _ when t == typeof(byte) => () => |
99 | | - { |
100 | | - var value = (byte)Random.Shared.Next(0, byte.MaxValue + 1); |
101 | | - return (T)(object)value; |
102 | | - } |
103 | | - , |
104 | | - |
105 | | - var _ when t == typeof(ushort) => () => |
106 | | - { |
107 | | - var value = (ushort)Random.Shared.Next(0, ushort.MaxValue + 1); |
108 | | - return (T)(object)value; |
109 | | - } |
110 | | - , |
111 | | - |
112 | | - var _ when t == typeof(uint) => () => |
113 | | - { |
114 | | - var value = (uint)Random.Shared.NextInt64(0, (long)uint.MaxValue + 1); |
115 | | - return (T)(object)value; |
116 | | - } |
117 | | - , |
118 | | - |
119 | | - var _ when t == typeof(ulong) => () => |
120 | | - { |
121 | | - // Build a full 64-bit value from two 32-bit chunks. |
122 | | - var hi = (ulong)(uint)Random.Shared.NextInt64(0, (long)uint.MaxValue + 1); |
123 | | - var lo = (ulong)(uint)Random.Shared.NextInt64(0, (long)uint.MaxValue + 1); |
124 | | - var value = (hi << 32) | lo; |
125 | | - return (T)(object)value; |
126 | | - } |
127 | | - , |
128 | | - |
129 | | - _ => throw new NotSupportedException( |
130 | | - $"EphemeralRandomIdGenerator<{t.Name}> supports only integral types " + |
131 | | - "(sbyte, byte, short, ushort, int, uint, long, ulong).") |
132 | | - }; |
133 | | - } |
| 31 | + /// <inheritdoc /> |
| 32 | + public static IIdGenerator<T> Shared { get; } = new EphemeralRandomIdGenerator<T>(); |
134 | 33 | } |
0 commit comments