// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
namespace UnrealBuildTool
{
///
/// Maps a unique string to an integer
///
class UniqueStringRegistry
{
// protect the InstanceMap
private object LockObject = new object();
// holds a mapping of string name to single instance
private Dictionary StringToInstanceMap = new Dictionary(StringComparer.OrdinalIgnoreCase);
public UniqueStringRegistry()
{
}
public bool HasString(string Name)
{
return StringToInstanceMap.ContainsKey(Name);
}
public int FindOrAddByName(string Name)
{
// look for existing one
int Instance = -1;
if (!StringToInstanceMap.TryGetValue(Name, out Instance))
{
lock (LockObject)
{
if (!StringToInstanceMap.TryGetValue(Name, out Instance))
{
// copy over the dictionary and add, so that other threads can keep reading out of the old one until it's time
Dictionary NewStringToInstanceMap = new Dictionary(StringToInstanceMap, StringComparer.OrdinalIgnoreCase);
// make and add a new instance number
Instance = StringToInstanceMap.Count;
NewStringToInstanceMap[Name] = Instance;
// replace the class's map
StringToInstanceMap = NewStringToInstanceMap;
}
}
}
return Instance;
}
public string[] GetStringNames()
{
return StringToInstanceMap.Keys.ToArray();
}
public int[] GetStringIds()
{
return StringToInstanceMap.Values.ToArray();
}
public string GetStringForId(int Id)
{
return StringToInstanceMap.First(x => x.Value == Id).Key;
}
}
}