/// <summary>
 /// Adjust current index condition based on the result item list
 /// </summary>
 /// <param name="conditionBoundry">condition boundry bytes</param>
 /// <param name="queryIndexCondition">query index condition</param>
 /// <param name="comparer">comparer the sort is based on</param>
 private static void AdjustIndexCondition(byte[] conditionBoundry, ref IndexCondition queryIndexCondition, BaseComparer comparer)
 {
     if (conditionBoundry != null)
     {
         // check the condiftion boundry and put it into the queryIndexCondition
         if (comparer.SortOrderList[0].SortBy == SortBy.DESC) // if desc
         {
             if (queryIndexCondition != null)
             {
                 if (queryIndexCondition.InclusiveMinValue == null)
                 {
                     queryIndexCondition.InclusiveMinValue = conditionBoundry;
                 }
                 else if (comparer.Compare(conditionBoundry, queryIndexCondition.InclusiveMinValue) == -1)
                 {
                     queryIndexCondition.InclusiveMinValue = conditionBoundry;
                 }
             }
             else
             {
                 queryIndexCondition = new IndexCondition
                 {
                     InclusiveMinValue = conditionBoundry
                 };
             }
         }
         else // if asc
         {
             if (queryIndexCondition != null)
             {
                 if (queryIndexCondition.InclusiveMaxValue == null)
                 {
                     queryIndexCondition.InclusiveMaxValue = conditionBoundry;
                 }
                 else if (comparer.Compare(conditionBoundry, queryIndexCondition.InclusiveMaxValue) == 1)
                 {
                     queryIndexCondition.InclusiveMaxValue = conditionBoundry;
                 }
             }
             else
             {
                 queryIndexCondition = new IndexCondition
                 {
                     InclusiveMaxValue = conditionBoundry
                 };
             }
         }
     }
 }
        public override int Compare(T[] x, T[] y)
        {
            if (x == null)
            {
                return(y == null ? 0 : -DefaultDirectionMultiplier);
            }
            if (y == null)
            {
                return(DefaultDirectionMultiplier);
            }

            var minLength = x.Length;
            var result    = -minLength - 1;

            if (minLength > y.Length)
            {
                minLength = y.Length;
                result    = minLength + 1;
            }
            for (var i = 0; i < minLength;)
            {
                var r = BaseComparer.Compare(x[i], y[i]);
                i++;
                if (r == 0)
                {
                    continue;
                }
                return(r > 0 ? i : -i);
            }
            return(result * (int)ComparisonRules.GetDefaultRuleDirection(minLength));
        }
 public override int Compare(Assembly x, Assembly y)
 {
     if (ReferenceEquals(x, y))
     {
         return(0);
     }
     if (x == null)
     {
         if (y == null)
         {
             return(0);
         }
         else
         {
             return(-DefaultDirectionMultiplier);
         }
     }
     else
     {
         if (y == null)
         {
             return(DefaultDirectionMultiplier);
         }
         else
         {
             return(BaseComparer.Compare(x.FullName, y.FullName));
         }
     }
 }
