19 using System.Collections.Generic;
21 namespace Deveel.Data.Index {
54 return new Block(512);
68 BaseArray =
new T[blockSize];
72 protected T[] BaseArray {
get; set; }
74 protected virtual int ArrayLength {
75 get {
return BaseArray.Length; }
82 IEnumerator IEnumerable.GetEnumerator() {
83 return GetEnumerator();
88 set { Next = (
Block) value; }
91 private Block Next {
get; set; }
94 get {
return Previous; }
95 set { Previous = (
Block) value; }
98 private Block Previous {
get; set; }
101 public bool HasChanged {
102 get {
return changed; }
107 get {
return count; }
108 protected set { count = value; }
113 get {
return count >= ArrayLength; }
117 public bool IsEmpty {
118 get {
return count <= 0; }
122 public virtual T Top {
123 get {
return GetArray(
true)[count - 1]; }
127 public virtual T Bottom {
130 throw new InvalidOperationException(
"no bottom value.");
132 return GetArray(
true)[0];
137 public T
this[
int index] {
138 get {
return GetArray(
true)[index]; }
141 GetArray(
false)[index] = value;
147 var newArray =
new T[BaseArray.Length];
148 Array.Copy(BaseArray, 0, newArray, 0, BaseArray.Length);
155 return x.CompareTo(y) <= 0;
159 return x.CompareTo(y) >= 0;
163 return x.CompareTo(y) > 0;
167 return x.CompareTo(y) < 0;
172 return count + number + 1 < ArrayLength;
176 public void Add(T value) {
178 var arr = GetArray(
false);
186 var arr = GetArray(
false);
187 var val = arr[index];
188 Array.Copy(BaseArray, index + 1, arr, index, (count - index));
195 var arr = GetArray(
true);
196 for (
int i = count - 1; i >= 0; --i) {
197 if (arr[i].Equals(value))
205 var arr = GetArray(
true);
206 for (
int i = startIndex; i < count; ++i) {
207 if (arr[i].Equals(value))
216 var arr = GetArray(
false);
217 Array.Copy(BaseArray, index, arr, index + 1, (count - index));
224 var block = (
Block) destBlock;
226 var arr = GetArray(
false);
227 var destArr = block.GetArray(
false);
230 int destbSize = block.Count;
232 Array.Copy(destArr, 0, destArr, length, destbSize);
236 Array.Copy(arr, count - length, destArr, 0, length);
238 block.count += length;
242 block.changed =
true;
247 var block = (
Block) destBlock;
248 var destArr = block.GetArray(
false);
249 Array.Copy(GetArray(
true), 0, destArr, 0, count);
251 block.changed =
true;
255 public int CopyTo(T[] destArray,
int arrayIndex) {
256 Array.Copy(GetArray(
true), 0, destArray, arrayIndex, count);
268 var arr = GetArray(
true);
270 int high = count - 1;
272 while (low <= high) {
273 int mid = (low + high)/2;
288 var arr = GetArray(
true);
290 int high = count - 1;
292 while (low <= high) {
293 if (high - low <= 2) {
294 for (
int i = low; i <= high; ++i) {
306 int mid = (low + high)/2;
311 }
else if (cmp > 0) {
323 var arr = GetArray(
true);
325 int high = count - 1;
327 while (low <= high) {
329 if (high - low <= 2) {
330 for (
int i = high; i >= low; --i) {
340 int mid = (low + high)/2;
345 }
else if (cmp > 0) {
356 var arr = GetArray(
true);
358 int high = count - 1;
360 while (low <= high) {
362 if (high - low <= 2) {
363 for (
int i = low; i <= high; ++i) {
364 if (arr[i].Equals(value))
366 if (IsGreater(arr[i], value))
372 int mid = (low + high)/2;
374 if (IsSmaller(arr[mid], value)) {
376 }
else if (IsGreater(arr[mid], value)) {
387 var arr = GetArray(
true);
389 int high = count - 1;
391 while (low <= high) {
393 if (high - low <= 2) {
394 for (
int i = high; i >= low; --i) {
395 if (arr[i].Equals(value))
397 if (IsSmaller(arr[i], value))
403 int mid = (low + high)/2;
405 if (IsSmaller(arr[mid], value)) {
407 }
else if (IsGreater(arr[mid], value)) {
433 return ++index < array.Length;
437 array = block.GetArray(
true);
442 get {
return array[index]; }
445 object IEnumerator.Current {
446 get {
return Current; }
static bool IsGreater(T x, T y)
int IndexOf(T value)
summary> Performs an iterative search from the given position to the end of the list in the block...
static bool IsGreaterOrEqual(T x, T y)
A comparer that is used within IIndex to compares two values which are indices to data that is bei...
int IndexOf(T value, int startIndex)
static bool IsSmallerOrEqual(T x, T y)
int SearchFirst(T value)
summary> Assuming a sorted block, finds the last index in the block that equals the given value...
IIndexBlock< T > Next
Gets or sets the next block in the hash.
void Clear()
Clears the block of all elements.
int CopyTo(T[] destArray, int arrayIndex)
BlockIndex()
Constructs an index with no values.
IEnumerator< T > GetEnumerator()
static bool IsSmaller(T x, T y)
void Add(T value)
Adss an int element to the block.
int SearchFirst(object key, IIndexComparer< T > comparer)
summary> Finds the last index in the block that equals the given key. /summary> param name="key"> ...
IIndexBlock< T > Previous
Gets or sets the previous block in the hash.
BlockIndex(IIndex< T > index)
override IIndexBlock< T > NewBlock()
Creates a new IIndexBlock for the given implementation.
virtual T[] GetArray(bool readOnly)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
int CompareValue(T index, DataObject value)
Compares a value contained in the underlying index at the given position and the
void MoveTo(IIndexBlock< T > destBlock, int destIndex, int length)
summary> Copies all the data from this block into the given destination block. /summary> param name="...
BlockIndex(IEnumerable< IIndexBlock< T >> blocks)
void CopyTo(IIndexBlock< T > destBlock)
summary> Copies all the data from this block into the given array. /summary> param name="array">The d...
A block contained in a BlockIndex.
int SearchLast(object key, IIndexComparer< T > comparer)
summary> Assuming a sorted block, finds the first index in the block that equals the given value...
bool CanContain(int number)
An implementation of an index of values that are stored across an array of blocks.
An implementation of BlockIndexBase that stores all values in blocks that are entirely stored in m...
T RemoveAt(int index)
Removes the element at the given index from the block.
BlockIndex(IEnumerable< T > values)
int BinarySearch(object key, IIndexComparer< T > comparer)
summary> Finds the first index in the block that equals the given key. /summary> param name="key"> ...
void Insert(T value, int index)
Inserts an element to the block at the given index.