7

I am writing a hash table using given interfaces, but I get the error says as the title at

return (IEnumerator<Key>)items.GetEnumerator();

and

 foreach (String first in ht);

The entire code shown as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace RIT_CS
{

public class NonExistentKey<Key> : Exception
{

    public Key BadKey { get; private set; }

    public NonExistentKey(Key k) :
        base("Non existent key in HashTable: " + k)
    {
        BadKey = k;
    }

}

interface Table<Key, Value> : IEnumerable<Key>
{

    void Put(Key k, Value v);

    bool Contains(Key k);

    Value Get(Key k);
}

public struct KeyValue<Key, Value>
{
    public Key K { get; set;}
    public Value V { get; set; }
}

class MyTable<Key, Value>:Table<Key, Value>
{
    private int size;
    private int count=0;
    private  LinkedList<KeyValue<Key, Value>>[] items;

    public MyTable(int size)
    {
        ... 
    }

    public void SetSize(int size)
    {
        ...
    }
    public LinkedList<KeyValue<Key, Value>>[] GetItems()
    {
        return items;
    }
    protected int Getposition(Key k)
    {
       ...
    }
    protected LinkedList<KeyValue<Key, Value>> GetLinkedList(int position)
    {

        return items[position];
    }
  public  void Put(Key k, Value v)
    {   
    }

 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  {
      return (System.Collections.IEnumerator)GetEnumerator();
  }

public  IEnumerator<Key> GetEnumerator()
  {

      return (IEnumerator<Key>)items.GetEnumerator();
  }


  public  bool Contains(Key k)
    {
    }
   public Value Get(Key k)
    {
    }

}

class TableFactory
{

    public static MyTable<Key, Value> Make<Key, Value>(int size = 100, double loadThreshold = 0.75)
    {
        MyTable<Key, Value> mytable = new MyTable<Key, Value>(size);
        return mytable;
    }

}

class MainClass
{
    public static void Main(string[] args)
    {
        MyTable<String, String> ht = TableFactory.Make<String, String>(10, 0.75);
        ht.Put("Joe", "Doe");
        ht.Put("Jane", "Brain");
        ht.Put("Chris", "Swiss");
        try
        {
            foreach (String first in ht)
            {
                Console.WriteLine(first + " -> " + ht.Get(first));
            }
            Console.WriteLine("=========================");

            ht.Put("Wavy", "Gravy");
            ht.Put("Chris", "Bliss");
           foreach (String first in ht)
            {
                Console.WriteLine(first + " -> " + ht.Get(first));
            }
            Console.WriteLine("=========================");

            Console.Write("Jane -> ");
            Console.WriteLine(ht.Get("Jane"));
            Console.Write("John -> ");
            Console.WriteLine(ht.Get("John"));
        }
        catch (NonExistentKey<String> nek)
        {
            Console.WriteLine(nek.Message);
            Console.WriteLine(nek.StackTrace);
        }

        Console.ReadLine();
    }
  }
}

To look better, I deleted some irrelevant codes.

user3330383
  • 171
  • 1
  • 2
  • 6
  • 1
    Can you share a smaller example? Is all the code really necessary to explain the issue? – kukido Feb 20 '14 at 00:11
  • It might indeed pay off to illustrate problems with representative code samples rather than posting entire sources. Anyway, here's +10 as a welcome. – J. Katzwinkel Feb 20 '14 at 00:16

3 Answers3

4

Just use the first option from https://stackoverflow.com/a/1272677/3162415 and write:

return ((IEnumerable<Key>)items).GetEnumerator();
1

items field is declared as

private LinkedList<KeyValue<Key, Value>>[] items;

And according to MSDN LinkedList<T> implements IEnumerable<T>, which in your case is IEnumerable<KeyValue<Key, Value>>.

So, given that IEnumerable<T>.GetEnumerator() returns IEnumerator<T>, which is again IEnumerator<KeyValue<Key, Value>> in your case, you can't cast it to IEnumerator<Key>. It won't work.

You can try following:

public IEnumerator<Key> GetEnumerator()
{
    return items.Select(x => x.K).GetEnumerator();
}
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • Hi MarcinJuraszek, I tried, but it seems like every element in items is of linkedlist> type,so I cannot get my (Key K) using x => x.K, so what should I do about this? – user3330383 Feb 20 '14 at 01:13
0
LinkedList<KeyValuePair<int, string>> items = new LinkedList<KeyValuePair<int, string>>();
            items.AddFirst(new KeyValuePair<int, string>(1, "qqq"));
            items.AddFirst(new KeyValuePair<int, string>(2, "www"));
            items.AddFirst(new KeyValuePair<int, string>(3, "eee"));

            var keys = items.Select(item => item.Key);

            foreach (var key in keys)
            {

            }
Maxim Q
  • 19
  • 1
  • 2