ok, did a bruit force type thing. would appreciate any comments on the code.
Seems to work:
using System;
using System.Collections.Generic;
using System.Text;
class ListSearch<T>
{
public ListSearch()
{
}
public int IndexOf(List<T> list, List<T> subList)
{
return IndexOf(list, subList, 0);
}
public int IndexOf(List<T> list, List<T> subList, int start)
{
T s = subList[0];
int i = list.FindIndex(start, delegate(T t) { return
t.Equals(s); });
if (i < 0)
{
// found no initial match
return -1;
}
for (int j = 1; j < subList.Count; j++)
{
if (!list[j + i].Equals(subList[j]))
{
// got here because we found a non equal value.
// if there are more elements in the list, start over at the
next element
if (j + i < list.Count)
{
return IndexOf(list, subList, i + 1);
}
else
{
return -1;
}
}
}
return i;
}
}
List<int> list = new List<int>();
List<int> subList = new List<int>();
list.Add(1); // 0
list.Add(2); // 1
list.Add(3); // 2
list.Add(4); // 3
list.Add(4); // 4
list.Add(5); // 5
subList.Add(4);
subList.Add(5);
ListSearch<int> listSearch = new ListSearch<int>();
int i = listSearch.IndexOf(list, subList);
returns 4 which is correct.
--
Andrew Robinson
blog.binaryocean.com
"Michael Bray" <mbray@makeDIntoDot_ctiusaDcom> wrote in message
news:Xn****************************@207.46.248.16. ..
"KJ" <n_**********@mail.com> wrote in news:1136932047.192507.134960
@f14g2000cwb.googlegroups.com:
This is an interesting post, and I wonder if anyone has implemented a
Set class yet using generics (and would like to share it).
Although it doesn't directly affect your question, in detail, he couldn't
use a set class because he is dependent on order, which is contrary to set
theory.
-mdb