Пример #1
0
        private RangeValue GetRange(int column, int row)
        {
            if (row >= this.values.Count)
            {
                return(null);
            }

            var flatIndex = RangeValue.ConvertIndex(column, row, this.columns, this.rows);

            // row ranges
            var rowRanges = this.values[row];

            //// the range index
            //var rangeIndex = rowRanges.BinarySearch(new RangeValue(0, flatIndex, flatIndex, this.columns, this.rows));

            //if (rangeIndex < 0) rangeIndex = ~rangeIndex;

            var rangeIndexes = new[] { rowRanges.Count - 1, rowRanges.Count - 2 };

            foreach (var index in rangeIndexes)
            {
                if (index < 0)
                {
                    continue;
                }
                var possibleRange = rowRanges[index];
                if (possibleRange.Contains(column, row))
                {
                    return(possibleRange);
                }
            }


            return(null);
        }
Пример #2
0
        /// <summary>
        /// Memorize the value with given key
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Memorize(Tuple <int, int> key, int value)
        {
            // check bounds
            if (!this.InBound(key))
            {
                return(false);
            }

            // get indexes of key
            var column = key.Item1;
            var row    = key.Item2;

            // get flat index
            var flatIndex = RangeValue.ConvertIndex(column, row, this.columns, this.rows);

            if (this.values[row] == null)
            {
                // add range list for row
                this.values[row] = new List <RangeValue>();
            }
            // the row
            var rowRanges = this.values[row];

            //// get index of certain range
            //var index = rowRanges.BinarySearch(new RangeValue(0, flatIndex, flatIndex, 0, 0));

            //// reverse index
            //if (index < 0) index = ~index;
            var index   = 1;
            var indexes = new[] { index - 1, index };

            foreach (var idx in indexes)
            {
                if (idx < 0)
                {
                    continue;
                }

                if (idx == rowRanges.Count)
                {
                    rowRanges.Insert(idx, new RangeValue(value, flatIndex, flatIndex, this.columns, this.rows));
                    return(true);
                }

                var range = rowRanges[idx];
                if (range.CanContinueWith(column, row) && range.Value == value)
                {
                    this.values[row][idx].EndIndex++;
                    return(true);
                }
            }


            return(false);
        }