DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Classes | Public Member Functions | Protected Member Functions | Package Functions | Properties | Private Member Functions | Static Private Member Functions | List of all members
Deveel.Data.Index.BlockIndexBase< T > Class Template Referenceabstract

An implementation of an index of values that are stored across an array of blocks. More...

Inheritance diagram for Deveel.Data.Index.BlockIndexBase< T >:
Deveel.Data.Index.IIndex< T > Deveel.Data.Index.BlockIndex< T >

Classes

class  Enumerator
 

Public Member Functions

void Insert (int index, T value)
 
void Add (T value)
 
RemoveAt (int index)
 
bool Contains (T value)
 
void InsertSort (T value)
 
bool UniqueInsertSort (T value)
 
bool RemoveSort (T value)
 
bool Contains (object key, IIndexComparer< T > comparer)
 
void InsertSort (object key, T value, IIndexComparer< T > comparer)
 
RemoveSort (object key, T value, IIndexComparer< T > comparer)
 
int SearchLast (object key, IIndexComparer< T > comparer)
 
int SearchFirst (object key, IIndexComparer< T > comparer)
 
IIndexEnumerator< T > GetEnumerator ()
 
IIndexEnumerator< T > GetEnumerator (int startOffset, int endOffset)
 

Protected Member Functions

 BlockIndexBase ()
 
 BlockIndexBase (IEnumerable< IIndexBlock< T >> blocks)
 
 BlockIndexBase (IEnumerable< T > values)
 
 BlockIndexBase (IIndex< T > index)
 
abstract IIndexBlock< T > NewBlock ()
 Creates a new IIndexBlock<T> for the given implementation. More...
 
virtual void OnDeleteBlock (IIndexBlock< T > block)
 Called when the class decides the given IIndexBlock<T> is no longer needed. More...
 

Package Functions

void CopyToArray (T[] array, int offset, int length)
 Copies the data from each block into the given int[] array. More...
 

Properties

List< IIndexBlock< T > > Blocks [get, private set]
 
bool IsReadOnly [get, set]
 
int Count [get, private set]
 
this[int index] [get]
 

Private Member Functions

IIndexBlock< T > InsertBlock (int index, IIndexBlock< T > block)
 Inserts a block at the given position in the index. More...
 
void RemoveBlock (int index)
 Removes a block from the given position in the index. More...
 
void InsertIntoBlock (T value, int blockIndex, IIndexBlock< T > block, int position)
 Inserts a value in the given block position in the list. More...
 
RemoveFromBlock (int blockIndex, IIndexBlock< T > block, int position)
 Removes the value from the given position in the specified block. More...
 
int FindBlockContaining (object key, IIndexComparer< T > comparer)
 Uses a binary search algorithm to quickly determine the index of the IIndexBlock<T> within 'blocks' of the block that contains the given key value using the IIndexComparer<T> as a lookup comparator. More...
 
int FindLastBlock (object key, IIndexComparer< T > comparer)
 Uses a binary search algorithm to quickly determine the index of the IIndexBlock<T> within 'blocks' of the block that contains the given key value using the IIndexComparer<T> as a lookup comparator. More...
 
int FindFirstBlock (object key, IIndexComparer< T > c)
 Uses a binary search algorithm to quickly determine the index of the IIndexBlock<T> within 'blocks' of the block that contains the given key value using the IIndexComparer<T> as a lookup comparator. More...
 
int FindLastBlock (T val)
 Uses a binary search algorithm to quickly determine the index of the IIndexBlock<T> within 'blocks' of the block that contains the given value. More...
 
void CheckImmutable ()
 Checks if the current index is mutable. More...
 
IEnumerator< T > IEnumerable< T >. GetEnumerator ()
 
IEnumerator IEnumerable. GetEnumerator ()
 

Static Private Member Functions

static bool IsSmallerOrEqual (T x, T y)
 
static bool IsGreaterOrEqual (T x, T y)
 
static bool IsGreater (T x, T y)
 
static bool IsSmaller (T x, T y)
 

Detailed Description

An implementation of an index of values that are stored across an array of blocks.

This allows for quicker insertion and deletion of values, including other memory saving benefits.

The class works as follows:

The benefits of this system are that inserts and deletes are fast even for very large lists. There are no megabyte sized arraycopies. Also, the object could be extended to a version that pages un-used blocks to disk thus saving precious system memory.

Note: The following methods are not optimal: Item(int), Insert(int, T), RemoveAt(int).
Specifically, they slow as the specified index nears the end of large lists.

