feat(ui-visuals): add layout, scale, decimation, viewport and curve math kernels
WPF-free geometry behind the visual controls: wrapped-grid layout with O(1) index/offset mapping and keyboard targets, nice axis ticks, min/max decimation, zoom/pan transforms, monotone curve editing rules, filmstrip and heatmap cell layout, an LRU cache and device-pixel snapping, with tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
16 files changed
+1410
No files matched your search
@@ -0,0 +1,81 @@
|
||||
namespace Titano.Ui.Visuals;
|
||||
|
||||
/// <summary>Fixed-capacity map that evicts the least recently used entry (decoded thumbnails).</summary>
|
||||
public sealed class LruCache<TKey, TValue>
|
||||
where TKey : notnull
|
||||
{
|
||||
private readonly Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>> _map;
|
||||
private readonly LinkedList<KeyValuePair<TKey, TValue>> _order = new();
|
||||
private int _capacity;
|
||||
|
||||
public LruCache(int capacity)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(capacity, 1);
|
||||
_capacity = capacity;
|
||||
_map = new Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>>(capacity);
|
||||
}
|
||||
|
||||
public int Count => _map.Count;
|
||||
|
||||
/// <summary>Maximum entries; lowering it evicts the oldest entries at once.</summary>
|
||||
public int Capacity
|
||||
{
|
||||
get => _capacity;
|
||||
set
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(value, 1);
|
||||
_capacity = value;
|
||||
Trim();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Looks up <paramref name="key"/> and marks it as the most recently used.</summary>
|
||||
public bool TryGet(TKey key, out TValue value)
|
||||
{
|
||||
if (_map.TryGetValue(key, out var node))
|
||||
{
|
||||
_order.Remove(node);
|
||||
_order.AddFirst(node);
|
||||
value = node.Value.Value;
|
||||
return true;
|
||||
}
|
||||
value = default!;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>Inserts or replaces <paramref name="key"/> as the most recently used entry.</summary>
|
||||
public void Set(TKey key, TValue value)
|
||||
{
|
||||
if (_map.TryGetValue(key, out var node))
|
||||
{
|
||||
_order.Remove(node);
|
||||
node.Value = new KeyValuePair<TKey, TValue>(key, value);
|
||||
_order.AddFirst(node);
|
||||
return;
|
||||
}
|
||||
_map[key] = _order.AddFirst(new KeyValuePair<TKey, TValue>(key, value));
|
||||
Trim();
|
||||
}
|
||||
|
||||
public bool Remove(TKey key)
|
||||
{
|
||||
if (!_map.Remove(key, out var node)) return false;
|
||||
_order.Remove(node);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_map.Clear();
|
||||
_order.Clear();
|
||||
}
|
||||
|
||||
private void Trim()
|
||||
{
|
||||
while (_map.Count > _capacity && _order.Last is { } oldest)
|
||||
{
|
||||
_order.RemoveLast();
|
||||
_map.Remove(oldest.Value.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user