60 lines
1.4 KiB
C#
60 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Stackoverflow.Utilities;
|
|
|
|
namespace SearchBox
|
|
{
|
|
public class IdNotFoundException : Exception { public IdNotFoundException(string message) : base(message) { } }
|
|
|
|
public class IdMapper
|
|
{
|
|
private int nextId = 0;
|
|
public BiDictionary<int, string> map = new BiDictionary<int, string>();
|
|
|
|
public IdMapper()
|
|
{
|
|
}
|
|
|
|
public int GetId(string pageName)
|
|
{
|
|
// Perform unicode normalization
|
|
pageName = pageName.Normalize(System.Text.NormalizationForm.FormC);
|
|
|
|
int result;
|
|
if (!map.TryGetBySecond(pageName, out result)) {
|
|
map.Add(result = nextId++, pageName);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public string GetPageName(int id)
|
|
{
|
|
string result;
|
|
if (!map.TryGetByFirst(id, out result))
|
|
throw new IdNotFoundException($"Error: Couldn't find {id} in the ID map.");
|
|
return result;
|
|
}
|
|
|
|
public void MovePageName(string oldPageName, string newPageName)
|
|
{
|
|
int id = map.GetBySecond(oldPageName);
|
|
map.RemoveBySecond(oldPageName);
|
|
map.Add(id, newPageName);
|
|
}
|
|
|
|
public void DeletePageName(string pageName)
|
|
{
|
|
map.RemoveBySecond(pageName);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
StringBuilder result = new StringBuilder("Id Map:\n");
|
|
foreach (BiDictionary<int, string>.Pair pair in map)
|
|
result.AppendLine($"\t{pair.First}: {pair.Second}");
|
|
return result.ToString();
|
|
}
|
|
}
|
|
}
|