IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
SortedTable.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2009-2012 Jeroen Frijters
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
19
20 Jeroen Frijters
21 jeroen@frijters.net
22
23*/
24using System;
25using System.Collections.Generic;
26
28{
29
30 abstract class SortedTable<T> : Table<T>
31 where T : SortedTable<T>.IRecord
32 {
33
34 internal interface IRecord : IComparable<T>
35 {
36
37 int FilterKey { get; }
38
39 }
40
41 internal struct Enumerable
42 {
43
44 readonly SortedTable<T> table;
45 readonly int token;
46
52 internal Enumerable(SortedTable<T> table, int token)
53 {
54 this.table = table;
55 this.token = token;
56 }
57
58 public Enumerator GetEnumerator()
59 {
60 var records = table.records;
61 if (table.Sorted == false)
62 return new Enumerator(records, table.RowCount - 1, -1, token);
63
64 int index = BinarySearch(records, table.RowCount, token & 0xFFFFFF);
65 if (index < 0)
66 return new Enumerator(null, 0, 1, -1);
67
68 int start = index;
69 while (start > 0 && (records[start - 1].FilterKey & 0xFFFFFF) == (token & 0xFFFFFF))
70 start--;
71
72 int end = index;
73 int max = table.RowCount - 1;
74 while (end < max && (records[end + 1].FilterKey & 0xFFFFFF) == (token & 0xFFFFFF))
75 end++;
76
77 return new Enumerator(records, end, start - 1, token);
78 }
79
80 static int BinarySearch(T[] records, int length, int maskedToken)
81 {
82 int min = 0;
83 int max = length - 1;
84 while (min <= max)
85 {
86 int mid = min + ((max - min) / 2);
87 int maskedValue = records[mid].FilterKey & 0xFFFFFF;
88 if (maskedToken == maskedValue)
89 return mid;
90 else if (maskedToken < maskedValue)
91 max = mid - 1;
92 else
93 min = mid + 1;
94 }
95
96 return -1;
97 }
98 }
99
100 internal struct Enumerator
101 {
102
103 readonly T[] records;
104 readonly int token;
105 readonly int max;
106 int index;
107
115 internal Enumerator(T[] records, int max, int index, int token)
116 {
117 this.records = records;
118 this.token = token;
119 this.max = max;
120 this.index = index;
121 }
122
123 public readonly int Current => index;
124
125 public bool MoveNext()
126 {
127 while (index < max)
128 {
129 index++;
130 if (records[index].FilterKey == token)
131 return true;
132 }
133
134 return false;
135 }
136 }
137
138 internal Enumerable Filter(int token)
139 {
140 return new Enumerable(this, token);
141 }
142
146 protected void Sort()
147 {
148 Array.Sort(records, 0, rowCount, Comparer<T>.Default);
149 }
150
151 }
152
153}
void Sort()
Sorts the records in the table.