Files

122 lines
2.6 KiB
C#
Raw Permalink Normal View History

2025-03-07 11:04:47 +01:00
using System.Collections.Generic;
using System;
namespace Seged
{
class Varolista<T>
{
private List<T> list;
public Varolista()
{
list = new List<T>();
}
public void Extend(List<T> list)
{
foreach (var item in list)
{
this.list.Add(item);
}
}
public List<T> GetList()
{
return list;
}
public void SetList(List<T> list)
{
this.list = list;
}
}
class Sor<T> : Varolista<T>
{
private int kezdo = 0;
public void Append(T item)
{
List<T> value = new List<T> { item };
Extend(value);
}
public int Length()
{
return GetList().Count - kezdo;
}
public T Pop()
{
T e = GetList()[kezdo++];
if (kezdo > 5 && kezdo > GetList().Count / 2)
{
SetList(GetList().GetRange(kezdo, GetList().Count - kezdo));
kezdo = 0;
}
return e;
}
}
class RLElem<T>
{
private readonly int ertek;
private readonly T elem;
public int GetErtek()
{
return ertek;
}
public T Elem()
{
return elem;
}
public RLElem(int ertek, T elem)
{
this.ertek = ertek;
this.elem = elem;
}
public bool lt(int other)
{
return ertek < other;
}
}
class RendezettLista<T> : Varolista<T>
{
private readonly Func<T, int> f; // Sorting function
public RendezettLista(Func<T, int> f)
{
this.f = f;
}
public void Append(T elem)
{
var par = new RLElem<T>(f(elem), elem);
List<RLElem<T>> list = GetList().ConvertAll(e => new RLElem<T>(f(e), e));
int index = list.BinarySearch(par, Comparer<RLElem<T>>.Create((a, b) => a.GetErtek().CompareTo(b.GetErtek())));
if (index < 0) index = ~index; // Get correct insertion position
list.Insert(index, par); // Insert at sorted position
SetList(list.ConvertAll(e => e.Elem())); // Convert back to original type
}
public T Pop()
{
List<T> list = GetList();
if (list.Count == 0) throw new InvalidOperationException("The list is empty.");
T elem = list[0];
list.RemoveAt(0);
SetList(list);
return elem;
}
}
}