cvsimport
[beagle.git] / search / SortedTileList.cs
blob9f50c215eb9667570c26d040761eba6ccb05b457
1 using System;
2 using System.Collections;
4 namespace Search {
6 public class SortedTileList : IEnumerable, ICloneable {
8 ArrayList tiles;
10 public SortedTileList (Search.SortType sort) : this (sort, new ArrayList ()) {}
12 SortedTileList (Search.SortType sort, ArrayList tiles)
14 this.tiles = tiles;
15 Sort = sort;
18 public int Add (Tiles.Tile tile)
20 int index = tiles.BinarySearch (tile, comparer);
21 if (index >= 0)
22 throw new ArgumentException ("duplicate");
24 tiles.Insert (~index, tile);
25 return ~index;
28 public void Clear ()
30 tiles.Clear ();
33 public bool Contains (Tiles.Tile tile)
35 return tiles.Contains (tile);
38 public int IndexOf (Tiles.Tile tile)
40 return tiles.IndexOf (tile);
43 public void Remove (Tiles.Tile tile)
45 int index = tiles.BinarySearch (tile, comparer);
46 if (index >= 0)
47 tiles.RemoveAt (index);
50 public void RemoveAt (int index)
52 tiles.RemoveAt (index);
55 public Tiles.Tile this[int index] {
56 get {
57 return (Tiles.Tile)tiles[index];
61 public int Count {
62 get {
63 return tiles.Count;
67 public IEnumerator GetEnumerator ()
69 return tiles.GetEnumerator ();
72 public object Clone ()
74 return new SortedTileList (sort, (ArrayList)tiles.Clone ());
77 public IList GetRange (int index, int count)
79 return tiles.GetRange (index, count);
82 Search.SortType sort;
83 IComparer comparer;
85 public Search.SortType Sort {
86 get {
87 return sort;
89 set {
90 sort = value;
91 switch (sort) {
92 case SortType.Relevance:
93 default:
94 comparer = new RelevanceComparer ();
95 break;
96 case SortType.Name:
97 comparer = new NameComparer ();
98 break;
99 case SortType.Modified:
100 comparer = new DateComparer ();
101 break;
104 tiles.Sort (comparer);
109 abstract class TileComparer : IComparer {
110 public int Compare (object x, object y)
112 Tiles.Tile tx = (Tiles.Tile)x, ty = (Tiles.Tile)y;
113 int ret;
115 ret = Compare (tx, ty);
116 if (ret == 0)
117 ret = -tx.Timestamp.CompareTo (ty.Timestamp);
118 if (ret == 0)
119 ret = tx.GetHashCode ().CompareTo (ty.GetHashCode ());
120 return ret;
123 public abstract int Compare (Tiles.Tile tx, Tiles.Tile ty);
126 class RelevanceComparer : TileComparer {
127 public override int Compare (Tiles.Tile tx, Tiles.Tile ty)
129 return -tx.Score.CompareTo (ty.Score);
133 class NameComparer : TileComparer {
134 public override int Compare (Tiles.Tile tx, Tiles.Tile ty)
136 return String.Compare (tx.Title, ty.Title, true);
140 class DateComparer : TileComparer {
141 public override int Compare (Tiles.Tile tx, Tiles.Tile ty)
143 return -tx.Timestamp.CompareTo (ty.Timestamp);