This commit is contained in:
wsh
2025-03-18 12:02:34 +09:00
parent f58d7bd66a
commit b90983ce81
14 changed files with 2063 additions and 6 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e61e58991e239f54ab1f7da7d8b8fb27
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,67 @@
using System.Collections.Generic;
namespace XRLib.Collections
{
public class Trie
{
private readonly TrieNode root;
public Trie()
{
root = new TrieNode();
}
public void Insert(string word)
{
var currentNode = root;
foreach (var ch in word)
{
if (!currentNode.Children.ContainsKey(ch))
{
currentNode.Children[ch] = new TrieNode();
}
currentNode = currentNode.Children[ch];
}
currentNode.IsEndOfWord = true;
}
public bool Search(string word)
{
var currentNode = root;
foreach (var ch in word)
{
if (!currentNode.Children.ContainsKey(ch))
{
return false;
}
currentNode = currentNode.Children[ch];
}
return currentNode.IsEndOfWord;
}
public bool TryGetContainingWords(string substring, out List<string> result)
{
result = new List<string>();
CollectWordsContainingSubstring(root, "", substring, result);
if(result.Count == 0)
{
return false;
}
return true;
}
private void CollectWordsContainingSubstring(TrieNode node, string currentWord, string substring, List<string> result)
{
if (node.IsEndOfWord && currentWord.Contains(substring))
{
result.Add(currentWord);
}
foreach (var child in node.Children)
{
CollectWordsContainingSubstring(child.Value, currentWord + child.Key, substring, result);
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 40291d5bf2e198443a049121951dc755

View File

@@ -0,0 +1,16 @@
using System.Collections.Generic;
namespace XRLib.Collections
{
public class TrieNode
{
public Dictionary<char, TrieNode> Children { get; private set; }
public bool IsEndOfWord { get; set; }
public TrieNode()
{
Children = new Dictionary<char, TrieNode>();
IsEndOfWord = false;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5e3dc6c15b2541b44b270defa7dd39f8

View File

@@ -0,0 +1,57 @@
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));
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9ef5e91ef56e8364da0ea2c011be7be0