コード例 #1
0
        public void Deconstruct_Returns_Item_And_Index()
        {
            var indexedItem = new ItemWithIndex <int>(42, 5);

            var(item, index) = indexedItem;
            item.Should().Be(42);
            index.Should().Be(5);
        }
コード例 #2
0
 static string ConvertLetter(ItemWithIndex <string> s)
 {
     if (s.Item == ".")
     {
         return("_");
     }
     if (s.Item.ToLower() == s.Item)
     {
         return(s.Item);
     }
     if (s.Index != 0)
     {
         return("_" + s.Item.ToLower());
     }
     return(s.Item.ToLower());
 }
コード例 #3
0
        public static ItemWithIndex <T>?BinarySearch <T>(this IList <T> collection, Func <ItemWithIndex <T>, int> getDirection)
        {
            var left  = 0;
            var right = collection.Count - 1;
            var mid   = 0;

            while (left < right)
            {
                mid = (left + right) / 2;
                var item = collection[mid];

                var point = new ItemWithIndex <T>
                {
                    Item  = item,
                    Index = mid,
                };

                var direction = getDirection(point);

                if (direction > 0)
                {
                    left = mid + 1;
                }
                else if (direction == 0)
                {
                    return(point);
                }
                else
                {
                    right = mid;
                }
            }

            if (mid == left)
            {
                return(null);
            }

            var lastPoint = new ItemWithIndex <T>
            {
                Item  = collection[left],
                Index = left,
            };

            return(getDirection(lastPoint) == 0 ? lastPoint : (ItemWithIndex <T>?)null);
        }
コード例 #4
0
 static bool HaveSameIndexAndItem <T>(ItemWithIndex <T> x, ItemWithIndex <T> y)
 {
     return(x.Index == y.Index && EqualityComparer <T> .Default.Equals(x.Item, y.Item));
 }