You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.6 KiB
62 lines
1.6 KiB
using System.Collections.Generic;
|
|
|
|
namespace Trash.Sonarr.ReleaseProfile
|
|
{
|
|
public class ScopedState<T>
|
|
{
|
|
private readonly T _defaultValue;
|
|
private readonly Stack<Node> _scopeStack = new();
|
|
|
|
public ScopedState(T defaultValue = default!)
|
|
{
|
|
_defaultValue = defaultValue;
|
|
}
|
|
|
|
public T Value => _scopeStack.Count > 0 ? _scopeStack.Peek().Value : _defaultValue;
|
|
|
|
public int? ActiveScope => _scopeStack.Count > 0 ? _scopeStack.Peek().Scope : null;
|
|
|
|
public int StackSize => _scopeStack.Count;
|
|
|
|
public void PushValue(T value, int scope)
|
|
{
|
|
if (_scopeStack.Count == 0 || _scopeStack.Peek().Scope < scope)
|
|
{
|
|
_scopeStack.Push(new Node(value, scope));
|
|
}
|
|
else if (_scopeStack.Peek().Scope == scope)
|
|
{
|
|
_scopeStack.Peek().Value = value;
|
|
}
|
|
}
|
|
|
|
public bool Reset(int scope)
|
|
{
|
|
if (_scopeStack.Count == 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var prevCount = StackSize;
|
|
while (_scopeStack.Count > 0 && _scopeStack.Peek().Scope >= scope)
|
|
{
|
|
_scopeStack.Pop();
|
|
}
|
|
|
|
return prevCount != StackSize;
|
|
}
|
|
|
|
private class Node
|
|
{
|
|
public Node(T value, int scope)
|
|
{
|
|
Value = value;
|
|
Scope = scope;
|
|
}
|
|
|
|
public T Value { get; set; }
|
|
public int Scope { get; }
|
|
}
|
|
}
|
|
}
|