Example #4
0
        /// <summary>
        /// Applies the Condition on the specified item value.
        /// </summary>
        /// <param name="itemValue">The item value.</param>
        /// <returns></returns>
        internal bool Process(byte[] itemValue)
        {
            bool         retVal       = false;
            BaseComparer itemComparer = new BaseComparer(false, null, new List <SortOrder>(1)
            {
                new SortOrder(DataType, SortBy.ASC)
            });

            switch (Operation)
            {
            case Operation.Equals:
                retVal = (ByteArrayComparerUtil.CompareByteArrays(Value, itemValue)) ? true : false;
                break;

            case Operation.NotEquals:
                retVal = (!ByteArrayComparerUtil.CompareByteArrays(Value, itemValue)) ? true : false;
                break;

            case Operation.GreaterThan:
                retVal = (itemComparer.Compare(Value, itemValue) < 0) ? true : false;
                break;

            case Operation.GreaterThanEquals:
                retVal = (itemComparer.Compare(Value, itemValue) <= 0) ? true : false;
                break;

            case Operation.LessThan:
                retVal = (itemComparer.Compare(Value, itemValue) > 0) ? true : false;
                break;

            case Operation.LessThanEquals:
                retVal = (itemComparer.Compare(Value, itemValue) >= 0) ? true : false;
                break;

            case Operation.BitwiseComplement:
            case Operation.BitwiseAND:
            case Operation.BitwiseOR:
            case Operation.BitwiseXOR:
            case Operation.BitwiseShiftLeft:
            case Operation.BitwiseShiftRight:
                retVal = CheckBitwiseCondition(itemValue);
                break;
            }
            return(retVal);
        }
        public override int Compare(Pair <T> x, Pair <T> y)
        {
            int result = BaseComparer.Compare(x.First, y.First);

            if (result != 0)
            {
                return(result);
            }
            return(BaseComparer.Compare(x.Second, y.Second));
        }
Example #6
0
        public int Compare(T x, T y)
        {
            var result = BaseComparer.Compare(x, y);

            if (Descending)
            {
                result = -result;
            }
            return(result);
        }
        // Constructors

        public EnumComparer(IComparerProvider provider, ComparisonRules comparisonRules)
            : base(provider, comparisonRules)
        {
            valueToIndex = new Dictionary <TEnum, int>();
            Array originalValues = Enum.GetValues(typeof(TEnum));
            int   valueCount     = originalValues.Length;

            if (valueCount < 1)
            {
                valueCount = 1;
                values     = new TEnum[] { default(TEnum) };
                valueToIndex.Add(values[0], 0);
            }
            else
            {
                TEnum[] allValues = new TEnum[valueCount];
                for (int i = 0; i < valueCount; i++)
                {
                    allValues[i] = (TEnum)originalValues.GetValue(i);
                }
                Array.Sort <TEnum>(allValues, (x, y) => BaseComparer.Compare(enumToSystem(x), enumToSystem(y)));
                for (int i = 0; i < valueCount - 1; i++)
                {
                    int j = i + 1;
                    if (BaseComparer.Equals(enumToSystem(allValues[i]), enumToSystem(allValues[j])))
                    {
                        valueCount--;
                        Array.Copy(allValues, j, allValues, i, valueCount - i);
                    }
                }
                values = new TEnum[valueCount];
                Array.Copy(allValues, values, valueCount);
                string[] names = Enum.GetNames(typeof(TEnum));
                for (int i = 0; i < names.Length; i++)
                {
                    TEnum current = (TEnum)originalValues.GetValue(i);
                    if (!valueToIndex.ContainsKey(current))
                    {
                        valueToIndex.Add(current, Array.IndexOf(values, current));
                    }
                }
            }
            maxIndex       = valueCount - 1;
            ValueRangeInfo = new ValueRangeInfo <TEnum>(
                true, values[0],
                true, values[valueCount - 1],
                false, default(TEnum));
        }
        public override bool AreSimilar <T>(T first, T second)
        {
            var propertyMetadatas = first.GetType().GetProperties();

            foreach (var propertymeta in propertyMetadatas)
            {
                var firstValue  = propertymeta.GetValue(first);
                var secondValue = propertymeta.GetValue(second);
                var IsEqual     = bc.Compare(firstValue, secondValue);
                if (IsEqual == false)
                {
                    return(false);
                }
            }
            return(true);
        }
