I’m trying to add an Any method to a Heap i found on internet and over which i has been working on and yes, i need it to accept a lambda as a parameter, otherwise it’s of no use for me and i’m running out of ideas.
This is the error than is showing me in the console:
Can you show me how to correct what i have wrong?
This is so - far the methods i try:
-
Static method on an external static class:
public static bool Any(this IEnumerable source, Func<TSource, bool> predicate)
{
if (source == null)
{
throw new ArgumentNullException(“source”);
}
if (predicate == null)
{
throw new ArgumentNullException(“predicate”);
}
foreach (TSource local in source)
{
if (predicate(local))
{
return true;
}
}
return false;
}
Also i try (and already delete) some proper methods inside the Heap class, but then i notice i need the any to accept lambdas, so a static method must be.
This is the code of the Heap, you can see some modifications commented on it:
using System;
using System.Collections.Generic;
public class Heap<T> where T : IHeapItem<T>, IEnumerable<T> {
List<T> items; //This used to be an array, and i don't have any problem if to fix this bug i must return it to be one
int currentItemCount;
public Heap(int maxHeapSize) {
items = new List<T>();//new T[maxHeapSize];
}
public void Add(T item) {
item.HeapIndex = currentItemCount;
items[currentItemCount] = item;
SortUp(item);
currentItemCount++;
}
public T RemoveFirst() {
T firstItem = items[0];
currentItemCount--;
items[0] = items[currentItemCount];
items[0].HeapIndex = 0;
SortDown(items[0]);
return firstItem;
}
public void UpdateItem(T item) {
SortUp(item);
}
public int Count {
get {
return currentItemCount;
}
}
public bool Contains(T item) {
return Equals(items[item.HeapIndex], item);
}
//TODO: Metodo ANY para reemplazar y poder hacer la comparación exitosamente ESTO DEBIESE REPARAR EL PROBLEMA
//public bool Any<T>(T isThereAny)
//{
// return items.Any(Toz => Toz.Equals(isThereAny));
//}
void SortDown(T item) {
while (true) {
int childIndexLeft = item.HeapIndex * 2 + 1;
int childIndexRight = item.HeapIndex * 2 + 2;
int swapIndex = 0;
if (childIndexLeft < currentItemCount) {
swapIndex = childIndexLeft;
if (childIndexRight < currentItemCount) {
if (items[childIndexLeft].CompareTo(items[childIndexRight]) < 0) {
swapIndex = childIndexRight;
}
}
if (item.CompareTo(items[swapIndex]) < 0) {
Swap (item,items[swapIndex]);
}
else {
return;
}
}
else {
return;
}
}
}
void SortUp(T item) {
int parentIndex = (item.HeapIndex-1)/2;
while (true) {
T parentItem = items[parentIndex];
if (item.CompareTo(parentItem) > 0) {
Swap (item,parentItem);
}
else {
break;
}
parentIndex = (item.HeapIndex-1)/2;
}
}
void Swap(T itemA, T itemB) {
items[itemA.HeapIndex] = itemB;
items[itemB.HeapIndex] = itemA;
int itemAIndex = itemA.HeapIndex;
itemA.HeapIndex = itemB.HeapIndex;
itemB.HeapIndex = itemAIndex;
}
}
public interface IHeapItem<T> : IComparable<T> {
int HeapIndex {
get;
set;
}
}
Any comment, suggestion, question or similar to improve this question and/or help me to achieve the desired result would be much apreciated too.
Thanks for your time and attention and wishfully waiting for your comment/answer