cppast/src/refl/OverTree.cs
2024-06-21 22:20:13 +08:00

74 lines
2.0 KiB
C#

using CppAst;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace refl
{
internal class OverTree
{
public const int PrefixSize = 6;
List<int> Ctors = new List<int>();
List<int> Funcs = new List<int>();
private bool isInit = false;
public OverTree() { }
public void Init(CppClass cppClass)
{
if (isInit)
{
return;
}
isInit = true;
foreach (var types in cppClass.Typedefs)
{
string key = types.Name.Substring(0, PrefixSize);
if(key == "__Ctor")
{
Ctors.Add(int.Parse(types.Name.Substring(PrefixSize)));
}
else if(key == "__Func")
{
Funcs.Add(int.Parse(types.Name.Substring(PrefixSize)));
}
}
Ctors.Sort();
Funcs.Sort();
}
public void Clear()
{
Ctors.Clear();
Funcs.Clear();
isInit = false;
}
public string Find(string name)
{
string key = name.Substring(0, PrefixSize);
int v = int.Parse(name.Substring(PrefixSize));
if (key == "__Ctor")
{
v = FindValue(Ctors, v);
}
else
{
v = FindValue(Funcs, v);
}
return key + v;
}
private static int FindValue(List<int> sortedNumbers, int target)
{
if(sortedNumbers.Count == 0)
{
return target;
}
int index = sortedNumbers.BinarySearch(target);
if(index < 0)
{
index = ~index;
}
index = index > 0 ? index - 1 : 0;
return sortedNumbers[index];
}
}
}