Пример #1
0
        public List <int> Solution(int[][] array)
        {
            var iterators = new Queue <ArrayIterator>();
            var heap      = new MinHeap <int>();

            for (var i = 0; i < array.Length; i++)
            {
                var iterator = new ArrayIterator(array[i]);

                iterators.Enqueue(iterator);
                var next = iterator.Next();
                heap.Push(next, next);
            }

            var result = new List <int>();

            while (heap.Count > 0)
            {
                if (iterators.Count > 0)
                {
                    var iterator = iterators.Dequeue();
                    var next     = iterator.Next();
                    heap.Push(next, next);

                    if (iterator.HasNext())
                    {
                        iterators.Enqueue(iterator);
                    }
                }

                result.Add(heap.Pop().Data);
            }

            return(result);
        }
Пример #2
0
    public void PushHeap()
    {
        ArrayIterator <int> pushHeapIt = FourTwoThreeOne.Begin().Copy(
            FourTwoThreeOne.End(),
            arrLong.Begin());

        pushHeapIt.SetCurrent(5);
        pushHeapIt = pushHeapIt.GetNext();
        arrLong.Begin().PushHeap(pushHeapIt, IsIntLessThanInt);
        Assert.That(arrLong, Is.EqualTo(new [] { 5, 4, 3, 1, 2, 0, 0, 0 }));
    }
Пример #3
0
    public void InplaceMerge()
    {
        ArrayIterator <int> copyResult = arr.Begin().Copy(
            arr.End(),
            arrLong.Begin());

        OneThreeThreeFour.Begin().Copy(OneThreeThreeFour.End(), copyResult);
        arrLong.Begin().InplaceMerge(
            copyResult,
            arrLong.End(),
            IsIntLessThanInt);
        Assert.That(arrLong, Is.EqualTo(new [] { 1, 1, 2, 2, 3, 3, 3, 4 }));
    }
Пример #4
0
        private Array ArraySetElementValue(int[] lengths, StreamExtractorHandler streamExtractor) //заполнение элементов массива значениями
        {
            IConcreteAction action     = MakeActionForElementType();
            var             arrayIndex = new ArrayIterator(lengths);

            for (int i = 0; i < array.Length; i++)
            {
                object elementValue = action.Deserialize(streamExtractor);
                int[]  indices      = arrayIndex.GetNext();
                array.SetValue(elementValue, indices);
            }
            return(array);
        }
Пример #5
0
        private void DeserealizeElement(int[] lengths)
        {
            IConcreteAction action         = MakeActionForElementType();
            var             arrayIndexator = new ArrayIterator(lengths);

            for (int i = 0; i < array.Length; i++)
            {
                int[]  index     = arrayIndexator.GetNext();
                object arrayItem = array.GetValue(index);

                List <byte> data = action.Serialize(arrayItem);
                resultStream.AddRange(data);
            }
        }
Пример #6
0
        public void MultiDimensionalArray()
        {
            int[,,] multiArray  = new int[2, 3, 4];
            multiArray[1, 2, 3] = 7698;
            multiArray[0, 1, 1] = 278;
            multiArray[0, 0, 0] = 325;
            byte[] data = BytesStream.Serialize(multiArray);
            int[,,] decod = BytesStream.Deserialize <int[, , ]>(data);

            Assert.AreEqual(multiArray.Length, decod.Length);
            var arrayIndex = new ArrayIterator(new int[] { 2, 3, 4 });

            for (int i = 0; i < multiArray.Length; i++)
            {
                int[] indices = arrayIndex.GetNext();
                Assert.AreEqual(multiArray.GetValue(indices), decod.GetValue(indices));
            }
        }
Пример #7
0
 public Iterator <N> Iterator()
 {
     if (iterator1 == null)
     {
         iterator1 = new ArrayIterator <N>(array, allowRemove);
         iterator2 = new ArrayIterator <N>(array, allowRemove);
     }
     if (!iterator1.valid)
     {
         iterator1.index = 0;
         iterator1.valid = true;
         iterator2.valid = false;
         return(iterator1);
     }
     iterator2.index = 0;
     iterator2.valid = true;
     iterator1.valid = false;
     return(iterator2);
 }
Пример #8
0
        public void Test1()
        {
            int[][] input = new int[4][];
            input[0] = new int[] { 1, 2 };
            input[1] = new int[] { 3 };
            input[2] = new int[] {  };
            input[3] = new int[] { 4, 5, 6 };

            ArrayIterator iterator = new ArrayIterator(input);

            Assert.True(iterator.HasNext());
            Assert.True(iterator.Next() == 1);
            Assert.True(iterator.Next() == 2);
            Assert.True(iterator.Next() == 3);
            Assert.True(iterator.Next() == 4);
            Assert.True(iterator.Next() == 5);
            Assert.True(iterator.Next() == 6);
            Assert.True(!iterator.HasNext());
            Assert.Throws <Exception>(() => iterator.Next());
        }
