I implemented a search function like this:
private static void GetResults(ref ObservableCollection<string> resultTitles, ref string[] query, ref ObservableCollection<int> weight)
{
int position = -1;
foreach (string[] r in SearchKeys.Keys)
{
position++;
foreach (string s in query)
{
int m = r.Length/2;
int min = r[m][0] < s[0] ? m : 0;
int max = r[m][0] <= s[0] ? r.Length : m+1;
for (int i = min; i < max; i++)
{
weight.Add(0);
if (r[i] == s)
{
if (weight[position] == 0)
{
resultTitles.Add(r[0]);
}
weight[position]++;
} // end if
} // end foreach(s)
} // end foreach(t)
} // end foreach(r)
}
In the main program, the use inputs a query, the program splits the query at the spaces, and passes the query and two empty ObservableCollection
s to this function.
SearchKeys.Keys
is implemented like this:
public static string[][] Keys = { Array1, Array2, Array3 };
The arrays are implemented like this:
private static string[] Array1 = { "Title", "val1", "val2", "val3", "val4", "val5", "val6" };
As always, all comments are welcome; particularly, I am interested in which ways could this code's performance be improved. The data in SearchKeys
is sorted, of course.
This is a previous version of my code, which may also be of interest. This version should run about twice as slow as the above, according to my estimates:
private void getResults(ref ObservableCollection<string> tmp, ref string[] query, ref ObservableCollection<int> weight)
{
int position = -1;
foreach (string[] r in SearchKeys.Keys)
{
position++;
foreach (string t in r)
{
weight.Add(0);
foreach (string s in query)
{
if (t == s)
{
if (weight[position] == 0)
{
tmp.Add(r[0]);
}
weight[position]++;
} // end if
} // end foreach(s)
} // end foreach(t)
} // end foreach(r)
}
This is how I call this function:
static void Main(string[] args)
{
ObservableCollection<int> weight = new ObservableCollection<int>();
ObservableCollection<string> resultTitles = new ObservableCollection<string>();
string[] query = Console.ReadLine().Split(' ');
GetResults(ref resultTitles, ref query, ref weight);
StableSort(ref resultTitles, ref weight);
foreach (string s in resultTitles)
Console.WriteLine(resultTitles.IndexOf(s)+": "+s);
}
This is StableSort
(see Stable Sort in C#):
static void StableSort(ref ObservableCollection<string> values, ref ObservableCollection<int> weights)
{
while(weights.Contains(0))
{
weights.Remove(0);
}
if (values == null) { throw new ArgumentNullException("values"); }
if (weights == null) { throw new ArgumentNullException("weights"); }
if (values.Count != weights.Count) { throw new ArgumentOutOfRangeException("collections count not equal", (Exception)null); }
ObservableCollection<string> localValues = new ObservableCollection<string>();
ObservableCollection<int> localWeights = new ObservableCollection<int>();
int index = -1;
var weightsWithIndex = weights.Select(p => new { Value = p, Index = ++index }).OrderByDescending(p => p.Value);
foreach (var w in weightsWithIndex)
{
localWeights.Add(w.Value);
localValues.Add(values[w.Index]);
}
values = localValues;
weights = localWeights;
}