From be3c34c8fef24ae8976134c0a554c99e8dcc45f5 Mon Sep 17 00:00:00 2001
From: Alexander Schneider <alexander.schneider@th-nuernberg.de>
Date: Fri, 24 Jun 2022 00:52:53 +0200
Subject: [PATCH] Add example solution for exercise generic dictionary.

---
 .../Exercise Generics Dictionary.csproj       |  55 ++++++
 Exercise Generics Dictionary/Program.cs       | 169 ++++++++++++++++++
 .../Properties/AssemblyInfo.cs                |  35 ++++
 3 files changed, 259 insertions(+)
 create mode 100644 Exercise Generics Dictionary/Exercise Generics Dictionary.csproj
 create mode 100644 Exercise Generics Dictionary/Program.cs
 create mode 100644 Exercise Generics Dictionary/Properties/AssemblyInfo.cs

diff --git a/Exercise Generics Dictionary/Exercise Generics Dictionary.csproj b/Exercise Generics Dictionary/Exercise Generics Dictionary.csproj
new file mode 100644
index 0000000..75914fc
--- /dev/null
+++ b/Exercise Generics Dictionary/Exercise Generics Dictionary.csproj	
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+    <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props"
+            Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
+    <PropertyGroup>
+        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+        <ProjectGuid>{EBD4CA3D-A7CF-4ADD-BBB1-1251AC73C851}</ProjectGuid>
+        <OutputType>Exe</OutputType>
+        <AppDesignerFolder>Properties</AppDesignerFolder>
+        <RootNamespace>Exercise_Generics_Dictionary</RootNamespace>
+        <AssemblyName>Exercise_Generics_Dictionary</AssemblyName>
+        <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
+        <FileAlignment>512</FileAlignment>
+        <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
+    </PropertyGroup>
+    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+        <PlatformTarget>AnyCPU</PlatformTarget>
+        <DebugSymbols>true</DebugSymbols>
+        <DebugType>full</DebugType>
+        <Optimize>false</Optimize>
+        <OutputPath>bin\Debug\</OutputPath>
+        <DefineConstants>DEBUG;TRACE</DefineConstants>
+        <ErrorReport>prompt</ErrorReport>
+        <WarningLevel>4</WarningLevel>
+    </PropertyGroup>
+    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+        <PlatformTarget>AnyCPU</PlatformTarget>
+        <DebugType>pdbonly</DebugType>
+        <Optimize>true</Optimize>
+        <OutputPath>bin\Release\</OutputPath>
+        <DefineConstants>TRACE</DefineConstants>
+        <ErrorReport>prompt</ErrorReport>
+        <WarningLevel>4</WarningLevel>
+    </PropertyGroup>
+    <ItemGroup>
+        <Reference Include="System"/>
+        <Reference Include="System.Core"/>
+        <Reference Include="System.Data"/>
+        <Reference Include="System.Xml"/>
+    </ItemGroup>
+    <ItemGroup>
+        <Compile Include="Program.cs"/>
+        <Compile Include="Properties\AssemblyInfo.cs"/>
+    </ItemGroup>
+    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>
+    <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+         Other similar extension points exist, see Microsoft.Common.targets.
+    <Target Name="BeforeBuild">
+    </Target>
+    <Target Name="AfterBuild">
+    </Target>
+    -->
+
+</Project>
diff --git a/Exercise Generics Dictionary/Program.cs b/Exercise Generics Dictionary/Program.cs
new file mode 100644
index 0000000..9614531
--- /dev/null
+++ b/Exercise Generics Dictionary/Program.cs	
@@ -0,0 +1,169 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2017 Alexander Schneider
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ *SOFTWARE.
+*/
+
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using static System.Console;
+
+namespace Schneider.Prog2.Exercise.Generic.Dictionary
+{
+
+    class Lexicon<Tk, Td> : IEnumerable<Td> where Tk:IComparable<Tk>
+    {
+
+        class Element
+        {
+            public Td Data;
+            public Tk Key;
+            public Element Next;
+
+            public Element(Td data, Tk key)
+            {
+                Data = data;
+                Key = key;
+            }
+        }
+
+        private Element root;
+        
+        public void Add(Tk key, Td data)
+        {
+            Element newEntry = new Element(data, key);
+            newEntry.Next = root;
+            root = newEntry;
+        }
+
+        public void Delete(Tk victim)
+        {
+            Element runner = root;
+            Element preRunner = null;
+
+            while (runner != null)
+            {
+                if (runner.Key.CompareTo(victim) == 0)
+                {
+                    if (preRunner == null)
+                    {
+                        root = runner.Next;
+                        runner = root;
+                    }
+                    else
+                    {
+                        preRunner.Next = runner.Next;
+                        runner = runner.Next;
+                    }
+                }
+                else
+                {
+                    preRunner = runner;
+                    runner = runner.Next;
+                }
+            }  
+        }
+
+        public Td[] Find(Tk wanted)
+        {
+            Element tarzan = root;
+
+            // erstmal zählen...
+            int counter = 0;
+            while (tarzan != null)
+            {
+                if (tarzan.Key.CompareTo(wanted) == 0)
+                {
+                    counter++;
+                }
+                tarzan = tarzan.Next;
+            }
+
+            if (counter > 0)
+            {
+                // und jetzt Ergebnis erzeugen
+                Td[] result = new Td[counter];
+                tarzan = root;
+                int index = 0;
+                while (tarzan != null)
+                {
+                    if (tarzan.Key.CompareTo(wanted) == 0)
+                    {
+                        result[index] = tarzan.Data;
+                        index++;
+                    }
+                    tarzan = tarzan.Next;
+                }
+                return result;
+            }
+
+            return new Td[0];
+        }
+        
+        // nicht nötig aber praktisch zum Ausgeben
+        public IEnumerator<Td> GetEnumerator()
+        {
+            Element tarzan = root;
+            while (tarzan != null)
+            {
+                yield return tarzan.Data;
+                tarzan = tarzan.Next;
+            }
+        }
+
+        IEnumerator IEnumerable.GetEnumerator()
+        {
+            return GetEnumerator();
+        }
+    }
+    
+    internal class Program
+    {
+        public static void Main(string[] args)
+        {
+            Lexicon<string,string> lexicon = new Lexicon<string, string>();
+            lexicon.Add("A", "alex");
+            lexicon.Add("O", "other");
+            lexicon.Add("O", "other");
+            lexicon.Add("O", "other");
+            lexicon.Add("A", "alex");
+            
+            lexicon.Delete("O");
+
+            // nur zum Testen...
+            foreach (var data in lexicon)
+            {
+               WriteLine(data);   
+            }
+            WriteLine("- - -");
+            
+            // und noch das Array Testen. Vorher wieder etwas hinein geben.
+            lexicon.Add("O", "other");
+            lexicon.Add("O", "other");
+            lexicon.Add("O", "other");
+            foreach (string value in lexicon.Find("A"))
+            {
+                WriteLine(value);
+            }
+        }
+    }
+}
diff --git a/Exercise Generics Dictionary/Properties/AssemblyInfo.cs b/Exercise Generics Dictionary/Properties/AssemblyInfo.cs
new file mode 100644
index 0000000..208bca5
--- /dev/null
+++ b/Exercise Generics Dictionary/Properties/AssemblyInfo.cs	
@@ -0,0 +1,35 @@
+using System.Reflection;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("Exercise_Generics_Dictionary")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("Exercise_Generics_Dictionary")]
+[assembly: AssemblyCopyright("Copyright ©  2022")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("EBD4CA3D-A7CF-4ADD-BBB1-1251AC73C851")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
\ No newline at end of file
-- 
GitLab