Example #1
0
        // non-static methods
        int IComparer.Compare(object first, object second)
        {
            if (first is int == false || second is string == false)
            {
                throw new ArgumentException("In ComparerForBinarySearch.Compare(object, object): invalid argument(s)");
            }

            int index = (int)first;
            string valueToCompare = (string)second;

            Record record = new Record(path, strPositions[index]);

            if (value == ComparingValues.name)
            {
                return record.name.CompareTo(valueToCompare);
            }
            else if (value == ComparingValues.surname)
            {
                return record.surname.CompareTo(valueToCompare);
            }
            else
            {
                return record.id.CompareTo(valueToCompare);
            }
        }
        private void SetGetSurnamesDelegate()
        {
            GetSurnames = delegate(ref List<Record> storage, string valueToCompare, int binarySearchIndex)
            {
                Record recordBuffer;

                while (binarySearchIndex > 0)
                {
                    recordBuffer = new Record(path, positions[sortedSurnames[binarySearchIndex - 1]]);
                    if (recordBuffer.surname != valueToCompare)
                    {
                        break;
                    }
                    --binarySearchIndex;
                }

                while (binarySearchIndex < sortedSurnames.Length)
                {
                    recordBuffer = new Record(path, positions[sortedSurnames[binarySearchIndex]]);

                    if (recordBuffer.surname != valueToCompare)
                    {
                        break;
                    }
                    else
                    {
                        storage.Add(recordBuffer);
                        ++binarySearchIndex;
                    }
                }
            };
        }
        private void SetGenerateKeyDelegate()
        {
            GenerateKey = delegate(ComparingValues valuesToCompare)
            {
                int[] valuesToSort = new int[positions.Count];

                for (int i = 0; i < valuesToSort.Length; ++i)
                {
                    valuesToSort[i] = i;
                }

                Comparison<int> sortByValue = delegate(int a, int b)
                {
                    Record person1 = new Record(path, positions[a]);
                    Record person2 = new Record(path, positions[b]);

                    if (valuesToCompare == ComparingValues.name)
                    {
                        return person1.name.CompareTo(person2.name);
                    }
                    else if (valuesToCompare == ComparingValues.surname)
                    {
                        return person1.surname.CompareTo(person2.surname);
                    }
                    else
                        return person1.id.CompareTo(person2.id);
                };

                Array.Sort(valuesToSort, sortByValue);

                if (valuesToCompare == ComparingValues.name)
                {
                    sortedNames = valuesToSort;
                }
                else if (valuesToCompare == ComparingValues.surname)
                {
                    sortedSurnames = valuesToSort;
                }
                else
                    sortedIDs = valuesToSort;
            };
        }