Example #9
0
 public override int Compare(TEnumerable x, TEnumerable y)
 {
     if (x == null)
     {
         return(y == null ? 0 : -DefaultDirectionMultiplier);
     }
     else
     {
         if (y == null)
         {
             return(DefaultDirectionMultiplier);
         }
         else
         {
             var ex = x.GetEnumerator();
             var ey = y.GetEnumerator();
             var i  = 1;
             while (true)
             {
                 var hasX = ex.MoveNext();
                 var hasY = ey.MoveNext();
                 if (!hasX)
                 {
                     return(!hasY ? 0 : -i * DefaultDirectionMultiplier);
                 }
                 else
                 {
                     if (!hasY)
                     {
                         return(i * DefaultDirectionMultiplier);
                     }
                     else
                     {
                         var r = BaseComparer.Compare(ex.Current, ey.Current);
                         if (r != 0)
                         {
                             return(r < 0 ? -i : i);
                         }
                     }
                 }
                 i++;
             }
         }
     }
 }
        private TEnum[] BuildValues(out int valueCount)
        {
            var originalValues = Enum.GetValues(typeof(TEnum));

            valueCount = originalValues.Length;
            if (valueCount < 1)
            {
                valueCount = 1;
                var values = new TEnum[] { default(TEnum) };
                valueToIndex.Add(values[0], 0);
                return(values);
            }
            else
            {
                var allValues = new TEnum[valueCount];
                for (var i = 0; i < valueCount; i++)
                {
                    allValues[i] = (TEnum)originalValues.GetValue(i);
                }

                Array.Sort <TEnum>(allValues, (x, y) => BaseComparer.Compare(EnumToSystem(x), EnumToSystem(y)));
                for (var i = 0; i < valueCount - 1; i++)
                {
                    var j = i + 1;
                    if (BaseComparer.Equals(EnumToSystem(allValues[i]), EnumToSystem(allValues[j])))
                    {
                        valueCount--;
                        Array.Copy(allValues, j, allValues, i, valueCount - i);
                    }
                }
                var values = new TEnum[valueCount];
                Array.Copy(allValues, values, valueCount);
                var names = Enum.GetNames(typeof(TEnum));
                for (var i = 0; i < names.Length; i++)
                {
                    var current = (TEnum)originalValues.GetValue(i);
                    if (!valueToIndex.ContainsKey(current))
                    {
                        valueToIndex.Add(current, Array.IndexOf(values, current));
                    }
                }
                return(values);
            }
        }
Example #11
0
        internal static void MergeItemLists(ref List<ResultItem> list1, List<ResultItem> list2, int maxMergeCount, BaseComparer baseComparer)
        {
            int mergedListCount = list1.Count + list2.Count;
            int count1 = 0;
            int count2 = 0;
            if (mergedListCount > maxMergeCount)
            {
                mergedListCount = maxMergeCount;
            }
            List<ResultItem> newList = new List<ResultItem>(mergedListCount);

            #region Merge until one list ends
            for (int i = 0; i < mergedListCount && count1 != list1.Count && count2 != list2.Count; i++)
            {
                newList.Add((baseComparer.Compare(list1[count1], list2[count2]) <= 0) ?
                                    list1[count1++] : // list1 item is greater
                                    list2[count2++]); // list2 item is greater
            }
            #endregion

            #region Append rest of the list1/list2 to newList
            if (count1 != list1.Count && newList.Count < mergedListCount)
            {
                int count = list1.Count - count1;
                for (int i = 0; i < count && newList.Count < mergedListCount; i++)
                {
                    newList.Add(list1[count1++]);
                }
            }
            else if (count2 != list2.Count && newList.Count < mergedListCount)
            {
                int count = list2.Count - count2;
                for (int i = 0; i < count && newList.Count < mergedListCount; i++)
                {
                    newList.Add(list2[count2++]);
                }
            }
            #endregion

            #region Update reference
            list1 = newList;
            #endregion
        }
