mirror of
https://github.com/Lendaia/oe-alga-feladatok.git
synced 2026-04-23 04:16:32 +01:00
thx
This commit is contained in:
@@ -1,6 +1,134 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
namespace OE.ALGA.Adatszerkezetek
|
||||
{
|
||||
// 6. heti labor feladat - Tesztek: 06_SzotarTesztek.cs
|
||||
|
||||
public class SzotarElem<K, T>
|
||||
{
|
||||
public K Kulcs;
|
||||
public T Tart;
|
||||
|
||||
public SzotarElem(K kulcs, T tart)
|
||||
{
|
||||
Kulcs = kulcs;
|
||||
Tart = tart;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class HasitoSzotarTulcsordulasiTerulettel<K, T> : Szotar<K, T>
|
||||
{
|
||||
private SzotarElem<K, T>[] E;
|
||||
private Func<K, int> h;
|
||||
private Lista<SzotarElem<K, T>> U = new LancoltLista<SzotarElem<K, T>>();
|
||||
|
||||
public HasitoSzotarTulcsordulasiTerulettel(int meret, Func<K, int> hasitoFuggveny)
|
||||
{
|
||||
E = new SzotarElem<K, T>[meret];
|
||||
this.h = x => Math.Abs(hasitoFuggveny(x)) % E.Length;
|
||||
}
|
||||
|
||||
public HasitoSzotarTulcsordulasiTerulettel(int meret) : this(meret, x => x.GetHashCode())
|
||||
{
|
||||
}
|
||||
|
||||
private SzotarElem<K, T> KulcsKeres(K kulcs)
|
||||
{
|
||||
int i = h(kulcs);
|
||||
if (E[i] != null && EqualityComparer<K>.Default.Equals(E[i].Kulcs, kulcs))
|
||||
{
|
||||
return E[h(kulcs)];
|
||||
}
|
||||
else
|
||||
{
|
||||
SzotarElem<K, T> e = null;
|
||||
U.Bejar(x =>
|
||||
{
|
||||
if (EqualityComparer<K>.Default.Equals(x.Kulcs, kulcs))
|
||||
{
|
||||
e = x;
|
||||
}
|
||||
});
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
public void Beir(K kulcs, T ertek)
|
||||
{
|
||||
SzotarElem<K, T> e = KulcsKeres(kulcs);
|
||||
if (e != null)
|
||||
{
|
||||
e.Tart = ertek;
|
||||
}
|
||||
else
|
||||
{
|
||||
SzotarElem<K, T> newDictenary = new SzotarElem<K, T>(kulcs, ertek);
|
||||
if (E[h(kulcs)] == null)
|
||||
{
|
||||
E[h(kulcs)] = newDictenary;
|
||||
}
|
||||
else
|
||||
{
|
||||
U.Hozzafuz(newDictenary);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public T Kiolvas(K kulcs)
|
||||
{
|
||||
SzotarElem<K, T> e = KulcsKeres(kulcs);
|
||||
if (e != null)
|
||||
{
|
||||
return e.Tart;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new HibasKulcsKivetel();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void Torol(K kulcs)
|
||||
{
|
||||
if (E[h(kulcs)] != null && EqualityComparer<K>.Default.Equals(E[h(kulcs)].Kulcs, kulcs))
|
||||
{
|
||||
E[h(kulcs)] = null;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
SzotarElem<K, T> e = null;
|
||||
U.Bejar(x =>
|
||||
{
|
||||
if (EqualityComparer<K>.Default.Equals(x.Kulcs, kulcs))
|
||||
{
|
||||
e = x;
|
||||
}
|
||||
});
|
||||
if (e != null)
|
||||
{
|
||||
U.Torol(e);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new HibasKulcsKivetel();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user