This type of structure is very fast for large sorted lists where values can be inserted at any position within the list. Care needs to be taken for lists where values are inserted and removed constantly, because fragmentation of the list blocks can occur. For example, adding 60,000 random entries followed by removing 50,000 random entries will result in many only partially filled blocks. Since each block takes a constant amount of memory, this may not be acceptable.

Type Constraints
T :IComparable<T> 
T :IEquatable<T> 

Definition at line 63 of file BlockIndexBase_T.cs.

Constructor & Destructor Documentation

Definition at line 64 of file BlockIndexBase_T.cs.

64  {
65  Count = 0;
66  IsReadOnly = false;
67  Blocks = new List<IIndexBlock<T>>(10);
68  }
List< IIndexBlock< T > > Blocks
Deveel.Data.Index.BlockIndexBase< T >.BlockIndexBase ( IEnumerable< IIndexBlock< T >>  blocks)
inlineprotected

Definition at line 70 of file BlockIndexBase_T.cs.

71  : this() {
72  foreach (var block in blocks) {
73  Blocks.Add(block);
74  Count += block.Count;
75  }
76  }
List< IIndexBlock< T > > Blocks
Deveel.Data.Index.BlockIndexBase< T >.BlockIndexBase ( IEnumerable< T >  values)
inlineprotected

Definition at line 78 of file BlockIndexBase_T.cs.

79  : this() {
80  foreach (var value in values) {
81  Add(value);
82  }
83  }
Deveel.Data.Index.BlockIndexBase< T >.BlockIndexBase ( IIndex< T >  index)
inlineprotected

Definition at line 85 of file BlockIndexBase_T.cs.

86  : this() {
87  if (index is BlockIndexBase<T>) {
88  // Optimization for when the input list is a BlockIntegerList
89  var blockIndex = (BlockIndexBase<T>) index;
90 
91  var inBlocks = blockIndex.Blocks;
92  int inBlocksCount = inBlocks.Count;
93  // For each block in 'blockIndex'
94  for (int i = 0; i < inBlocksCount; ++i) {
95  // get the block.
96  var block = inBlocks[i];
97  // Insert a new block in this object.
98  var destBlock = InsertBlock(i, NewBlock());
99  // Copy the contents of the source block to the new destination block.
100  block.CopyTo(destBlock);
101  }
102 
103  // Set the size of the list
104  Count = blockIndex.Count; //count;
105  } else {
106  // The case when IIntegerList type is not known
107  var i = index.GetEnumerator();
108  while (i.MoveNext()) {
109  Add(i.Current);
110  }
111  }
112 
113  // If the given list is immutable then set this list to immutable
114  if (index.IsReadOnly)
115  IsReadOnly = true;
116  }
abstract IIndexBlock< T > NewBlock()
Creates a new IIndexBlock for the given implementation.
IIndexBlock< T > InsertBlock(int index, IIndexBlock< T > block)
Inserts a block at the given position in the index.

Member Function Documentation

void Deveel.Data.Index.BlockIndexBase< T >.Add ( value)
inline

Definition at line 547 of file BlockIndexBase_T.cs.

547  {
548  CheckImmutable();
549 
550  int size = Blocks.Count;
551  var block = Blocks[size - 1];
552  InsertIntoBlock(value, size - 1, block, block.Count);
553  }
void InsertIntoBlock(T value, int blockIndex, IIndexBlock< T > block, int position)
Inserts a value in the given block position in the list.
List< IIndexBlock< T > > Blocks
void CheckImmutable()
Checks if the current index is mutable.
void Deveel.Data.Index.BlockIndexBase< T >.CheckImmutable ( )
inlineprivate

Checks if the current index is mutable.

This is called before any mutable operations on the index: if the index is mutable and empty then an empty block is added to the index.

Exceptions
InvalidOperationExceptionThrown if the index is read-only.

Definition at line 499 of file BlockIndexBase_T.cs.

499  {
500  if (IsReadOnly)
501  throw new InvalidOperationException("Index is read-only.");
502 
503  // HACK: We have a side effect of checking whether the list is immutable.
504  // If the block list doesn't contain any entries we add one here. This
505  // hack reduces the memory requirements.
506  if (Blocks.Count == 0) {
507  InsertBlock(0, NewBlock());
508  }
509  }
abstract IIndexBlock< T > NewBlock()
Creates a new IIndexBlock for the given implementation.
List< IIndexBlock< T > > Blocks
IIndexBlock< T > InsertBlock(int index, IIndexBlock< T > block)
Inserts a block at the given position in the index.
bool Deveel.Data.Index.BlockIndexBase< T >.Contains ( value)
inline

Definition at line 572 of file BlockIndexBase_T.cs.

