Flow.Launcher/Flow.Launcher.Infrastructure/TranslationMapping.cs
Jeremy Wu cd9825380d
Some checks failed
Publish Default Plugins / publish (push) Has been cancelled
Build / build (push) Has been cancelled
Release 2.1.0 | Plugin 5.2.0 (#4276)
2026-02-24 22:52:23 +11:00

34 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
namespace Flow.Launcher.Infrastructure
{
public class TranslationMapping
{
private bool _isConstructed;
// Assuming one original item maps to multi translated items
// list[i] is the last translated index + 1 of original index i
private readonly List<int> _originalToTranslated = new();
public void AddNewIndex(int translatedIndex, int length)
{
if (_isConstructed)
throw new InvalidOperationException("Mapping shouldn't be changed after construction");
_originalToTranslated.Add(translatedIndex + length);
}
public int MapToOriginalIndex(int translatedIndex)
{
var searchResult = _originalToTranslated.BinarySearch(translatedIndex);
return searchResult >= 0 ? searchResult + 1 : ~searchResult;
}
public void EndConstruct()
{
if (_isConstructed)
throw new InvalidOperationException("Mapping has already been constructed");
_isConstructed = true;
}
}
}