Example #12
0
        internal static void Intersect <T>(bool isTagPrimarySort,
                                           string sortFieldName,
                                           List <string> localIdentityTagNames,
                                           List <SortOrder> sortOrderList,
                                           ItemList <T> resultList,
                                           ItemList <T> currentList,
                                           int maxResultItems,
                                           bool isLastIntersection) where T : IItem
        {
            int i;

            if ((localIdentityTagNames == null || localIdentityTagNames.Count < 1) && !isTagPrimarySort)
            {
                // Traverse both CacheIndexInternal simultaneously
                int          j;
                BaseComparer comparer = new BaseComparer(isTagPrimarySort, sortFieldName, sortOrderList);
                for (i = 0, j = 0; i < resultList.Count && j < currentList.Count &&
                     /* Check if resultList has accumalated enough items */ !(maxResultItems > 0 && isLastIntersection && i == maxResultItems);)
                {
                    int retVal = comparer.Compare(resultList.GetItem(i), currentList.GetItem(j));

                    if (retVal == 0)
                    {
                        //Items equal. Move pointers to both lists
                        i++;
                        j++;
                    }
                    else
                    {
                        if (retVal > 0) // resultList item is greater and skip currentList item
                        {
                            j++;
                        }
                        else // currentList item is greater and remove resultList item
                        {
                            resultList.RemoveAt(i);
                        }
                    }
                }
            }
            else
            {
                // Assign smaller list to resultList
                if (resultList.Count > currentList.Count)
                {
                    //Swap resultList and currentList
                    ItemList <T> tempList = resultList;
                    resultList  = currentList;
                    currentList = tempList;
                }

                for (i = 0; i < resultList.Count && !(maxResultItems > 0 && i == maxResultItems);)
                {
                    if (currentList.BinarySearchItem(resultList.GetItem(i), isTagPrimarySort, sortFieldName, sortOrderList, localIdentityTagNames) < 0)
                    {
                        //Remove item from resultList
                        resultList.RemoveAt(i);
                    }
                    else
                    {
                        i++;
                    }
                }
            }

            //Get rid of uninspected items in resultList
            if (i < resultList.Count)
            {
                resultList.RemoveRange(i, resultList.Count - i);
            }
        }
Example #13
0
 public override int Compare(T x, T y) => BaseComparer.Compare(x, y);
Example #14
0
 public override int Compare(Wrapper1 <T> x, Wrapper1 <T> y)
 {
     return(BaseComparer.Compare(x.Value, y.Value));
 }
Example #15
0
        /// <summary>
        /// Applies the Condition on the specified item value.
        /// </summary>
        /// <param name="itemValue">The item value.</param>
        /// <returns></returns>
        internal bool Process(byte[] itemValue)
        {
            bool retVal = false;
            BaseComparer itemComparer = new BaseComparer(false, null, new List<SortOrder>(1) { new SortOrder(DataType, SortBy.ASC) });

            switch (Operation)
            {
                case Operation.Equals:
                    retVal = (ByteArrayComparerUtil.CompareByteArrays(Value, itemValue)) ? true : false;
                    break;

                case Operation.NotEquals:
                    retVal = (!ByteArrayComparerUtil.CompareByteArrays(Value, itemValue)) ? true : false;
                    break;

                case Operation.GreaterThan:
                    retVal = (itemComparer.Compare(Value, itemValue) < 0) ? true : false;
                    break;

                case Operation.GreaterThanEquals:
                    retVal = (itemComparer.Compare(Value, itemValue) <= 0) ? true : false;
                    break;

                case Operation.LessThan:
                    retVal = (itemComparer.Compare(Value, itemValue) > 0) ? true : false;
                    break;

                case Operation.LessThanEquals:
                    retVal = (itemComparer.Compare(Value, itemValue) >= 0) ? true : false;
                    break;

                case Operation.BitwiseComplement:
                case Operation.BitwiseAND:
                case Operation.BitwiseOR:
                case Operation.BitwiseXOR:
                case Operation.BitwiseShiftLeft:
                case Operation.BitwiseShiftRight:
                    retVal = CheckBitwiseCondition(itemValue);
                    break;
            }
            return retVal;
        }
 public override int Compare(T x, T y)
 {
     return(BaseComparer.Compare(x, y));
 }