572  {
573  int blockIndex = FindLastBlock(value);
574 
575  if (blockIndex < 0)
576  // We didn't find in the list, so return false.
577  return false;
578 
579  // We got a block, so find out if it's in the block or not.
580  var block = Blocks[blockIndex];
581 
582  // Find, if not there then return false.
583  return block.SearchLast(value) >= 0;
584  }
List< IIndexBlock< T > > Blocks
int FindLastBlock(object key, IIndexComparer< T > comparer)
Uses a binary search algorithm to quickly determine the index of the IIndexBlock within 'blocks' o...
bool Deveel.Data.Index.BlockIndexBase< T >.Contains ( object  key,
IIndexComparer< T >  comparer 
)
inline

Definition at line 684 of file BlockIndexBase_T.cs.

684  {
685  int blockIndex = FindBlockContaining(key, comparer);
686 
687  if (blockIndex < 0)
688  // We didn't find in the list, so return false.
689  return false;
690 
691  // We got a block, so find out if it's in the block or not.
692  var block = Blocks[blockIndex];
693 
694  // Find, if not there then return false.
695  return block.BinarySearch(key, comparer) >= 0;
696  }
int FindBlockContaining(object key, IIndexComparer< T > comparer)
Uses a binary search algorithm to quickly determine the index of the IIndexBlock within 'blocks' o...
List< IIndexBlock< T > > Blocks
void Deveel.Data.Index.BlockIndexBase< T >.CopyToArray ( T[]  array,
int  offset,
int  length 
)
inlinepackage

Copies the data from each block into the given int[] array.

Parameters
array
offset
length

The int[] array must be big enough to fit all the data in this list.

Definition at line 520 of file BlockIndexBase_T.cs.

520  {
521  if (array.Length < length || (offset + length) > Count)
522  throw new InvalidOperationException("Size mismatch.");
523 
524  foreach (var block in Blocks) {
525  offset += block.CopyTo(array, offset);
526  }
527  }
List< IIndexBlock< T > > Blocks
int Deveel.Data.Index.BlockIndexBase< T >.FindBlockContaining ( object  key,
IIndexComparer< T >  comparer 
)
inlineprivate

Uses a binary search algorithm to quickly determine the index of the IIndexBlock<T> within 'blocks' of the block that contains the given key value using the IIndexComparer<T> as a lookup comparator.

Parameters
key
comparer
Returns

Definition at line 290 of file BlockIndexBase_T.cs.

290  {
291  if (Count == 0)
292  return -1;
293 
294  int low = 0;
295  int high = Blocks.Count - 1;
296 
297  while (low <= high) {
298  int mid = (low + high)/2;
299  var block = Blocks[mid];
300 
301  // Is what we are searching for lower than the bottom value?
302  if (comparer.CompareValue(block.Bottom, (DataObject) key) > 0) {
303  high = mid - 1;
304  }
305  // No, then is it greater than the highest value?
306  else if (comparer.CompareValue(block.Top, (DataObject) key) < 0) {
307  low = mid + 1;
308  }
309  // Must be inside this block then!
310  else {
311  return mid;
312  }
313  }
314 
315  return -(low + 1); // key not found.
316  }
List< IIndexBlock< T > > Blocks
int Deveel.Data.Index.BlockIndexBase< T >.FindFirstBlock ( object  key,
IIndexComparer< T >  c 
)
inlineprivate

Uses a binary search algorithm to quickly determine the index of the IIndexBlock<T> within 'blocks' of the block that contains the given key value using the IIndexComparer<T> as a lookup comparator.

Parameters
key
c
Returns

Definition at line 379 of file BlockIndexBase_T.cs.

379  {
380  if (Count == 0) {
381  return -1;
382  }
383 
384  int low = 0;
385  int high = Blocks.Count - 1;
386 
387  while (low <= high) {
388 
389  if (high - low <= 2) {
390  for (int i = low; i <= high; ++i) {
391  var block1 = Blocks[i];
392  if (c.CompareValue(block1.Top, (DataObject) key) >= 0) {
393  if (c.CompareValue(block1.Bottom, (DataObject) key) <= 0)
394  return i;
395  return -(i + 1);
396  }
397  }
398  return -(high + 1) - 1;
399  }
400 
401  int mid = (low + high)/2;
402  var block = Blocks[mid];
403 
404  // Is what we are searching for lower than the bottom value?
405  if (c.CompareValue(block.Bottom, (DataObject) key) > 0) {
406  high = mid - 1;
407  }
408  // No, then is it greater than the highest value?
409  else if (c.CompareValue(block.Top, (DataObject) key) < 0) {
410  low = mid + 1;
411  }
412  // Equal, so highest must be someplace between mid and high.
413  else {
414  high = mid;
415  }
416  }
417 
418  return -(low + 1); // key not found.
419  }
List< IIndexBlock< T > > Blocks
int Deveel.Data.Index.BlockIndexBase< T >.FindLastBlock ( object  key,
IIndexComparer< T >  comparer 
)
inlineprivate

