2024-06-02 06:18:57 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Flow.Launcher.Infrastructure
|
|
|
|
|
|
{
|
|
|
|
|
|
public class TranslationMapping
|
|
|
|
|
|
{
|
2025-07-13 15:29:41 +00:00
|
|
|
|
private bool _isConstructed;
|
2024-06-02 06:18:57 +00:00
|
|
|
|
|
2025-07-13 15:39:44 +00:00
|
|
|
|
// Assuming one original item maps to multi translated items
|
|
|
|
|
|
// list[i] is the last translated index + 1 of original index i
|
2025-07-13 15:29:41 +00:00
|
|
|
|
private readonly List<int> _originalToTranslated = new();
|
2025-04-09 12:06:17 +00:00
|
|
|
|
|
2025-07-13 07:56:25 +00:00
|
|
|
|
public void AddNewIndex(int translatedIndex, int length)
|
2024-06-02 06:18:57 +00:00
|
|
|
|
{
|
2025-07-13 15:29:41 +00:00
|
|
|
|
if (_isConstructed)
|
|
|
|
|
|
throw new InvalidOperationException("Mapping shouldn't be changed after construction");
|
|
|
|
|
|
_originalToTranslated.Add(translatedIndex + length);
|
2024-06-02 06:18:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int MapToOriginalIndex(int translatedIndex)
|
|
|
|
|
|
{
|
2025-07-13 15:29:41 +00:00
|
|
|
|
var searchResult = _originalToTranslated.BinarySearch(translatedIndex);
|
2025-12-07 10:16:09 +00:00
|
|
|
|
return searchResult >= 0 ? searchResult + 1 : ~searchResult;
|
2024-06-02 06:18:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-13 15:29:41 +00:00
|
|
|
|
public void EndConstruct()
|
2024-06-02 06:18:57 +00:00
|
|
|
|
{
|
2025-07-13 15:29:41 +00:00
|
|
|
|
if (_isConstructed)
|
2024-06-02 06:18:57 +00:00
|
|
|
|
throw new InvalidOperationException("Mapping has already been constructed");
|
2025-07-13 15:29:41 +00:00
|
|
|
|
_isConstructed = true;
|
2024-06-02 06:18:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|