From c640109d4323f457ac138f88ad3489b81e53afd5 Mon Sep 17 00:00:00 2001 From: syneffort Date: Tue, 18 Apr 2023 10:16:04 +0900 Subject: [PATCH] hashTable --- .vscode/launch.json | 29 +++++++++ .vscode/tasks.json | 41 ++++++++++++ .../Client}/UseConcurrentDictionary.cs | 4 +- .../Client}/UseDictionary.cs | 2 +- .../Client}/UseHashTable.cs | 2 +- .../Client/UseSimpleHashTable.cs | 21 ++++++ .../hashTableStructure/HashTable/Node.cs | 16 +++++ .../HashTable/SimpleHashTable.cs | 65 +++++++++++++++++++ DataStructure/hashTableStructure/Program.cs | 20 +++++- DataStructure/treeStructure/Program.cs | 16 ----- 10 files changed, 194 insertions(+), 22 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json rename DataStructure/{treeStructure/Structure => hashTableStructure/Client}/UseConcurrentDictionary.cs (93%) rename DataStructure/{treeStructure/Structure => hashTableStructure/Client}/UseDictionary.cs (91%) rename DataStructure/{treeStructure/Structure => hashTableStructure/Client}/UseHashTable.cs (90%) create mode 100644 DataStructure/hashTableStructure/Client/UseSimpleHashTable.cs create mode 100644 DataStructure/hashTableStructure/HashTable/Node.cs create mode 100644 DataStructure/hashTableStructure/HashTable/SimpleHashTable.cs delete mode 100644 DataStructure/treeStructure/Program.cs diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..33b65f8 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,29 @@ +{ + // IntelliSense를 사용하여 가능한 특성에 대해 알아보세요. + // 기존 특성에 대한 설명을 보려면 가리킵니다. + // 자세한 내용을 보려면 https://go.microsoft.com/fwlink/?linkid=830387을(를) 방문하세요. + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/DataStructure/hashTableStructure/bin/Debug/net7.0/hashTableStructure.dll", + "args": [], + "cwd": "${workspaceFolder}/DataStructure/hashTableStructure", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..a7cb9aa --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/DataStructure/hashTableStructure/hashTableStructure.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/DataStructure/hashTableStructure/hashTableStructure.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/DataStructure/hashTableStructure/hashTableStructure.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/DataStructure/treeStructure/Structure/UseConcurrentDictionary.cs b/DataStructure/hashTableStructure/Client/UseConcurrentDictionary.cs similarity index 93% rename from DataStructure/treeStructure/Structure/UseConcurrentDictionary.cs rename to DataStructure/hashTableStructure/Client/UseConcurrentDictionary.cs index aa4b391..6bd40dc 100644 --- a/DataStructure/treeStructure/Structure/UseConcurrentDictionary.cs +++ b/DataStructure/hashTableStructure/Client/UseConcurrentDictionary.cs @@ -4,7 +4,7 @@ using System.Collections.Concurrent; using System.Threading; using System.Threading.Tasks; -namespace treeStructure.Structure +namespace hashTableStructure.Client { public class UseConcurrentDictionary { @@ -27,7 +27,7 @@ namespace treeStructure.Structure Task t2 = Task.Factory.StartNew(() => { int key = 1; - string val; + string val = string.Empty; while (key <= 100) { if (dict.TryGetValue(key, out val)) diff --git a/DataStructure/treeStructure/Structure/UseDictionary.cs b/DataStructure/hashTableStructure/Client/UseDictionary.cs similarity index 91% rename from DataStructure/treeStructure/Structure/UseDictionary.cs rename to DataStructure/hashTableStructure/Client/UseDictionary.cs index 542dc14..013ae8a 100644 --- a/DataStructure/treeStructure/Structure/UseDictionary.cs +++ b/DataStructure/hashTableStructure/Client/UseDictionary.cs @@ -1,7 +1,7 @@ using System; using System.Collections; -namespace treeStructure.Structure +namespace hashTableStructure.Client { public class UseDictionary { diff --git a/DataStructure/treeStructure/Structure/UseHashTable.cs b/DataStructure/hashTableStructure/Client/UseHashTable.cs similarity index 90% rename from DataStructure/treeStructure/Structure/UseHashTable.cs rename to DataStructure/hashTableStructure/Client/UseHashTable.cs index 4b2d72a..962d360 100644 --- a/DataStructure/treeStructure/Structure/UseHashTable.cs +++ b/DataStructure/hashTableStructure/Client/UseHashTable.cs @@ -1,7 +1,7 @@ using System; using System.Collections; -namespace treeStructure.Structure +namespace hashTableStructure.Client { public class UseHashTable { diff --git a/DataStructure/hashTableStructure/Client/UseSimpleHashTable.cs b/DataStructure/hashTableStructure/Client/UseSimpleHashTable.cs new file mode 100644 index 0000000..ab739b0 --- /dev/null +++ b/DataStructure/hashTableStructure/Client/UseSimpleHashTable.cs @@ -0,0 +1,21 @@ +using System; +using hashTableStructure.HashTable; + +namespace hashTableStructure.Client +{ + class UseSimpleHashTable + { + public static void DoWork() + { + SimpleHashTable ht = new SimpleHashTable(); + ht.Put("Lim S", "Sales 01"); + ht.Put("Bea H", "Sales 02"); + ht.Put("Kim P", "SW 01"); + ht.Put("Lee C", "SW 02"); + + System.Console.WriteLine(ht.Get("Lee C")); + System.Console.WriteLine(ht.Get("Lim S")); + System.Console.WriteLine(ht.Contains("Unknown")); + } + } +} \ No newline at end of file diff --git a/DataStructure/hashTableStructure/HashTable/Node.cs b/DataStructure/hashTableStructure/HashTable/Node.cs new file mode 100644 index 0000000..f9f0cec --- /dev/null +++ b/DataStructure/hashTableStructure/HashTable/Node.cs @@ -0,0 +1,16 @@ +namespace hashTableStructure.HashTable +{ + class Node + { + public object Key { get; set; } + public object Value { get; set; } + public Node Next { get; set; } + + public Node(object key, object value) + { + this.Key = key; + this.Value = value; + this.Next = null; + } + } +} \ No newline at end of file diff --git a/DataStructure/hashTableStructure/HashTable/SimpleHashTable.cs b/DataStructure/hashTableStructure/HashTable/SimpleHashTable.cs new file mode 100644 index 0000000..25b86bd --- /dev/null +++ b/DataStructure/hashTableStructure/HashTable/SimpleHashTable.cs @@ -0,0 +1,65 @@ +namespace hashTableStructure.HashTable +{ + class SimpleHashTable + { + private const int INITIAL_SIZE = 16; + private int _size; + private Node[] _buckets; + + public SimpleHashTable() + { + _size = INITIAL_SIZE; + _buckets = new Node[_size]; + } + + public void Put(object key, object value) + { + int index = HashFunction(key); + if (_buckets[index] == null) + { + _buckets[index] = new Node(key, value); + } + else + { + Node newNode = new Node(key, value); + newNode.Next = _buckets[index]; + _buckets[index] = newNode; + } + } + + public object Get(object key) + { + int index = HashFunction(key); + if (_buckets[index] != null) + { + for (Node n = _buckets[index]; n != null; n = n.Next) + { + if (n.Key == key) + return n.Value; + } + } + + return null; + } + + public bool Contains(object key) + { + int index = HashFunction(key); + if (_buckets[index] != null) + { + for (Node n = _buckets[index]; n != null; n = n.Next) + { + if (n.Key == key) + return true; + } + } + + return false; + } + + protected virtual int HashFunction(object key) + { + return Math.Abs(key.GetHashCode() + 1 + ((key.GetHashCode() >> 5) + 1) % _size) % _size; + } + } +} \ No newline at end of file diff --git a/DataStructure/hashTableStructure/Program.cs b/DataStructure/hashTableStructure/Program.cs index 3751555..d23d37d 100644 --- a/DataStructure/hashTableStructure/Program.cs +++ b/DataStructure/hashTableStructure/Program.cs @@ -1,2 +1,18 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); +using hashTableStructure.Client; + +namespace hashTableStructure +{ + static class Program + { + static void Main(string[] args) + { + //UseHashTable.DoWork(); + + // UseDictionary.DoWork(); + + // UseConcurrentDictionary.DoWork(); + + UseSimpleHashTable.DoWork(); + } + } +} \ No newline at end of file diff --git a/DataStructure/treeStructure/Program.cs b/DataStructure/treeStructure/Program.cs deleted file mode 100644 index b161f95..0000000 --- a/DataStructure/treeStructure/Program.cs +++ /dev/null @@ -1,16 +0,0 @@ -using treeStructure.Structure; - -namespace treeStructure -{ - static class Program - { - static void Main(string[] args) - { - //UseHashTable.DoWork(); - - // UseDictionary.DoWork(); - - UseConcurrentDictionary.DoWork(); - } - } -} \ No newline at end of file