Пример #9
0
        static void Main(string[] args)
        {
            Iterator <int> collection1 = new LinkedListIterator <int>(new Node <int>(1, new Node <int>(2, new Node <int>(3, new Empty <int>()))));

            PrintCollection(collection1);

            Iterator <int> collection2 = new ArrayIterator <int>(new int[] { 9, 10, 11, -1 });

            PrintCollection(collection2);

            int[][] tmp_collection3 = new int[3][];
            tmp_collection3[0] = new int[] { 3, 2, 1 };
            tmp_collection3[1] = new int[] { 2, 1, 3 };
            tmp_collection3[2] = new int[] { 1, 3, 2 };
            Iterator <int> collection3 = new MatrixIterator <int>(tmp_collection3, 3, 3);

            PrintCollection(collection3);

            Iterator <int> collection4 = new NaturalNumbers();

            PrintCollection(collection4);
        }
Пример #10
0
 public IEnumerator <T> GetEnumerator()
 {
     // lastAcquire.getBuffer().setLength(0);
     // new Throwable().printStackTrace(new java.io.PrintWriter(lastAcquire));
     if (iterator1 == null)
     {
         iterator1 = new ArrayIterator(array, allowRemove);
         iterator2 = new ArrayIterator(array, allowRemove);
         // iterator1.iterable = this;
         // iterator2.iterable = this;
     }
     if (!iterator1.valid)
     {
         iterator1.index = 0;
         iterator1.valid = true;
         iterator2.valid = false;
         return(iterator1);
     }
     iterator2.index = 0;
     iterator2.valid = true;
     iterator1.valid = false;
     return(iterator2);
 }
Пример #11
0
    public void Init()
    {
        Game.Dispatcher.AddListener("StartGame", InitializeInstanceData);
        Game.Dispatcher.AddListener("InstanceDataLoaded", LoadInstanceData);

        roundItr = new ArrayIterator("round", Game, (int position) => {
            instance.RoundIndex = position;

            // Reset the pitch and agenda item iterators when a new round begins
            pitchItr.Reset();
            agendaItemItr.Reset();
            SaveData();
        });

        pitchItr = new ArrayIterator("pitch", Game, (int position) => {
            CurrentRound.PitchIndex = position;
            SaveData();
        });

        agendaItemItr = new ArrayIterator("agenda_item", Game, (int position) => {
            CurrentRound.AgendaItemIndex = position;
            SaveData();
        });
    }
Пример #12
0
	public void Init () {

		Game.Dispatcher.AddListener ("StartGame", InitializeInstanceData);
		Game.Dispatcher.AddListener ("InstanceDataLoaded", LoadInstanceData);

		roundItr = new ArrayIterator ("round", Game, (int position) => { 
			instance.RoundIndex = position;

			// Reset the pitch and agenda item iterators when a new round begins
			pitchItr.Reset ();
			agendaItemItr.Reset ();
			SaveData ();
		});

		pitchItr = new ArrayIterator ("pitch", Game, (int position) => { 
			CurrentRound.PitchIndex = position; 
			SaveData ();
		});

		agendaItemItr = new ArrayIterator ("agenda_item", Game, (int position) => { 
			CurrentRound.AgendaItemIndex = position; 
			SaveData ();
		});
	}
Пример #13
0
        public List <int> Solution(int[] input)
        {
            var sortedSubarrays = new List <List <int> >();
            var increasing      = true;
            var startIndex      = 0;

            for (var i = 1; i <= input.Length; i++)
            {
                Console.WriteLine(i);

                if (i == input.Length ||
                    (input[i - 1] < input[i] && !increasing) ||
                    (input[i - 1] >= input[i] && increasing))
                {
                    var newList = new List <int>();

                    if (increasing)
                    {
                        for (var j = startIndex; j < i; j++)
                        {
                            newList.Add(input[j]);
                        }
                    }
                    else
                    {
                        for (var j = i - 1; j >= startIndex; j--)
                        {
                            newList.Add(input[j]);
                        }
                    }

                    sortedSubarrays.Add(newList);

                    startIndex = i;
                    increasing = !increasing;
                }
            }

            var queue = new Queue <ArrayIterator>();
            var heap  = new MinHeap <int>();

            foreach (var sortedSubarray in sortedSubarrays)
            {
                var iterator = new ArrayIterator()
                {
                    Index = 0, List = sortedSubarray
                };
                queue.Enqueue(iterator);
                var next = iterator.Next();
                heap.Push(next, next);
            }

            var result = new List <int>();

            while (heap.Count > 0)
            {
                if (queue.Count > 0)
                {
                    var array = queue.Dequeue();
                    var value = array.Next();

                    heap.Push(value, value);

                    if (array.HasNext())
                    {
                        queue.Enqueue(array);
                    }
                }

                result.Add(heap.Pop().Data);
            }

            return(result);
        }