2024-06-02 06:18:57 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Flow.Launcher.Infrastructure
|
|
|
|
|
|
{
|
|
|
|
|
|
public class TranslationMapping
|
|
|
|
|
|
{
|
|
|
|
|
|
private bool constructed;
|
|
|
|
|
|
|
2025-07-13 11:35:11 +00:00
|
|
|
|
// Assuming one original item maps to multi translated items
|
2025-07-13 08:08:28 +00:00
|
|
|
|
// list[i] is the last translated index + 1 of original index i
|
2025-07-13 12:58:15 +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
|
|
|
|
{
|
|
|
|
|
|
if (constructed)
|
|
|
|
|
|
throw new InvalidOperationException("Mapping shouldn't be changed after constructed");
|
|
|
|
|
|
|
2025-07-13 07:56:25 +00:00
|
|
|
|
originalToTranslated.Add(translatedIndex + length);
|
2024-06-02 06:18:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int MapToOriginalIndex(int translatedIndex)
|
|
|
|
|
|
{
|
2025-07-13 07:56:25 +00:00
|
|
|
|
int loc = originalToTranslated.BinarySearch(translatedIndex);
|
2025-07-13 11:33:20 +00:00
|
|
|
|
return loc >= 0 ? loc : ~loc;
|
2024-06-02 06:18:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void endConstruct()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (constructed)
|
|
|
|
|
|
throw new InvalidOperationException("Mapping has already been constructed");
|
|
|
|
|
|
constructed = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|