Uses a binary search algorithm to quickly determine the index of the IIndexBlock<T> within 'blocks' of the block that contains the given key value using the IIndexComparer<T> as a lookup comparator.

Parameters
key
comparer
Returns

Definition at line 327 of file BlockIndexBase_T.cs.

327  {
328  if (Count == 0)
329  return -1;
330 
331  int low = 0;
332  int high = Blocks.Count - 1;
333 
334  while (low <= high) {
335  if (high - low <= 2) {
336  for (int i = high; i >= low; --i) {
337  var block1 = Blocks[i];
338  if (comparer.CompareValue(block1.Bottom, (DataObject) key) <= 0) {
339  if (comparer.CompareValue(block1.Top, (DataObject) key) >= 0)
340  return i;
341  return -(i + 1) - 1;
342  }
343  }
344  return -(low + 1);
345  }
346 
347  int mid = (low + high)/2;
348  var block = Blocks[mid];
349 
350  // Is what we are searching for lower than the bottom value?
351  if (comparer.CompareValue(block.Bottom, (DataObject) key) > 0) {
352  high = mid - 1;
353  }
354  // No, then is it greater than the highest value?
355  else if (comparer.CompareValue(block.Top, (DataObject) key) < 0) {
356  low = mid + 1;
357  }
358  // Equal, so highest must be someplace between mid and high.
359  else {
360  low = mid;
361  if (low == high) {
362  return low;
363  }
364  }
365  }
366 
367  return -(low + 1); // key not found.
368  }
List< IIndexBlock< T > > Blocks
int Deveel.Data.Index.BlockIndexBase< T >.FindLastBlock ( val)
inlineprivate

Uses a binary search algorithm to quickly determine the index of the IIndexBlock<T> within 'blocks' of the block that contains the given value.

Parameters
val
Returns

Definition at line 444 of file BlockIndexBase_T.cs.

444  {
445  if (Count == 0) {
446  return -1;
447  }
448 
449  int low = 0;
450  int high = Blocks.Count - 1;
451 
452  while (low <= high) {
453 
454  if (high - low <= 2) {
455  for (int i = high; i >= low; --i) {
456  var block1 = Blocks[i];
457  if (IsSmallerOrEqual(block1.Bottom, val)) {
458  if (IsGreaterOrEqual(block1.Top, val))
459  return i;
460  return -(i + 1) - 1;
461  }
462  }
463  return -(low + 1);
464  }
465 
466  int mid = (low + high)/2;
467  var block = Blocks[mid];
468 
469  // Is what we are searching for lower than the bottom value?
470  if (IsGreater(block.Bottom,val)) {
471  high = mid - 1;
472  }
473  // No, then is it greater than the highest value?
474  else if (IsSmaller(block.Top, val)) {
475  low = mid + 1;
476  }
477  // Equal, so highest must be someplace between mid and high.
478  else {
479  low = mid;
480  if (low == high) {
481  return low;
482  }
483  }
484  }
485 
486  return -(low + 1); // key not found.
487  }
static bool IsGreaterOrEqual(T x, T y)
static bool IsSmallerOrEqual(T x, T y)
List< IIndexBlock< T > > Blocks
IIndexEnumerator<T> Deveel.Data.Index.BlockIndexBase< T >.GetEnumerator ( )
inline

Definition at line 808 of file BlockIndexBase_T.cs.

808  {
809  return GetEnumerator(0, Count - 1);
810  }
IIndexEnumerator< T > GetEnumerator()
IIndexEnumerator<T> Deveel.Data.Index.BlockIndexBase< T >.GetEnumerator ( int  startOffset,
int  endOffset 
)
inline

Definition at line 812 of file BlockIndexBase_T.cs.

812  {
813  return new Enumerator(this, startOffset, endOffset);
814  }
IEnumerator<T> IEnumerable<T>. Deveel.Data.Index.BlockIndexBase< T >.GetEnumerator ( )
inlineprivate

Definition at line 816 of file BlockIndexBase_T.cs.

816  {
817  return GetEnumerator();
818  }
IIndexEnumerator< T > GetEnumerator()
IEnumerator IEnumerable. Deveel.Data.Index.BlockIndexBase< T >.GetEnumerator ( )
inlineprivate

