Click here to Skip to main content
15,887,330 members
Home / Discussions / C#
   

C#

 
GeneralRe: Generic collections Pin
Nicholas Marty19-Jul-13 1:17
professionalNicholas Marty19-Jul-13 1:17 
GeneralRe: Generic collections Pin
PozzaVecia19-Jul-13 1:26
PozzaVecia19-Jul-13 1:26 
GeneralRe: Generic collections Pin
Nicholas Marty19-Jul-13 2:07
professionalNicholas Marty19-Jul-13 2:07 
AnswerRe: Generic collections Pin
Keith Barrow19-Jul-13 1:04
professionalKeith Barrow19-Jul-13 1:04 
GeneralRe: Generic collections Pin
Nicholas Marty19-Jul-13 2:09
professionalNicholas Marty19-Jul-13 2:09 
GeneralRe: Generic collections Pin
Keith Barrow19-Jul-13 2:33
professionalKeith Barrow19-Jul-13 2:33 
AnswerRe: Generic collections Pin
Richard Deeming19-Jul-13 3:22
mveRichard Deeming19-Jul-13 3:22 
AnswerRe: Generic collections Pin
BillWoodruff21-Jul-13 0:57
professionalBillWoodruff21-Jul-13 0:57 
I really like (and upvoted) your question; it was fun to see what I could implement quickly, and I'm going to post the code here before I read the other responses on the thread, since I feel I can learn something from knowing whether or not the code I post actually meets your requirements, and, possibly, learning from other people's responses.

I wonder if I have not just duplicated the functionality of a Dictionary<K, V> ? Also, I suspect Linq queries could be used in some way, efficiently here, rather than, as I did, creating two Lists in the class (memory cost ?).

Disclaimer: I've tested only with KeyValuePair<string, double> since that's what you used. Testing with instances of a custom class as K, or V: that's on my list Smile | :)

Here's the revised class:
C#
public class IndexedKVPList<K, V> : List<KeyValuePair<K, V>>
{
    private readonly List<K> keyList = new List<K>();
    private readonly List<V> valueList = new List<V>();

    // modified to use 'new
    new public void Add(KeyValuePair<K, V> newKVP)
    {
        keyList.Add(newKVP.Key);
        valueList.Add(newKVP.Value);
        base.Add(newKVP);
    }

    // new function to Remove KeyValue pair
    new public void Remove(KeyValuePair<K, V> killKVP)
    {
        if(! this.Contains(killKVP)) throw new Exception("error in Remove in IndexedKVPPair");

        keyList.Remove(killKVP.Key);
        valueList.Remove(killKVP.Value);
        base.Remove(killKVP);
    }

    public KeyValuePair<K, V> GetKVPByValue(V theValue)
    {
        return this[valueList.IndexOf(theValue)];
    }

    public K GetKeyByValue(V theValue)
    {
        return this[valueList.IndexOf(theValue)].Key;
    }

    // new function to get the index by KeyValue pair
    public int GetIndexByKVP(KeyValuePair<K, V> theKVPPair)
    {
        return this.IndexOf(theKVPPair);
    }

    // new function to get the index by Value
    // what happens if there are multiple identical values ?
    public int GetIndexByValue(V theValue)
    {
        return (valueList.IndexOf(theValue));
    }

    public KeyValuePair<K, V> GetKVPByKey(K theKey)
    {
        return this[keyList.IndexOf(theKey)];
    }

    public V GetValueByKey(K theKey)
    {
        return this[keyList.IndexOf(theKey)].Value;
    }

    // new function to get the index by Key
    public int GetIndexByKey(K theKey)
    {
        return (keyList.IndexOf(theKey));
    }
}
Here's the revised test:
XML
private void TestIKVP()
{
    var iKVList = new IndexedKVPList<string, double>();

    for (int i = 0; i < 10; i++)
    {
        iKVList.Add(new KeyValuePair<string, double>("Key: " + i, i));
    }

    var kvp1 = iKVList.GetKVPByKey("Key: 8");
    var kvp2 = iKVList.GetKVPByValue(6);

    Console.WriteLine(kvp1 + " " + kvp2);

    // test Remove function
    iKVList.Remove(iKVList[4]);

    foreach (var theKVP in iKVList)
    {
        Console.WriteLine(theKVP.Key + " Value:" + theKVP.Value.GetType());
    }

    Console.WriteLine(iKVList.GetIndexByKVP(kvp2));
    Console.WriteLine(iKVList.GetIndexByKey("Key: 9"));
    Console.WriteLine(iKVList.GetIndexByValue(3));
}


~
“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal


modified 22-Jul-13 7:06am.

GeneralRe: Generic collections Pin
PozzaVecia21-Jul-13 23:05
PozzaVecia21-Jul-13 23:05 
GeneralRe: Generic collections Pin
BillWoodruff22-Jul-13 0:44
professionalBillWoodruff22-Jul-13 0:44 
GeneralRe: Generic collections Pin
PozzaVecia22-Jul-13 1:14
PozzaVecia22-Jul-13 1:14 
GeneralRe: Generic collections Pin
harold aptroot22-Jul-13 1:05
harold aptroot22-Jul-13 1:05 
GeneralRe: Generic collections Pin
PozzaVecia22-Jul-13 3:31
PozzaVecia22-Jul-13 3:31 
GeneralRe: Generic collections Pin
BillWoodruff23-Jul-13 2:41
professionalBillWoodruff23-Jul-13 2:41 
GeneralRe: Generic collections Pin
harold aptroot23-Jul-13 3:11
harold aptroot23-Jul-13 3:11 
GeneralRe: Generic collections Pin
BillWoodruff23-Jul-13 4:09
professionalBillWoodruff23-Jul-13 4:09 
GeneralRe: Generic collections Pin
harold aptroot23-Jul-13 5:32
harold aptroot23-Jul-13 5:32 
GeneralRe: Generic collections Pin
BillWoodruff23-Jul-13 18:11
professionalBillWoodruff23-Jul-13 18:11 
GeneralRe: Generic collections Pin
harold aptroot23-Jul-13 22:42
harold aptroot23-Jul-13 22:42 
GeneralRe: Generic collections Pin
harold aptroot21-Jul-13 1:18
harold aptroot21-Jul-13 1:18 
GeneralRe: Generic collections Pin
PozzaVecia21-Jul-13 22:56
PozzaVecia21-Jul-13 22:56 
GeneralRe: Generic collections Pin
BillWoodruff21-Jul-13 23:40
professionalBillWoodruff21-Jul-13 23:40 
GeneralRe: Generic collections Pin
PozzaVecia22-Jul-13 1:18
PozzaVecia22-Jul-13 1:18 
AnswerRe: Generic collections Pin
M.Kamran Asim21-Jul-13 21:19
M.Kamran Asim21-Jul-13 21:19 
QuestionDisable Windows Navigation Pin
aymen Tn18-Jul-13 5:11
aymen Tn18-Jul-13 5:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.