private long UpperBound(IComparable key, Operation.Operation.ComparableComparator comparator) { long[] sortedIndex = GetSortIndexAsc(); long first = 0; long count = sortedIndex.Length; while (count > 0) { long it = first; long step = count / 2; it += step; var value = GetRowValue(sortedIndex[it]); int result = comparator(key, value); if (result >= 0) { first = it + 1; count -= step + 1; } else { count = step; } } return(first); }
public override int Compare(long rowLhs, Operation.Expression expression, long rowRhs) { Operation.Operation.ComparableComparator comparator = Operation.Operation.GetComparator(type, expression.type); var val1 = GetRowValue(rowLhs); var val2 = expression.GetComparableValue(rowRhs); int result = comparator(val1, val2); return(result); }
public override long[] GetMatchIndex(ArrayRange rowRange, Operation.Operator operation, Operation.Expression expression, long expressionRowFirst, bool rowToRow) { Update(); long count = rowRange.Count; long[] matchedIndices = new long[count]; long indexOflastMatchedIndex = 0; Operation.Operation.ComparableComparator comparator = Operation.Operation.GetComparator(type, expression.type); if (rowToRow) { for (long i = 0; i != count; ++i) { var leftValue = GetRowValue(rowRange[i]); if (Operation.Operation.Match(operation, comparator, leftValue, expression, expressionRowFirst + i)) { matchedIndices[indexOflastMatchedIndex] = rowRange[i]; ++indexOflastMatchedIndex; } } } else { if (Operation.Operation.IsOperatorOneToMany(operation)) { for (int i = 0; i != count; ++i) { var leftValue = GetRowValue(rowRange[i]); if (Operation.Operation.Match(operation, comparator, leftValue, expression, expressionRowFirst)) { matchedIndices[indexOflastMatchedIndex] = rowRange[i]; ++indexOflastMatchedIndex; } } } else { var valueRight = expression.GetComparableValue(expressionRowFirst); for (int i = 0; i != count; ++i) { var leftValue = GetRowValue(rowRange[i]); if (Operation.Operation.Match(operation, comparator, leftValue, valueRight)) { matchedIndices[indexOflastMatchedIndex] = rowRange[i]; ++indexOflastMatchedIndex; } } } } if (indexOflastMatchedIndex != count) { System.Array.Resize(ref matchedIndices, (int)indexOflastMatchedIndex); } return(matchedIndices); }
public override long[] GetMatchIndex(ArrayRange rowRange, Operation.Operator operation, Operation.Expression expression, long expressionRowFirst, bool rowToRow) { using (Profiling.GetMarker(Profiling.MarkerId.ColumnMatchQuery).Auto()) { Update(); long count = rowRange.Count; var matchedIndices = new List <long>(128); Operation.Operation.ComparableComparator comparator = Operation.Operation.GetComparator(type, expression.type); if (rowToRow) { for (long i = 0; i != count; ++i) { var leftValue = m_Cache[rowRange[i]]; if (Operation.Operation.Match(operation, comparator, leftValue, expression, expressionRowFirst + i)) { matchedIndices.Add(rowRange[i]); } } } else { if (Operation.Operation.IsOperatorOneToMany(operation)) { for (int i = 0; i != count; ++i) { var leftValue = m_Cache[rowRange[i]]; if (Operation.Operation.Match(operation, comparator, leftValue, expression, expressionRowFirst)) { matchedIndices.Add(rowRange[i]); } } } else { var valueRight = expression.GetComparableValue(expressionRowFirst); //Optimization for equal operation when querying on all data if (rowRange.IsSequence && operation == Operation.Operator.Equal) { //use the sorted index to trim down invalid values long[] sortedIndex = GetSortIndexAsc(); int lowerIndexIndex = LowerBoundIndex(sortedIndex, (int)rowRange.Sequence.First, (int)rowRange.Count, valueRight); int upperIndexIndex = (int)rowRange.Sequence.Last; for (int i = lowerIndexIndex; i < upperIndexIndex; ++i) { if (m_Cache[sortedIndex[i]].CompareTo(valueRight) == 0) { matchedIndices.Add(sortedIndex[i]); } else { break; } } #if PROFILER_DEBUG_TEST { //Test to validate if optimization works int matchCount = 0; for (int i = 0; i != count; ++i) { var leftValue = m_Cache[rowRange[i]]; if (Operation.Operation.Match(operation, comparator, leftValue, valueRight)) { ++matchCount; if (matchedIndices.FindIndex(x => x == rowRange[i]) < 0) { UnityEngine.Debug.LogError("GetMatchIndex Optimization failure on index " + rowRange[i]); } } } if (matchCount != matchedIndices.Count) { UnityEngine.Debug.LogError("GetMatchIndex Optimization failure on match count " + matchCount + " != " + matchedIndices.Count); } } #endif } else { for (int i = 0; i != count; ++i) { var leftValue = m_Cache[rowRange[i]]; if (Operation.Operation.Match(operation, comparator, leftValue, valueRight)) { matchedIndices.Add(rowRange[i]); } } } } } var matchedIndicesArray = matchedIndices.ToArray(); #if MEMPROFILER_DEBUG_INFO Algorithm.DebugLog("GetMatchIndex : indexCount " + rowRange.Count + " op:" + Operation.Operation.OperatorToString(operation) + " Exp():" + expression.GetValueString(expressionRowFirst, DefaultDataFormatter.Instance) + " expRowFirst:" + expressionRowFirst + " Column:" + this.GetDebugString(rowRange[0]) + " Expression:" + expression.GetDebugString(expressionRowFirst) + " Result Count:" + (matchedIndices != null ? matchedIndices.Length : 0)); #endif return(matchedIndicesArray); } }
public override long GetFirstMatchIndex(Operation.Operator operation, Operation.Expression expression, long expRowFirst) { using (Profiling.GetMarker(Profiling.MarkerId.ColumnFirstMatchQuery).Auto()) { Update(); long[] sortedIndex = GetSortIndexAsc(); Operation.Operation.ComparableComparator comparator = Operation.Operation.GetComparator(type, expression.type); var val2 = expression.GetComparableValue(expRowFirst); long firstMatchIndex = -1; switch (operation) { case Operation.Operator.Less: { long iFirst = sortedIndex[0]; var val1 = GetRowValue(iFirst); int result = comparator(val1, val2); if (result < 0) { firstMatchIndex = iFirst; } break; } case Operation.Operator.LessEqual: { long iFirst = sortedIndex[0]; var val1 = GetRowValue(iFirst); int result = comparator(val1, val2); if (result <= 0) { firstMatchIndex = iFirst; } break; } case Operation.Operator.Equal: { long iFirstGreaterEqual = LowerBound(val2, comparator); if (iFirstGreaterEqual < sortedIndex.Length) { long index = sortedIndex[iFirstGreaterEqual]; var val1 = GetRowValue(index); int comparisonResult = comparator(val1, val2); if (comparisonResult == 0) { firstMatchIndex = index; } } break; } case Operation.Operator.GreaterEqual: { long iFirstGreaterEqual = LowerBound(val2, comparator); if (iFirstGreaterEqual < sortedIndex.Length) { firstMatchIndex = sortedIndex[iFirstGreaterEqual]; } break; } case Operation.Operator.Greater: { long iFirstGreater = UpperBound(val2, comparator); if (iFirstGreater < sortedIndex.Length) { firstMatchIndex = sortedIndex[iFirstGreater]; } break; } } #if (PROFILER_DEBUG_TEST) { long count = sortedIndex.Length; long resultTest = -1; for (long i = 0; i != count; ++i) { long index = sortedIndex[i]; var val = GetRowValue(index); int comparisonResult = comparator(val, val2); bool matchResult = Operation.Operation.Match(operation, comparisonResult); if (matchResult) { resultTest = index; break; } } UnityEngine.Debug.Assert(resultTest == firstMatchIndex); if (resultTest >= 0) { var val1 = GetRowValue(resultTest); int comparisonResult = comparator(val1, val2); bool resultMatch = Operation.Operation.Match(operation, comparisonResult); UnityEngine.Debug.Assert(resultMatch); } } #endif #if MEMPROFILER_DEBUG_INFO Algorithm.DebugLog("GetFirstMatchIndex :" + " op:" + Operation.Operation.OperatorToString(operation) + " Exp():" + expression.GetValueString(expRowFirst, DefaultDataFormatter.Instance) + " expRowFirst:" + expRowFirst + " Column:" + this.GetDebugString(0) + " Expression:" + expression.GetDebugString(expRowFirst) + " Result:" + firstMatchIndex); #endif return(firstMatchIndex); } }
public override long[] GetMatchIndex(ArrayRange rowRange, Operation.Operator operation, Operation.Expression expression, long expressionRowFirst, bool rowToRow) { using (Profiling.GetMarker(Profiling.MarkerId.ColumnMatchQuery).Auto()) { Update(); long count = rowRange.Count; long[] matchedIndices = new long[count]; long indexOflastMatchedIndex = 0; Operation.Operation.ComparableComparator comparator = Operation.Operation.GetComparator(type, expression.type); if (rowToRow) { for (long i = 0; i != count; ++i) { var leftValue = GetRowValue(rowRange[i]); if (Operation.Operation.Match(operation, comparator, leftValue, expression, expressionRowFirst + i)) { matchedIndices[indexOflastMatchedIndex] = rowRange[i]; ++indexOflastMatchedIndex; } } } else { if (Operation.Operation.IsOperatorOneToMany(operation)) { for (int i = 0; i != count; ++i) { var leftValue = GetRowValue(rowRange[i]); if (Operation.Operation.Match(operation, comparator, leftValue, expression, expressionRowFirst)) { matchedIndices[indexOflastMatchedIndex] = rowRange[i]; ++indexOflastMatchedIndex; } } } else { var valueRight = expression.GetComparableValue(expressionRowFirst); for (int i = 0; i != count; ++i) { var leftValue = GetRowValue(rowRange[i]); if (Operation.Operation.Match(operation, comparator, leftValue, valueRight)) { matchedIndices[indexOflastMatchedIndex] = rowRange[i]; ++indexOflastMatchedIndex; } } } } if (indexOflastMatchedIndex != count) { System.Array.Resize(ref matchedIndices, (int)indexOflastMatchedIndex); } #if MEMPROFILER_DEBUG_INFO Algorithm.DebugLog("GetMatchIndex : indexCount " + rowRange.Count + " op:" + Operation.Operation.OperatorToString(operation) + " Exp():" + expression.GetValueString(expressionRowFirst, DefaultDataFormatter.Instance) + " expRowFirst:" + expressionRowFirst + " Column:" + this.GetDebugString(rowRange[0]) + " Expression:" + expression.GetDebugString(expressionRowFirst) + " Result Count:" + (matchedIndices != null ? matchedIndices.Length : 0)); #endif return(matchedIndices); } }
public override long[] GetMatchIndex(ArrayRange rowRange, Operation.Operator operation, Operation.Expression expression, long expressionRowFirst, bool rowToRow) { Update(); long count = rowRange.Count; var matchedIndices = new List <long>(128); Operation.Operation.ComparableComparator comparator = Operation.Operation.GetComparator(type, expression.type); if (rowToRow) { for (long i = 0; i != count; ++i) { var leftValue = m_Cache[rowRange[i]]; if (Operation.Operation.Match(operation, comparator, leftValue, expression, expressionRowFirst + i)) { matchedIndices.Add(rowRange[i]); } } } else { if (Operation.Operation.IsOperatorOneToMany(operation)) { for (int i = 0; i != count; ++i) { var leftValue = m_Cache[rowRange[i]]; if (Operation.Operation.Match(operation, comparator, leftValue, expression, expressionRowFirst)) { matchedIndices.Add(rowRange[i]); } } } else { var valueRight = expression.GetComparableValue(expressionRowFirst); //Optimization for equal operation when querying on all data if (rowRange.IsSequence && operation == Operation.Operator.Equal) { //use the sorted index to trim down invalid values long[] sortedIndex = GetSortIndexAsc(); int lowerIndexIndex = LowerBoundIndex(sortedIndex, (int)rowRange.Sequence.First, (int)rowRange.Count, valueRight); int upperIndexIndex = (int)rowRange.Sequence.Last; for (int i = lowerIndexIndex; i < upperIndexIndex; ++i) { if (m_Cache[sortedIndex[i]].CompareTo(valueRight) == 0) { matchedIndices.Add(sortedIndex[i]); } else { break; } } } else { for (int i = 0; i != count; ++i) { var leftValue = m_Cache[rowRange[i]]; if (Operation.Operation.Match(operation, comparator, leftValue, valueRight)) { matchedIndices.Add(rowRange[i]); } } } } } var matchedIndicesArray = matchedIndices.ToArray(); return(matchedIndicesArray); }
public override long GetFirstMatchIndex(Operation.Operator operation, Operation.Expression expression, long expRowFirst) { Update(); long[] sortedIndex = GetSortIndexAsc(); Operation.Operation.ComparableComparator comparator = Operation.Operation.GetComparator(type, expression.type); var val2 = expression.GetComparableValue(expRowFirst); long firstMatchIndex = -1; switch (operation) { case Operation.Operator.Less: { long iFirst = sortedIndex[0]; var val1 = GetRowValue(iFirst); int result = comparator(val1, val2); if (result < 0) { firstMatchIndex = iFirst; } break; } case Operation.Operator.LessEqual: { long iFirst = sortedIndex[0]; var val1 = GetRowValue(iFirst); int result = comparator(val1, val2); if (result <= 0) { firstMatchIndex = iFirst; } break; } case Operation.Operator.Equal: { long iFirstGreaterEqual = LowerBound(val2, comparator); if (iFirstGreaterEqual < sortedIndex.Length) { long index = sortedIndex[iFirstGreaterEqual]; var val1 = GetRowValue(index); int comparisonResult = comparator(val1, val2); if (comparisonResult == 0) { firstMatchIndex = index; } } break; } case Operation.Operator.GreaterEqual: { long iFirstGreaterEqual = LowerBound(val2, comparator); if (iFirstGreaterEqual < sortedIndex.Length) { firstMatchIndex = sortedIndex[iFirstGreaterEqual]; } break; } case Operation.Operator.Greater: { long iFirstGreater = UpperBound(val2, comparator); if (iFirstGreater < sortedIndex.Length) { firstMatchIndex = sortedIndex[iFirstGreater]; } break; } } return(firstMatchIndex); }