コード例 #1
0
ファイル: OneBasedArray.cs プロジェクト: ilusi0n/CsharpExtras
        public void PairAndExecute <TOther>(IOneBasedArray <TOther> other, Action <TVal, TOther> pairProcessor)
        {
            int zipLength = Math.Min(Length, other.Length);

            for (int i = 1; i <= zipLength; i++)
            {
                pairProcessor(this[i], other[i]);
            }
        }
コード例 #2
0
        public void GivenOneBasedArrayWhenMapAppliedThenResultIsArrayOfMappedValues()
        {
            IOneBasedArray <int>    intArr    = GenTestIntArray();
            const string            Even      = "Even";
            const string            Odd       = "Odd";
            IOneBasedArray <string> resultArr = intArr.Map(i => i % 2 == 0 ? Even : Odd);

            Assert.AreEqual(resultArr.ZeroBasedEquivalent, new string[] { Odd, Even, Odd, Even, Odd, Even });
        }
コード例 #3
0
        public void GivenTwoOneBasedArraysOfDifferentTypesAndLengthsWhenZippedThenResultIsAsExpected()
        {
            IOneBasedArray <string> stringArr = GenTestStringArray();
            IOneBasedArray <int>    intArr    = GenTestIntArray();

            Assert.AreNotEqual(stringArr.Length, intArr.Length,
                               "Given: Test arrays should be different legnths to begin with. Equal length arrays does not propely capture this test case.");
            Func <int, string, (int, string)> zipper = (i, s) => (i, s);
            IOneBasedArray <(int, string)>    zipped = intArr.ZipArray <string, (int, string)>(zipper, stringArr);

            (int, string)[] zeroBasedZipped = zipped.ZeroBasedEquivalent;
コード例 #4
0
        public void Given_2DArray_When_SlicedIntoColumns_Then_AllColumnsHaveCorrectLength()
        {
            IOneBasedArray2D <string> array = new OneBasedArray2DImpl <string>(new string[, ] {
                { "1", "2" }, { "a", "b" }, { "5", "6" }
            });

            for (int col = 1; col <= array.GetLength(1); col++)
            {
                IOneBasedArray <string> colSlice = array.SliceColumn(col);
                Assert.AreEqual(3, colSlice.Length, "Sliced column should have correct length");
            }
        }
コード例 #5
0
        public void Given_2DArray_When_SlicedIntoRows_Then_AllRowsHaveCorrectLength()
        {
            IOneBasedArray2D <string> array = new OneBasedArray2DImpl <string>(new string[, ] {
                { "1", "2" }, { "a", "b" }, { "5", "6" }
            });

            for (int row = 1; row <= array.GetLength(0); row++)
            {
                IOneBasedArray <string> rowSlice = array.SliceRow(row);
                Assert.AreEqual(2, rowSlice.Length, "Sliced row should have correct length");
            }
        }
コード例 #6
0
        public void GivenOneBasedArrayWhenPairAndExecuteSumProductOnSelfThenSumOfSquaresResults()
        {
            IOneBasedArray <int> intArr = GenTestIntArray();
            int totalSum = 0;
            Action <int, int> sumProdAccumulator = (i, j) => totalSum += i * j;

            intArr.PairAndExecute <int>(intArr, sumProdAccumulator);
            //Pyramidal number formula used below to calculate sum of squares: https://en.wikipedia.org/wiki/Square_pyramidal_number
            const int N = 6;

            Assert.AreEqual(N * N * N / 3 + N * N / 2 + N / 6, totalSum);
        }
コード例 #7
0
        private void AddResultsForFindInverseOfOneBasedArray(IDictionary <string, string> dict)
        {
            string[] zeroBasedBackingArray                = new string[] { "One", "Two", "Buzz", "Four", "Five", "Buzz" };
            IOneBasedArray <string>            array      = CsharpExtrasApi.NewOneBasedArray(zeroBasedBackingArray);
            IDictionary <string, IList <int> > inverseMap = array.InverseMap();
            StringBuilder sb = new StringBuilder("[ ");

            foreach ((string key, IList <int> indices) in inverseMap)
            {
                sb.Append("(").Append(key).Append(" -> ").Append(indices[0]);
                for (int i = 1; i < indices.Count; i++)
                {
                    sb.Append(", ").Append(indices[i]);
                }
                sb.Append(") ");
            }
            sb.Append("]");
            string resultString = sb.ToString();

            dict.Add("One-based inverse", resultString);
        }
コード例 #8
0
 public void WriteToColumn(IOneBasedArray <TVal> values, int column, int offset)
 {
     ZeroBasedEquivalent.WriteToColumn(values.ZeroBasedEquivalent, column - 1, offset);
 }
コード例 #9
0
 public void WriteToRow(IOneBasedArray <TVal> values, int row, int offset)
 {
     ZeroBasedEquivalent.WriteToRow(values.ZeroBasedEquivalent, row - 1, offset);
 }
コード例 #10
0
ファイル: OneBasedArray.cs プロジェクト: ilusi0n/CsharpExtras
 public IOneBasedArray <TVal> ZipMulti <TOther>(Func <TVal, TOther, TVal> zipper, IOneBasedArray <TOther> other, params IOneBasedArray <TOther>[] extras)
 {
     TOther[][] extrasZeroBased = extras.Map(arr => arr.ZeroBasedEquivalent);
     TVal[]     zippedZeroBased = ZeroBasedEquivalent.ZipMulti(zipper, other.ZeroBasedEquivalent, extrasZeroBased);
     return(new OneBasedArrayImpl <TVal>(zippedZeroBased));
 }
コード例 #11
0
ファイル: OneBasedArray.cs プロジェクト: ilusi0n/CsharpExtras
 public IOneBasedArray <TResult> ZipArray <TOther1, TOther2, TOther3, TResult>(Func <TVal, TOther1, TOther2, TOther3, TResult> zipper, IOneBasedArray <TOther1> other1, IOneBasedArray <TOther2> other2, IOneBasedArray <TOther3> other3)
 {
     TResult[] zippedZeroBased = ZeroBasedEquivalent.ZipArray(zipper, other1.ZeroBasedEquivalent, other2.ZeroBasedEquivalent, other3.ZeroBasedEquivalent);
     return(new OneBasedArrayImpl <TResult>(zippedZeroBased));
 }
コード例 #12
0
ファイル: OneBasedArray.cs プロジェクト: ilusi0n/CsharpExtras
 public IOneBasedArray <TResult> ZipArray <TOther, TResult>(Func <TVal, TOther, TResult> zipper, IOneBasedArray <TOther> other)
 {
     TResult[] zippedZeroBased = ZeroBasedEquivalent.ZipArray(zipper, other.ZeroBasedEquivalent);
     return(new OneBasedArrayImpl <TResult>(zippedZeroBased));
 }
コード例 #13
0
ファイル: OneBasedArray.cs プロジェクト: ilusi0n/CsharpExtras
 public IMultiValueMap <TVal, TOther> ZipToMultiValueMap <TOther>(IOneBasedArray <TOther> other)
 {
     return(ZeroBasedEquivalent.ZipToMultiValueMap(other.ZeroBasedEquivalent));
 }
コード例 #14
0
ファイル: OneBasedArray.cs プロジェクト: ilusi0n/CsharpExtras
 public IDictionary <TVal, TOther> ZipToDictionary <TOther>(IOneBasedArray <TOther> other)
 {
     return(ZeroBasedEquivalent.ZipToDictionary(other.ZeroBasedEquivalent));
 }