使用由提問者提供的邏輯,這裏是提供ElementBefore
和ElementAfter
訪問任何SortedSet
使用擴展方法的方式:
public static class BeforeAfterExtension
{
//The true workhorse of the code!
public static T ElementBefore<T>(this SortedSet<T> mySet, T val, IComparer<T> comparer)
{
//we are getting the first element el such that it is before
//the provided value val. We must reverse the set because
//otherwise, the first element in the set is always the first
//to be less than the provided value.
return mySet.Reverse().FirstOrDefault(el => comparer.Compare(el, val) < 0);
}
//Contains all the actual logic
public static T ElementAfter<T>(this SortedSet<T> mySet, T val, IComparer<T> comparer)
{
//we are getting the first element el such that it is after
//the provided value val.
return mySet.FirstOrDefault(el => comparer.Compare(el, val) > 0);
}
//use the existing logic, but use the default comparer for the set
public static T ElementBefore<T>(this SortedSet<T> mySet, T val)
{
return ElementBefore(mySet, val, mySet.Comparer);
}
//use the existing logic, but use the default comparer for the set
public static T ElementAfter<T>(this SortedSet<T> mySet, T val)
{
return ElementAfter(mySet, val, mySet.Comparer);
}
//provide a condition that the element must already exist in the set
//Consider a set of strings containing only "apple" and "cat"
//without forcing the element to exist in the set, we could search for
//the element before "bear" and get "apple" even though "bear" is not
//in the set. Forcing the element to exist by setting mustExist=true
//would return null if the element is not there already
public static T ElementBefore<T>(this SortedSet<T> mySet, T val, bool mustExist, IComparer<T> comparer) {
{
if (mustExist)
{
if (mySet.Contains(val))
{
//take advantage of existing logic
return ElementBefore(mySet, val, comparer);
}
else
{
return null;
}
}
else
{
//take advantage of existing logic
return ElementBefore(mySet, val, comparer);
}
}
//provide a condition that the element must already exist in the set
//Consider a set of strings containing only "apple" and "cat"
//without forcing the element to exist in the set, we could search for
//the element after "bear" and get "cat" even though "bear" is not
//in the set. Forcing the element to exist by setting mustExist=true
//would return null if the element is not there already
public static T ElementBefore<T>(this SortedSet<T> mySet, T val, bool mustExist, IComparer<T> comparer) {
{
if (mustExist)
{
if (mySet.Contains(val))
{
//take advantage of existing logic
return ElementAfter(mySet, val, comparer);
}
else
{
return null;
}
}
else
{
//take advantage of existing logic
return ElementAfter(mySet, val, comparer);
}
}
//just use the default set comparer
public static T ElementBefore<T>(this SortedSet<T> mySet, T val, bool mustExist)
{
//take advantage of existing logic
return ElementBefore(mySet, val, mustExist, mySet.Comparer);
}
//just use the default set comparer
public static T ElementAfter<T>(this SortedSet<T> mySet, T val, bool mustExist)
{
//take advantage of existing logic
return ElementAfter(mySet, val, mustExist, mySet.Comparer);
}
}
有在那裏一些額外的方法,使所使用的比較器的定製,以及所需要的元件組中的存在。
現在所有需要的是
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
s = pastEntriesSS.ElementBefore(s);
}
else
{
s = pastEntriesSS.ElementAfter(s);
}
,這是可擴展到包含任何類型的對象集,不止是字符串
此外,這會根據輸入查找的元素不一定是在(例如,如果集合僅包含「蘋果」和「貓」,即使「熊」本身不在列表中,如果集合僅包含「蘋果」和「貓」,則「熊」的輸入將顯示「蘋果」和之後的「貓」),但這對搜索幫手來說不一定是壞事 – Sam
@Sam是的,我確實在想這件事,那就是所期望的行爲。在完整的代碼和回車鍵用於添加到列表並實際將值添加到數據庫。我考慮一個選項卡和項目不存在將項目添加到列表中,但不會將其添加到數據庫。例如,用戶有一個他們想要鍵入的可能值的列表。 – Paparazzi
@Sam認爲他們進入b,只想到達第一個b,但沒有理由列出名單上的b – Paparazzi