-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e521862
commit 47766ac
Showing
12 changed files
with
479 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Backtrace.Framework45Example | ||
{ | ||
public static class ApplicationSettings | ||
{ | ||
public const string Host = @"https://myserver.sp.backtrace.io:6097"; | ||
public const string Token = "4dca18e8769d0f5d10db0d1b665e64b3d716f76bf182fbcdad5d1d8070c12db0"; | ||
public const string DatabasePath = ""; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Backtrace.FrameworkExample/Backtrace.FrameworkExample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Backtrace\Backtrace.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Backtrace.Framework45Example.Model | ||
{ | ||
public class Node | ||
{ | ||
public virtual char Key { get; set; } | ||
public virtual bool IsTerminal { get; set; } | ||
public virtual Node Parent { get; set; } | ||
public virtual Dictionary<char, Node> Children { get; set; } | ||
|
||
public Node(char key) : this(key, false) { } | ||
|
||
public Node(char key, bool isTerminal) | ||
Check warning on line 18 in Backtrace.FrameworkExample/Model/Node.cs GitHub Actions / build
|
||
{ | ||
Key = key; | ||
IsTerminal = isTerminal; | ||
Children = new Dictionary<char, Node>(); | ||
} | ||
|
||
/// <summary> | ||
/// Return the word at this node if the node is terminal; otherwise, return null | ||
/// </summary> | ||
public virtual string Word | ||
{ | ||
get | ||
{ | ||
if (!IsTerminal) | ||
return null; | ||
Check warning on line 33 in Backtrace.FrameworkExample/Model/Node.cs GitHub Actions / build
|
||
|
||
var curr = this; | ||
var stack = new Stack<char>(); | ||
|
||
while (curr.Parent != null) | ||
{ | ||
stack.Push(curr.Key); | ||
curr = curr.Parent; | ||
} | ||
|
||
return new String(stack.ToArray()); | ||
} | ||
|
||
} | ||
|
||
///// <summary> | ||
///// Returns an enumerable collection of terminal child nodes. | ||
///// </summary> | ||
public virtual IEnumerable<Node> GetTerminalChildren() | ||
{ | ||
foreach (var child in Children.Values) | ||
{ | ||
if (child.IsTerminal) | ||
yield return child; | ||
|
||
foreach (var grandChild in child.GetTerminalChildren()) | ||
if (grandChild.IsTerminal) | ||
yield return grandChild; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Remove this element upto its parent. | ||
/// </summary> | ||
public virtual void Remove() | ||
{ | ||
IsTerminal = false; | ||
|
||
if (Children.Count == 0 && Parent != null) | ||
{ | ||
Parent.Children.Remove(Key); | ||
|
||
if (!Parent.IsTerminal) | ||
Parent.Remove(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Backtrace.Framework45Example.Model | ||
{ | ||
public class Tree : IEnumerable<String> | ||
{ | ||
private Node _root { get; set; } | ||
|
||
public Tree() | ||
{ | ||
_root = new Node(' ', false); | ||
} | ||
|
||
/// <summary> | ||
/// Add word to tree | ||
/// </summary> | ||
public void Add(string word) | ||
{ | ||
if (string.IsNullOrEmpty(word)) | ||
{ | ||
throw new ArgumentException("Word is empty or null."); | ||
} | ||
|
||
var current = _root; | ||
for (int i = 0; i < word.Length; ++i) | ||
{ | ||
if (!current.Children.ContainsKey(word[i])) | ||
{ | ||
var newTreeNode = new Node(word[i]) | ||
{ | ||
Parent = current | ||
}; | ||
current.Children.Add(word[i], newTreeNode); | ||
} | ||
|
||
current = current.Children[word[i]]; | ||
} | ||
|
||
if (current.IsTerminal) | ||
{ | ||
throw new InvalidOperationException("Word already exists in Tree."); | ||
} | ||
current.IsTerminal = true; | ||
} | ||
|
||
/// <summary> | ||
/// Removes a word from the tree. | ||
/// </summary> | ||
public void Remove(string word) | ||
{ | ||
if (string.IsNullOrEmpty(word)) | ||
{ | ||
throw new ArgumentException("Word is empty or null."); | ||
} | ||
|
||
var current = _root; | ||
|
||
for (int i = 0; i < word.Length; ++i) | ||
{ | ||
if (!current.Children.ContainsKey(word[i])) | ||
{ | ||
var exception = new KeyNotFoundException("Word doesn't belong to tree."); | ||
throw exception; | ||
} | ||
current = current.Children[word[i]]; | ||
} | ||
|
||
if (!current.IsTerminal) | ||
{ | ||
var exception = new KeyNotFoundException("Word doesn't belong to tree."); | ||
throw exception; | ||
} | ||
current.Remove(); | ||
} | ||
|
||
#region IEnumerable<String> Implementation | ||
/// <summary> | ||
/// IEnumerable\<String\>.IEnumerator implementation. | ||
/// </summary> | ||
public IEnumerator<string> GetEnumerator() | ||
{ | ||
return _root.GetTerminalChildren().Select(node => node.Word).GetEnumerator(); | ||
} | ||
|
||
/// <summary> | ||
/// IEnumerable\<String\>.IEnumerator implementation. | ||
/// </summary> | ||
/// <returns></returns> | ||
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
#endregion IEnumerable<String> Implementation | ||
|
||
} | ||
} |
Oops, something went wrong.