Definition at line 820 of file BlockIndexBase_T.cs.

820  {
821  return GetEnumerator();
822  }
IIndexEnumerator< T > GetEnumerator()
void Deveel.Data.Index.BlockIndexBase< T >.Insert ( int  index,
value 
)
inline

Definition at line 529 of file BlockIndexBase_T.cs.

529  {
530  CheckImmutable();
531 
532  int size = Blocks.Count;
533  int start = 0;
534  for (int i = 0; i < size; ++i) {
535  var block = Blocks[i];
536  int bsize = block.Count;
537  if (index >= start && index <= start + bsize) {
538  InsertIntoBlock(value, i, block, index - start);
539  return;
540  }
541  start += bsize;
542  }
543 
544  throw new ArgumentOutOfRangeException("index");
545  }
void InsertIntoBlock(T value, int blockIndex, IIndexBlock< T > block, int position)
Inserts a value in the given block position in the list.
List< IIndexBlock< T > > Blocks
void CheckImmutable()
Checks if the current index is mutable.
IIndexBlock<T> Deveel.Data.Index.BlockIndexBase< T >.InsertBlock ( int  index,
IIndexBlock< T >  block 
)
inlineprivate

Inserts a block at the given position in the index.

Parameters
index
block
Returns

Definition at line 165 of file BlockIndexBase_T.cs.

165  {
166  Blocks.Insert(index, block);
167 
168  // Point to next in the list.
169  if (index + 1 < Blocks.Count) {
170  var nextBlock = Blocks[index + 1];
171  block.Next = nextBlock;
172  nextBlock.Previous = block;
173  } else {
174  block.Next = null;
175  }
176 
177  // Point to previous in the list.
178  if (index > 0) {
179  var prevBlock = Blocks[index - 1];
180  block.Previous = prevBlock;
181  prevBlock.Next = block;
182  } else {
183  block.Previous = null;
184  }
185 
186  return block;
187  }
List< IIndexBlock< T > > Blocks
void Deveel.Data.Index.BlockIndexBase< T >.InsertIntoBlock ( value,
int  blockIndex,
IIndexBlock< T >  block,
int  position 
)
inlineprivate

Inserts a value in the given block position in the list.

Parameters
value
blockIndex
block
position

Definition at line 223 of file BlockIndexBase_T.cs.

223  {
224  block.Insert(value, position);
225 
226  ++Count;
227  // Is the block full?
228  if (block.IsFull) {
229  // We need to move half of the data out of this block into either the
230  // next block or create a new block to store it.
231 
232  // The size that we going to zap out of this block.
233  int moveSize = (block.Count/7) - 1;
234 
235  // The block to move half the data from this block.
236  IIndexBlock<T> moveTo;
237 
238  // Is there a next block?
239  if (blockIndex < Blocks.Count - 1) {
240  var nextBlock = Blocks[blockIndex + 1];
241  // Yes, can this block contain half the values from this block?
242  if (nextBlock.CanContain(moveSize)) {
243  moveTo = nextBlock;
244  } else {
245  // Can't contain so insert a new block.
246  moveTo = InsertBlock(blockIndex + 1, NewBlock());
247  }
248 
249  } else {
250  // No next block so create a new block
251  moveTo = InsertBlock(blockIndex + 1, NewBlock());
252  }
253 
254  // 'moveTo' should be set to the block we are to use to move half the
255  // data from this block.
256  block.MoveTo(moveTo, 0, moveSize);
257  }
258  }
abstract IIndexBlock< T > NewBlock()
Creates a new IIndexBlock for the given implementation.
List< IIndexBlock< T > > Blocks
IIndexBlock< T > InsertBlock(int index, IIndexBlock< T > block)
Inserts a block at the given position in the index.
void Deveel.Data.Index.BlockIndexBase< T >.InsertSort ( value)
inline

Definition at line 586 of file BlockIndexBase_T.cs.

586  {
587  CheckImmutable();
588 
589  int blockIndex = FindLastBlock(value);
590 
591  if (blockIndex < 0) {
592  // Not found a block,
593  // The block to insert the value,
594  blockIndex = (-(blockIndex + 1)) - 1;
595  if (blockIndex < 0) {
596  blockIndex = 0;
597  }
598  }
599 
600  // We got a block, so find out if it's in the block or not.
601  var block = Blocks[blockIndex];
602 
603  // The point to insert in the block,
604  int i = block.SearchLast(value);
605  if (i < 0) {
606  i = -(i + 1);
607  } else {
608  i = i + 1;
609  // NOTE: A block can never become totally full so it's always okay to
610  // skip one ahead.
611  }
612 
613  // Insert value into the block,
614  InsertIntoBlock(value, blockIndex, block, i);
615  }
void InsertIntoBlock(T value, int blockIndex, IIndexBlock< T > block, int position)
Inserts a value in the given block position in the list.
List< IIndexBlock< T > > Blocks
void CheckImmutable()
Checks if the current index is mutable.
int FindLastBlock(object key, IIndexComparer< T > comparer)
Uses a binary search algorithm to quickly determine the index of the IIndexBlock within 'blocks' o...
void Deveel.Data.Index.BlockIndexBase< T >.InsertSort ( object  key,
value,
IIndexComparer< T >  comparer 
)
inline

