~ubuntu-branches/ubuntu/jaunty/beagle/jaunty-security

« back to all changes in this revision

Viewing changes to search/Beagle.Search/SortedTileList.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-05-04 00:31:32 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20080504003132-2tkm5o8moo5952ri
Tags: 0.3.7-2ubuntu1
 * Merge from Debian unstable. (LP: #225746) Remaining Ubuntu changes:
  - debian/control:
    + Rename ice{weasel,dove}-beagle to {mozilla,thunderbird}-beagle and
      and update the dependencies accordingly.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// SortedTileList.cs
 
3
//
 
4
// Copyright (C) 2008 Lukas Lipka <lukaslipka@gmail.com>
 
5
//
 
6
 
 
7
using System;
 
8
using System.Collections;
 
9
using System.Collections.Generic;
 
10
 
 
11
using Beagle.Search.Tiles;
 
12
 
 
13
namespace Beagle.Search {
 
14
 
 
15
        public class SortedTileList : IEnumerable<Tile> {
 
16
 
 
17
                private List<Tile> tiles = null;
 
18
 
 
19
                private SortType sort_type;
 
20
                private TileComparer comparer = null;
 
21
 
 
22
                public SortedTileList (SortType sort)
 
23
                {
 
24
                        this.tiles = new List<Tile> ();
 
25
                        this.SortType = sort;
 
26
                }
 
27
 
 
28
                public SortedTileList (SortType sort, Tile[] tiles_array)
 
29
                {
 
30
                        this.tiles = new List<Tile> (tiles_array);
 
31
                        this.SortType = sort;
 
32
                }
 
33
 
 
34
                public int Add (Tile tile)
 
35
                {
 
36
                        int index = tiles.BinarySearch (tile, comparer);
 
37
 
 
38
                        if (index >= 0)
 
39
                                throw new ArgumentException ("duplicate");
 
40
 
 
41
                        tiles.Insert (~index, tile);
 
42
 
 
43
                        return ~index;
 
44
                }
 
45
 
 
46
                public void Clear ()
 
47
                {
 
48
                        tiles.Clear ();
 
49
                }
 
50
 
 
51
                public bool Contains (Tile tile)
 
52
                {
 
53
                        return tiles.Contains (tile);
 
54
                }
 
55
 
 
56
                public int IndexOf (Tile tile)
 
57
                {
 
58
                        return tiles.IndexOf (tile);
 
59
                }
 
60
 
 
61
                public void Remove (Tile tile)
 
62
                {
 
63
                        int index = tiles.BinarySearch (tile, comparer);
 
64
                        
 
65
                        if (index >= 0)
 
66
                                tiles.RemoveAt (index);
 
67
                }
 
68
 
 
69
                public void RemoveAt (int index)
 
70
                {
 
71
                        tiles.RemoveAt (index);
 
72
                }
 
73
 
 
74
                public Tile this [int index] {
 
75
                        get { return tiles[index]; }
 
76
                }
 
77
 
 
78
                public int Count {
 
79
                        get { return tiles.Count; }
 
80
                }
 
81
 
 
82
                public IEnumerator<Tile> GetEnumerator ()
 
83
                {
 
84
                        return tiles.GetEnumerator ();
 
85
                }
 
86
 
 
87
 
 
88
                IEnumerator IEnumerable.GetEnumerator() {
 
89
                        return GetEnumerator();
 
90
                }
 
91
                
 
92
                public object Clone ()
 
93
                {
 
94
                        return new SortedTileList (sort_type, tiles.ToArray ());
 
95
                }
 
96
 
 
97
                public IList<Tile> GetRange (int index, int count)
 
98
                {
 
99
                        return tiles.GetRange (index, count);
 
100
                }
 
101
 
 
102
                public SortType SortType {
 
103
                        get { return sort_type; }
 
104
                        set {
 
105
                                sort_type = value;
 
106
 
 
107
                                switch (sort_type) {
 
108
                                        case SortType.Relevance:
 
109
                                        default:
 
110
                                                comparer = new RelevanceComparer ();
 
111
                                        break;
 
112
                                        case SortType.Name:
 
113
                                                comparer = new NameComparer ();
 
114
                                        break;
 
115
                                        case SortType.Modified:
 
116
                                                comparer = new DateComparer ();
 
117
                                        break;
 
118
                                }
 
119
 
 
120
                                tiles.Sort (comparer);
 
121
                        }
 
122
                }
 
123
        
 
124
                private abstract class TileComparer : IComparer<Tile> {
 
125
                        
 
126
                        public int Compare (Tile x, Tile y)
 
127
                        {
 
128
                                int ret = TileCompare (x, y);
 
129
                                
 
130
                                if (ret == 0)
 
131
                                        ret = -x.Timestamp.CompareTo (y.Timestamp);
 
132
                                
 
133
                                if (ret == 0)
 
134
                                        ret = x.GetHashCode ().CompareTo (y.GetHashCode ());
 
135
                                
 
136
                                return ret;
 
137
                        }
 
138
                        
 
139
                        public abstract int TileCompare (Tile x, Tile y);
 
140
                }
 
141
                
 
142
                private class RelevanceComparer : TileComparer {
 
143
                        
 
144
                        public override int TileCompare (Tile x, Tile y)
 
145
                        {
 
146
                                return -x.Score.CompareTo (y.Score);
 
147
                        }
 
148
                }
 
149
                
 
150
                private class NameComparer : TileComparer {
 
151
                        
 
152
                        public override int TileCompare (Tile x, Tile y)
 
153
                        {
 
154
                                return String.Compare (x.Title, y.Title, true);
 
155
                        }
 
156
                }
 
157
                
 
158
                private class DateComparer : TileComparer {
 
159
                        
 
160
                        public override int TileCompare (Tile x, Tile y)
 
161
                        {
 
162
                                return -x.Timestamp.CompareTo (y.Timestamp);
 
163
                        }
 
164
                }
 
165
        }
 
166
}
 
167