58 lines
1.5 KiB
C#
58 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace XRLib.Collections
|
|
{
|
|
public class TrieTest : MonoBehaviour
|
|
{
|
|
public TMP_InputField input;
|
|
public TMP_InputField search;
|
|
public TextMeshProUGUI prf_InputText;
|
|
public TextMeshProUGUI searchResult;
|
|
public List<TextMeshProUGUI> inputs = new ();
|
|
|
|
private Trie trie;
|
|
|
|
private void Awake()
|
|
{
|
|
trie = new Trie();
|
|
input.onEndEdit.AddListener((s) => Insert());
|
|
search.onValueChanged.AddListener((s) => Search());
|
|
}
|
|
|
|
public void Insert()
|
|
{
|
|
if(input.text.Length == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if(trie.Search(input.text))
|
|
{
|
|
return;
|
|
}
|
|
|
|
trie.Insert(input.text);
|
|
var t = Instantiate(prf_InputText, prf_InputText.transform.parent);
|
|
t.GetComponent<TextMeshProUGUI>().SetText(input.text);
|
|
inputs.Add(t);
|
|
t.transform.position = new Vector3(t.transform.position.x, t.transform.position.y + inputs.Count * 30, t.transform.position.z);
|
|
input.text = "";
|
|
}
|
|
|
|
public void Search()
|
|
{
|
|
if(search.text.Length == 0)
|
|
{
|
|
searchResult.SetText("");
|
|
return;
|
|
}
|
|
if (trie.TryGetContainingWords(search.text, out var words))
|
|
{
|
|
searchResult.SetText(string.Join(", ", words));
|
|
}
|
|
}
|
|
}
|
|
}
|