Definition at line 698 of file BlockIndexBase_T.cs.

698  {
699  CheckImmutable();
700 
701  int blockIndex = FindLastBlock(key, comparer);
702 
703  if (blockIndex < 0) {
704  // Not found a block,
705  // The block to insert the value,
706  blockIndex = (-(blockIndex + 1)) - 1;
707  if (blockIndex < 0) {
708  blockIndex = 0;
709  }
710  }
711 
712  // We got a block, so find out if it's in the block or not.
713  var block = Blocks[blockIndex];
714 
715  // The point to insert in the block,
716  int i = block.SearchLast(key, comparer);
717  if (i < 0) {
718  i = -(i + 1);
719  } else {
720  i = i + 1;
721  // NOTE: A block can never become totally full so it's always okay to
722  // skip one ahead.
723  }
724 
725  // Insert value into the block,
726  InsertIntoBlock(value, blockIndex, block, i);
727  }
void InsertIntoBlock(T value, int blockIndex, IIndexBlock< T > block, int position)
Inserts a value in the given block position in the list.
List< IIndexBlock< T > > Blocks
void CheckImmutable()
Checks if the current index is mutable.
int FindLastBlock(object key, IIndexComparer< T > comparer)
Uses a binary search algorithm to quickly determine the index of the IIndexBlock within 'blocks' o...
static bool Deveel.Data.Index.BlockIndexBase< T >.IsGreater ( x,
y 
)
inlinestaticprivate

Definition at line 429 of file BlockIndexBase_T.cs.

429  {
430  return x.CompareTo(y) > 0;
431  }
static bool Deveel.Data.Index.BlockIndexBase< T >.IsGreaterOrEqual ( x,
y 
)
inlinestaticprivate

Definition at line 425 of file BlockIndexBase_T.cs.

425  {
426  return x.CompareTo(y) >= 0;
427  }
static bool Deveel.Data.Index.BlockIndexBase< T >.IsSmaller ( x,
y 
)
inlinestaticprivate

Definition at line 433 of file BlockIndexBase_T.cs.

433  {
434  return x.CompareTo(y) < 0;
435  }
static bool Deveel.Data.Index.BlockIndexBase< T >.IsSmallerOrEqual ( x,
y 
)
inlinestaticprivate

Definition at line 421 of file BlockIndexBase_T.cs.

421  {
422  return x.CompareTo(y) <= 0;
423  }
abstract IIndexBlock<T> Deveel.Data.Index.BlockIndexBase< T >.NewBlock ( )
protectedpure virtual

Creates a new IIndexBlock<T> for the given implementation.

Returns

Implemented in Deveel.Data.Index.BlockIndex< T >.

virtual void Deveel.Data.Index.BlockIndexBase< T >.OnDeleteBlock ( IIndexBlock< T >  block)
inlineprotectedvirtual

Called when the class decides the given IIndexBlock<T> is no longer needed.

Parameters
block

Provided as an event for derived classes to intercept.

Definition at line 155 of file BlockIndexBase_T.cs.

155  {
156  }
T Deveel.Data.Index.BlockIndexBase< T >.RemoveAt ( int  index)
inline

Definition at line 555 of file BlockIndexBase_T.cs.

555  {
556  CheckImmutable();
557 
558  int size = Blocks.Count;
559  int start = 0;
560  for (int i = 0; i < size; ++i) {
561  var block = Blocks[i];
562  int bsize = block.Count;
563  if (index >= start && index <= start + bsize) {
564  return RemoveFromBlock(i, block, index - start);
565  }
566  start += bsize;
567  }
568 
569  throw new ArgumentOutOfRangeException("index");
570  }
List< IIndexBlock< T > > Blocks
void CheckImmutable()
Checks if the current index is mutable.
T RemoveFromBlock(int blockIndex, IIndexBlock< T > block, int position)
Removes the value from the given position in the specified block.
void Deveel.Data.Index.BlockIndexBase< T >.RemoveBlock ( int  index)
inlineprivate

