public static IntArrayList IntersectSorted(IntArrayList list1, IntArrayList list2, IComparer comparer) { int count1 = list1.Count; int count2 = list2.Count; if (count1 == 0) { return(list1); } if (count2 == 0) { return(list2); } IntArrayList result = new IntArrayList(Math.Min(count1, count2)); int index1 = 0; int index2 = 0; while (index1 < count1 && index2 < count2) { int compareResult = comparer.Compare(list1 [index1], list2 [index2]); if (compareResult == 0) { result.Add(list1 [index1]); index1++; } else if (compareResult < 0) { index1++; } else { index2++; } } return(result); }
public abstract void SearchForRange(IFixedLengthKey beginKey, IFixedLengthKey endKey, IntArrayList offsets);
public abstract void GetAllKeys(IntArrayList offsets);
public void SearchForRange(IFixedLengthKey beginKey, IFixedLengthKey endKey, IntArrayList offsets) { if (_treeNode.Count == 0) { return; } int index, offset; KeyPair pair; IFixedLengthKey deferredKey; int deferredInsertionsIndex = ~GetDeferredInsertionIndex(beginKey, -1); IFixedLengthKey foundKey = SearchL(0, _rightBound, beginKey, -1, out index); if (foundKey != null && index != -1 && index < _rightBound) { int deferredDeletionsIndex = ~GetDeferredDeletionIndex(beginKey, -1); SetPosition(index); for ( ; index < _rightBound; ++index) { IFixedLengthKey aKey = MemoryReadNext(out offset); for ( ; deferredInsertionsIndex < _deferredInsertions.Count; ++deferredInsertionsIndex) { pair = (KeyPair)_deferredInsertions[deferredInsertionsIndex]; deferredKey = pair._key; if (deferredKey.CompareTo(aKey) > 0) { break; } if (beginKey.CompareTo(deferredKey) <= 0) { if (endKey.CompareTo(deferredKey) < 0) { break; } offsets.Add(pair._offset); } } if (endKey.CompareTo(aKey) < 0) { break; } bool deleted = false; while (deferredDeletionsIndex < _deferredDeletions.Count) { pair = (KeyPair)_deferredDeletions[deferredDeletionsIndex]; int compareResult = pair._key.CompareTo(aKey); if (compareResult == 0) { compareResult = pair._offset - offset; } if (compareResult > 0) { break; } ++deferredDeletionsIndex; if (compareResult == 0) { deleted = true; break; } } if (!deleted) { offsets.Add(offset); } } } for ( ; deferredInsertionsIndex < _deferredInsertions.Count; ++deferredInsertionsIndex) { pair = (KeyPair)_deferredInsertions[deferredInsertionsIndex]; deferredKey = pair._key; if (beginKey.CompareTo(deferredKey) <= 0) { if (endKey.CompareTo(deferredKey) < 0) { break; } offsets.Add(pair._offset); } } }
public override void SearchForRange(IFixedLengthKey beginKey, IFixedLengthKey endKey, IntArrayList offsets) { _searchNode.ChangeMinKey(beginKey, 0); RBNodeBase rbNode = _rbTree.GetMaximumLess(_searchNode); if (rbNode == null) { rbNode = _rbTree.GetMinimumNode(); } while (rbNode != null && ((BTreeNode)rbNode.Key).MinKey.CompareTo(endKey) <= 0) { BTreePage page = PreparePage((BTreeNode)rbNode.Key); page.SearchForRange(beginKey, endKey, offsets); rbNode = _rbTree.GetNext(rbNode); } }
internal IntArrayListEnumerator(IntArrayList list) { _list = list; _index = -1; _version = list._version; }
/// <summary> /// Intersects the specified lists of integers. This method is guaranteed to return a new list instance /// and not to reuse any of the lists passed to it. /// </summary> /// <param name="list1">The first list to intersect.</param> /// <param name="list2">The second list to intersect.</param> /// <returns>The intersection result.</returns> public static IntArrayList IntersectSortedNew(IntArrayList list1, IntArrayList list2) { int count1 = list1.Count; int count2 = list2.Count; int index1 = 0; int index2 = 0; int[] result = new int [AdjustCapacity(Math.Min(count1, count2))]; int destIndex = 0; if (count1 > 0 && count2 > 0) { int compareResult = list1[0] - list2[0]; if (compareResult < 0) { compareResult = list1.BinarySearch(list2[0]); if (compareResult >= 0) { index1 = compareResult; } else { index1 = ~compareResult; } } else if (compareResult > 0) { compareResult = list2.BinarySearch(list1[0]); if (compareResult >= 0) { index2 = compareResult; } else { index2 = ~compareResult; } } compareResult = list1[count1 - 1] - list2[count2 - 1]; if (compareResult < 0) { compareResult = Array.BinarySearch(list2._items, index2, count2 - index2, list1[count1 - 1]); if (compareResult >= 0) { count2 = compareResult + 1; } else { count2 = ~compareResult; } } else if (compareResult > 0) { compareResult = Array.BinarySearch(list1._items, index1, count1 - index1, list2[count2 - 1]); if (compareResult >= 0) { count1 = compareResult + 1; } else { count1 = ~compareResult; } } while (index1 < count1 && index2 < count2) { compareResult = list1._items[index1] - list2._items[index2]; if (compareResult == 0) { int value = list1._items [index1]; result [destIndex++] = value; index1++; // skip duplicates while (index1 < count1 && list1._items [index1] == value) { index1++; } do { index2++; }while(index2 < count2 && list2._items [index2] == value); } else if (compareResult < 0) { index1++; } else { index2++; } } } return(new IntArrayList(result, destIndex)); }
public void IntersectSorted(IntArrayList list) { int count1 = _size; int count2 = list._size; int compareResult; int index1 = 0; int index2 = 0; int destIndex = 0; if (count1 > 0 && count2 > 0) { compareResult = _items[0] - list[0]; if (compareResult < 0) { compareResult = BinarySearch(list[0]); if (compareResult >= 0) { index1 = compareResult; } else { index1 = ~compareResult; } } else if (compareResult > 0) { compareResult = list.BinarySearch(_items[0]); if (compareResult >= 0) { index2 = compareResult; } else { index2 = ~compareResult; } } compareResult = _items[count1 - 1] - list[count2 - 1]; if (compareResult < 0) { compareResult = Array.BinarySearch(list._items, index2, count2 - index2, _items[count1 - 1]); if (compareResult >= 0) { count2 = compareResult + 1; } else { count2 = ~compareResult; } } else if (compareResult > 0) { compareResult = Array.BinarySearch(_items, index1, count1 - index1, list[count2 - 1]); if (compareResult >= 0) { count1 = compareResult + 1; } else { count1 = ~compareResult; } } while (index1 < count1 && index2 < count2) { compareResult = _items[index1] - list._items[index2]; if (compareResult == 0) { int value = _items[index1++]; _items[destIndex++] = value; // skip duplicates while (index1 < count1 && _items[index1] == value) { index1++; } do { index2++; }while(index2 < count2 && list._items[index2] == value); } else if (compareResult < 0) { index1++; } else { index2++; } } } _size = destIndex; _version++; }