Example #17
0
        internal static void Intersect <T>(
            bool isTagPrimarySort,
            string sortFieldName,
            List <string> localIdentityTagNames,
            List <SortOrder> sortOrderList,
            ItemList <T> resultList,
            ItemList <T> currentList) where T : IItem
        {
            if (localIdentityTagNames == null || localIdentityTagNames.Count < 1)
            {
                // Traverse both CacheIndexInternal simultaneously
                int          i, j;
                BaseComparer comparer = new BaseComparer(isTagPrimarySort, sortFieldName, sortOrderList);
                for (i = resultList.Count - 1, j = currentList.Count - 1; i > -1 && j > -1;)
                {
                    int retVal = comparer.Compare(resultList.GetItem(i), currentList.GetItem(j));

                    if (retVal == 0)
                    {
                        //Items equal. Move pointers to both lists
                        i--;
                        j--;
                    }
                    else
                    {
                        if (retVal < 0) // resultList item is greater
                        {
                            j--;
                        }
                        else
                        {
                            resultList.RemoveAt(i);
                            i--;
                        }
                    }
                }
                //Get rid of uninspected items in resultList
                if (i > -1)
                {
                    resultList.RemoveRange(0, i + 1);
                }
            }
            else
            {
                // Assign smaller list to resultList
                if (resultList.Count > currentList.Count)
                {
                    //Swap resultList and currentList
                    ItemList <T> tempList = resultList;
                    resultList  = currentList;
                    currentList = tempList;
                }
                for (int i = resultList.Count - 1; i > -1; i--)
                {
                    if (currentList.BinarySearchItem(resultList.GetItem(i), isTagPrimarySort, sortFieldName, sortOrderList, localIdentityTagNames) < 0)
                    {
                        //Remove item from resultList
                        resultList.RemoveAt(i);
                    }
                }
            }
        }
 /// <summary>
 /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
 /// </summary>
 /// <param name="x">The InternalItem to compare.</param>
 /// <param name="y">The InternalItem to compare.</param>
 /// <returns>
 /// Value
 /// Condition
 /// Less than zero
 /// <paramref name="x"/> is less than <paramref name="y"/>.
 /// Zero
 /// <paramref name="x"/> equals <paramref name="y"/>.
 /// Greater than zero
 /// <paramref name="x"/> is greater than <paramref name="y"/>.
 /// </returns>
 public int Compare(InternalItem x, InternalItem y)
 {
     return(comparer.Compare(x, y));
 }
 public override int Compare(TEnum x, TEnum y)
 {
     return(BaseComparer.Compare(enumToSystem(x), enumToSystem(y)));
 }
Example #20
0
 public override int Compare(TEnumerable x, TEnumerable y)
 {
     if (x == null)
     {
         if (y == null)
         {
             return(0);
         }
         else
         {
             return(-DefaultDirectionMultiplier);
         }
     }
     else
     {
         if (y == null)
         {
             return(DefaultDirectionMultiplier);
         }
         else
         {
             IEnumerator <T> ex = x.GetEnumerator();
             IEnumerator <T> ey = y.GetEnumerator();
             int             i  = 1;
             while (true)
             {
                 bool hasX = ex.MoveNext();
                 bool hasY = ey.MoveNext();
                 if (!hasX)
                 {
                     if (!hasY)
                     {
                         return(0);
                     }
                     else
                     {
                         return(-i * DefaultDirectionMultiplier);
                     }
                 }
                 else
                 {
                     if (!hasY)
                     {
                         return(i * DefaultDirectionMultiplier);
                     }
                     else
                     {
                         int r = BaseComparer.Compare(ex.Current, ey.Current);
                         if (r != 0)
                         {
                             if (r < 0)
                             {
                                 return(-i);
                             }
                             else
                             {
                                 return(i);
                             }
                         }
                     }
                 }
                 i++;
             }
         }
     }
 }
Example #21
0
        public override int Compare(Pair <T> x, Pair <T> y)
        {
            var result = BaseComparer.Compare(x.First, y.First);

            return(result != 0 ? result : BaseComparer.Compare(x.Second, y.Second));
        }
 public override int Compare(TEnum x, TEnum y) => BaseComparer.Compare(EnumToSystem(x), EnumToSystem(y));