5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Collections;
29
using System.Collections.Generic;
30
using System.Collections.ObjectModel;
31
using System.Threading;
34
using MonoDevelop.Projects;
35
using MonoDevelop.Components;
36
using MonoDevelop.Core;
37
using MonoDevelop.Core.Instrumentation;
38
using MonoDevelop.Ide.Gui;
39
using MonoDevelop.Ide.CodeCompletion;
40
using MonoDevelop.Components.MainToolbar;
41
using ICSharpCode.NRefactory.TypeSystem;
43
namespace MonoDevelop.Components.MainToolbar
45
class ResultsDataSource: List<SearchResult>, ISearchDataSource
49
SearchResult bestResult;
50
int bestRank = int.MinValue;
52
public ResultsDataSource (Gtk.Widget widget)
57
public void SortUpToN (MonoDevelop.Components.MainToolbar.SearchCategory.DataItemComparer comparison, int n)
59
// use built-in sorting for small lists since the fast algorithm is only correct for lists larger than n * 2
61
this.Sort (comparison);
65
// build binary heap from all items
66
for (int i = 0; i < Count; i++) {
68
var item = this [offset + i]; // use next item
70
// and move it on top, if greater than parent
72
comparison.Compare (this[offset + (index - 1) / 2], item) > 0) {
73
int top = (index - 1) / 2;
74
this [offset + index] = this [offset + top];
77
this [offset + index] = item;
80
var bound = Math.Max (0, Count - 1 - n);
81
for (int i = Count - 1; i > bound; i--) {
82
// delete max and place it as last
83
var last = this [offset + i];
84
this [offset + i] = this [offset];
87
// the last one positioned in the heap
88
while (index * 2 + 1 < i) {
89
int left = index * 2 + 1, right = left + 1;
91
if (right < i && comparison.Compare (this [offset + left], this [offset + right]) > 0) {
92
if (comparison.Compare (last, this [offset + right]) < 0)
95
this [offset + index] = this [offset + right];
98
if (comparison.Compare (last, this [offset + left]) < 0)
101
this [offset + index] = this [offset + left];
105
this [offset + index] = last;
108
// switch the lasts elements with the first ones (the last n elements are sorted)
109
for (int i = 0; i < n; i++) {
110
var tmp = this [Count - 1 - i];
111
this [Count - 1 - i] = this [i];
116
#region ISearchDataSource implementation
118
Gdk.Pixbuf ISearchDataSource.GetIcon (int item)
120
return this [item].Icon;
123
string ISearchDataSource.GetMarkup (int item, bool isSelected)
126
return GLib.Markup.EscapeText (this [item].PlainText);
127
return this [item].GetMarkupText (widget);
130
string ISearchDataSource.GetDescriptionMarkup (int item, bool isSelected)
133
return GLib.Markup.EscapeText (this [item].Description);
134
return this [item].GetDescriptionMarkupText (widget);
137
ICSharpCode.NRefactory.TypeSystem.DomRegion ISearchDataSource.GetRegion (int item)
139
var result = this [item];
140
return new DomRegion (result.File, result.Row, result.Column, result.Row, result.Column);
143
bool ISearchDataSource.CanActivate (int item)
145
var result = this [item];
146
return result.CanActivate;
149
void ISearchDataSource.Activate (int item)
151
var result = this [item];
155
double ISearchDataSource.GetWeight (int item)
157
return this [item].Rank;
160
int ISearchDataSource.ItemCount {
166
TooltipInformation ISearchDataSource.GetTooltip (int item)
168
return this [item].TooltipInformation;
172
public SearchResult BestResult {
178
public void AddResult (SearchResult res)
182
if (res.Rank > bestRank) {