Removes a block from the given position in the index.

Parameters
index

Definition at line 193 of file BlockIndexBase_T.cs.

193  {
194  // Alter linked list pointers.
195  IIndexBlock<T> newPrev = null;
196  IIndexBlock<T> newNext = null;
197  if (index + 1 < Blocks.Count) {
198  newNext = Blocks[index + 1];
199  }
200  if (index > 0) {
201  newPrev = Blocks[index - 1];
202  }
203 
204  if (newPrev != null) {
205  newPrev.Next = newNext;
206  }
207  if (newNext != null) {
208  newNext.Previous = newPrev;
209  }
210 
211  var beenRemoved = Blocks[index];
212  Blocks.RemoveAt(index);
213  OnDeleteBlock(beenRemoved);
214  }
virtual void OnDeleteBlock(IIndexBlock< T > block)
Called when the class decides the given IIndexBlock is no longer needed.
List< IIndexBlock< T > > Blocks
T Deveel.Data.Index.BlockIndexBase< T >.RemoveFromBlock ( int  blockIndex,
IIndexBlock< T >  block,
int  position 
)
inlineprivate

Removes the value from the given position in the specified block.

Parameters
blockIndex
block
position

It returns the value that used to be at that position.

Returns

Definition at line 270 of file BlockIndexBase_T.cs.

270  {
271  var oldValue = block.RemoveAt(position);
272  --Count;
273  // If we have emptied out this block, then we should remove it from the
274  // list.
275  if (block.IsEmpty && Blocks.Count > 1)
276  RemoveBlock(blockIndex);
277 
278  return oldValue;
279  }
void RemoveBlock(int index)
Removes a block from the given position in the index.
List< IIndexBlock< T > > Blocks
bool Deveel.Data.Index.BlockIndexBase< T >.RemoveSort ( value)
inline

Definition at line 650 of file BlockIndexBase_T.cs.

650  {
651  CheckImmutable();
652 
653  int blockIndex = FindLastBlock(value);
654 
655  if (blockIndex < 0) {
656  // Not found a block,
657  // The block to remove the value,
658  blockIndex = (-(blockIndex + 1)) - 1;
659  if (blockIndex < 0) {
660  blockIndex = 0;
661  }
662  }
663 
664  // We got a block, so find out if it's in the block or not.
665  var block = Blocks[blockIndex];
666 
667  // The point to remove the block,
668  int i = block.SearchLast(value);
669  if (i < 0) {
670  // This means we can't found the value in the given block, so return
671  // false.
672  return false;
673  }
674 
675  // Remove value into the block,
676  var valRemoved = RemoveFromBlock(blockIndex, block, i);
677  if (!value.Equals(valRemoved))
678  throw new InvalidOperationException("Incorrect value removed.");
679 
680  // Value removed so return true.
681  return true;
682  }
List< IIndexBlock< T > > Blocks
void CheckImmutable()
Checks if the current index is mutable.
int FindLastBlock(object key, IIndexComparer< T > comparer)
Uses a binary search algorithm to quickly determine the index of the IIndexBlock within 'blocks' o...
T RemoveFromBlock(int blockIndex, IIndexBlock< T > block, int position)
Removes the value from the given position in the specified block.
T Deveel.Data.Index.BlockIndexBase< T >.RemoveSort ( object  key,
value,
IIndexComparer< T >  comparer 
)
inline

Definition at line 729 of file BlockIndexBase_T.cs.

729  {
730  CheckImmutable();
731 
732  // Find the range of blocks that the value is in.
733  int origBlockIndex = FindFirstBlock(key, comparer);
734  int blockIndex = origBlockIndex;
735  int lastBlockIndex = Blocks.Count - 1;
736 
737  if (blockIndex < 0)
738  // Not found in a block,
739  throw new InvalidOperationException("Value (" + key + ") was not found in the list.");
740 
741  var block = Blocks[blockIndex];
742  int i = block.IndexOf(value);
743  while (i == -1) {
744  // If not found, go to next block
745  ++blockIndex;
746  if (blockIndex > lastBlockIndex)
747  throw new InvalidOperationException("Value (" + key + ") was not found in the list.");
748 
749  block = Blocks[blockIndex];
750  // Try and find the value within this block
751  i = block.IndexOf(value);
752  }
753 
754  // Remove value from the block,
755  return RemoveFromBlock(blockIndex, block, i);
756  }
List< IIndexBlock< T > > Blocks
void CheckImmutable()
Checks if the current index is mutable.
int FindFirstBlock(object key, IIndexComparer< T > c)
Uses a binary search algorithm to quickly determine the index of the IIndexBlock within 'blocks' o...
T RemoveFromBlock(int blockIndex, IIndexBlock< T > block, int position)
Removes the value from the given position in the specified block.
int Deveel.Data.Index.BlockIndexBase< T >.SearchFirst ( object  key,
IIndexComparer< T >  comparer 
)
inline

