0.22.1
This commit is contained in:
8
Assets/Scripts/XRLib/DataStructure.meta
Normal file
8
Assets/Scripts/XRLib/DataStructure.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e61e58991e239f54ab1f7da7d8b8fb27
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
67
Assets/Scripts/XRLib/DataStructure/Trie.cs
Normal file
67
Assets/Scripts/XRLib/DataStructure/Trie.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/XRLib/DataStructure/Trie.cs.meta
Normal file
2
Assets/Scripts/XRLib/DataStructure/Trie.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 40291d5bf2e198443a049121951dc755
|
||||
16
Assets/Scripts/XRLib/DataStructure/TrieNode.cs
Normal file
16
Assets/Scripts/XRLib/DataStructure/TrieNode.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/XRLib/DataStructure/TrieNode.cs.meta
Normal file
2
Assets/Scripts/XRLib/DataStructure/TrieNode.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5e3dc6c15b2541b44b270defa7dd39f8
|
||||
57
Assets/Scripts/XRLib/DataStructure/TrieTest.cs
Normal file
57
Assets/Scripts/XRLib/DataStructure/TrieTest.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/XRLib/DataStructure/TrieTest.cs.meta
Normal file
2
Assets/Scripts/XRLib/DataStructure/TrieTest.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9ef5e91ef56e8364da0ea2c011be7be0
|
||||
Reference in New Issue
Block a user