Definition at line 783 of file BlockIndexBase_T.cs.

783  {
784  int blockNum = FindFirstBlock(key, comparer);
785  int sr;
786 
787  if (blockNum < 0) {
788  // Guarenteed not found in any blocks so return start of insert block
789  blockNum = (-(blockNum + 1)); // - 1;
790  sr = -1;
791  } else {
792  // We got a block, so find out if it's in the block or not.
793  var block = Blocks[blockNum];
794 
795  // Try and find it in the block,
796  sr = block.SearchFirst(key, comparer);
797  }
798 
799  int offset = 0;
800  for (int i = 0; i < blockNum; ++i) {
801  var block = Blocks[i];
802  offset += block.Count;
803  }
804 
805  return sr >= 0 ? offset + sr : -offset + sr;
806  }
List< IIndexBlock< T > > Blocks
int FindFirstBlock(object key, IIndexComparer< T > c)
Uses a binary search algorithm to quickly determine the index of the IIndexBlock within 'blocks' o...
int Deveel.Data.Index.BlockIndexBase< T >.SearchLast ( object  key,
IIndexComparer< T >  comparer 
)
inline

Definition at line 758 of file BlockIndexBase_T.cs.

758  {
759  int blockIndex = FindLastBlock(key, comparer);
760  int sr;
761 
762  if (blockIndex < 0) {
763  // Guarenteed not found in any blocks so return start of insert block
764  blockIndex = (-(blockIndex + 1)); // - 1;
765  sr = -1;
766  } else {
767  // We got a block, so find out if it's in the block or not.
768  var block = Blocks[blockIndex];
769 
770  // Try and find it in the block,
771  sr = block.SearchLast(key, comparer);
772  }
773 
774  int offset = 0;
775  for (int i = 0; i < blockIndex; ++i) {
776  var block = Blocks[i];
777  offset += block.Count;
778  }
779 
780  return sr >= 0 ? offset + sr : -offset + sr;
781  }
List< IIndexBlock< T > > Blocks
int FindLastBlock(object key, IIndexComparer< T > comparer)
Uses a binary search algorithm to quickly determine the index of the IIndexBlock within 'blocks' o...
bool Deveel.Data.Index.BlockIndexBase< T >.UniqueInsertSort ( value)
inline

Definition at line 617 of file BlockIndexBase_T.cs.

617  {
618  CheckImmutable();
619 
620  int blockIndex = FindLastBlock(value);
621 
622  if (blockIndex < 0) {
623  // Not found a block,
624  // The block to insert the value,
625  blockIndex = (-(blockIndex + 1)) - 1;
626  if (blockIndex < 0) {
627  blockIndex = 0;
628  }
629  }
630 
631  // We got a block, so find out if it's in the block or not.
632  var block = Blocks[blockIndex];
633 
634  // The point to insert in the block,
635  int i = block.SearchLast(value);
636  if (i < 0) {
637  i = -(i + 1);
638  } else {
639  // This means we found the value in the given block, so return false.
640  return false;
641  }
642 
643  // Insert value into the block,
644  InsertIntoBlock(value, blockIndex, block, i);
645 
646  // Value inserted so return true.
647  return true;
648  }
void InsertIntoBlock(T value, int blockIndex, IIndexBlock< T > block, int position)
Inserts a value in the given block position in the list.
List< IIndexBlock< T > > Blocks
void CheckImmutable()
Checks if the current index is mutable.
int FindLastBlock(object key, IIndexComparer< T > comparer)
Uses a binary search algorithm to quickly determine the index of the IIndexBlock within 'blocks' o...

Property Documentation

List<IIndexBlock<T> > Deveel.Data.Index.BlockIndexBase< T >.Blocks
getprivate setprotected

Definition at line 118 of file BlockIndexBase_T.cs.

int Deveel.Data.Index.BlockIndexBase< T >.Count
getprivate set

Definition at line 122 of file BlockIndexBase_T.cs.

bool Deveel.Data.Index.BlockIndexBase< T >.IsReadOnly
getset

Definition at line 120 of file BlockIndexBase_T.cs.

T Deveel.Data.Index.BlockIndexBase< T >.this[int index]
get

Definition at line 124 of file BlockIndexBase_T.cs.


The documentation for this class was generated from the following file: