Esempio n. 1
0
 public void NewSample(float sample)
 {
     ++lastFFT;
     signal.Add(sample);
     if (lastFFT > FFTPeriod)
     {
         CalculateFFT();
     }
 }
        public void EnumerationOrderWithLessThanCapacityItemsIsCorrect()
        {
            int[] array = new int[] { 7, 5, 6 };
            CircularArray <int> carray = new CircularArray <int>(5, false);

            // Add all items from normal array to circular array.
            for (int i = 0; i < array.Length; i++)
            {
                carray.Add(array[i], out int oldItem);
            }

            // Create a new array from enumeration over circular array.
            int index = 0;

            int[] arrayCopy = new int[array.Length];
            foreach (int item in carray)
            {
                arrayCopy[index++] = item;
            }

            // Check that each item from the enumeration was in correct order.
            for (int i = 0; i < array.Length; i++)
            {
                Assert.Equal(array[i], arrayCopy[i]);
            }
        }
Esempio n. 3
0
 public static void testCircularArray()
 {
   CircularArray<String> cArray = new CircularArray<String>(0);
   cArray.Add("A");
   cArray.Add("B");
   cArray.Add("C");
   cArray.Add("D");
   cArray.Add("E");
   cArray.Add("F");
   cArray.Add("G");
   cArray.Add("H");
   cArray.Add("I");
   Console.WriteLine(cArray.ToString());
 }
            public void Increases_Length_By_1_When_Number_Of_Added_Items_Is_Less_Than_MaximumSize()
            {
                var circularArray = new CircularArray <string>(10);

                // act
                circularArray.Add("1");

                Assert.Equal(1, circularArray.Length);
            }
 private void AddLines(int numberOfLines)
 {
     using (StreamReader Reader = new StreamReader(GetTestStream()))
     {
         for (int i = 0; i < numberOfLines; i++)
         {
             arrayStringBuffer.Add(Reader.ReadLine());
         }
     }
 }
            public void Keeps_Length_At_MaximumSize_When_Number_Of_Added_Items_Equals_MaximumSize()
            {
                var circularArray = new CircularArray <string>(10);

                circularArray.Add("1");
                circularArray.Add("2");
                circularArray.Add("3");
                circularArray.Add("4");
                circularArray.Add("5");
                circularArray.Add("6");
                circularArray.Add("7");
                circularArray.Add("8");
                circularArray.Add("9");

                // act
                circularArray.Add("10");

                Assert.Equal(10, circularArray.Length);
            }
            public void Returns_Last_Added_Value_When_Number_Of_Added_Items_Equals_MaximumSize()
            {
                var circularArray = new CircularArray <string>(10);

                circularArray.Add("1");
                circularArray.Add("2");
                circularArray.Add("3");
                circularArray.Add("4");
                circularArray.Add("5");
                circularArray.Add("6");
                circularArray.Add("7");
                circularArray.Add("8");
                circularArray.Add("9");
                circularArray.Add("10");

                // act
                string tail = circularArray.GetTail();

                Assert.Equal("10", tail);
            }
Esempio n. 8
0
        public void IndexerAssignment()
        {
            var array = new CircularArray <string>(3);

            array[2] = "assigned";
            CheckEnumerator(array, new[] { null, null, "assigned" });
            array.Add("pusher");
            CheckEnumerator(array, new[] { "pusher", null, null });
            array[2] = "assigned";
            CheckEnumerator(array, new[] { "pusher", null, "assigned" });
        }
        /// <inheritdoc />
        public bool AddTimeData(IPAddress peerAddress, TimeSpan offsetSample, bool isInboundConnection)
        {
            bool res = false;

            bool startWarningLoopNow = false;
            lock (this.lockObject)
            {
                if (!this.SwitchedOff)
                {
                    HashSet<IPAddress> sources = isInboundConnection ? this.inboundSampleSources : this.outboundSampleSources;
                    bool alreadyIncluded = sources.Contains(peerAddress);
                    if (!alreadyIncluded)
                    {
                        sources.Add(peerAddress);

                        CircularArray<TimestampOffsetSample> samples = isInboundConnection ? this.inboundTimestampOffsets : this.outboundTimestampOffsets;

                        var newSample = new TimestampOffsetSample
                        {
                            Source = peerAddress,
                            TimeOffset = offsetSample
                        };

                        TimestampOffsetSample oldSample;
                        if (samples.Add(newSample, out oldSample))
                        {
                            // If we reached the maximum number of samples, we need to remove oldest sample.
                            sources.Remove(oldSample.Source);
                            this.logger.LogTrace("Oldest sample {0} from peer '{1}' removed.", oldSample.TimeOffset, oldSample.Source);
                        }

                        this.RecalculateTimeOffsetLocked();

                        // If SwitchedOffLimitReached is set, timeOffset is set to zero,
                        // so we need to check both conditions here.
                        if (!this.IsSystemTimeOutOfSync
                            && ((Math.Abs(this.timeOffset.TotalSeconds) > TimeOffsetWarningThresholdSeconds) || this.SwitchedOffLimitReached))
                        {
                            startWarningLoopNow = true;
                            this.IsSystemTimeOutOfSync = true;
                        }

                        res = true;
                    }
                    else this.logger.LogTrace("Sample from peer '{0}' is already included.", peerAddress);
                }
                else this.logger.LogTrace("Time sync feature is switched off.");
            }

            if (startWarningLoopNow)
                this.StartWarningLoop();

            return res;
        }
Esempio n. 10
0
        protected override float Next(float[] input)
        {
            prevs.Add(input[VALUES]);
            float result = 0;

            for (int i = 0; i < prevs.Length; ++i)
            {
                result += kernel[i] * prevs[i];
            }
            return(result);
        }
            public void Returns_An_Array_Where_Last_Item_Equals_The_Last_Item_Added_When_Less_Than_Or_Equal_Too_MaximumSize_Items_Are_Added()
            {
                var circularArray = new CircularArray <string>(10);

                circularArray.Add("1");
                circularArray.Add("2");
                circularArray.Add("3");
                circularArray.Add("4");
                circularArray.Add("5");
                circularArray.Add("6");
                circularArray.Add("7");
                circularArray.Add("8");
                circularArray.Add("9");
                circularArray.Add("10");

                // act
                var array = circularArray.GetArray();

                Assert.Equal("10", array[9]);
            }
            public void Returns_Last_Added_Value()
            {
                var circularArray = new CircularArray <string>(10);

                circularArray.Add("1");

                // act
                string tail = circularArray.GetTail();

                Assert.Equal("1", tail);
            }
Esempio n. 13
0
            public void Update()
            {
                uint newGlobal = valueGetterFunction();

                if (newGlobal >= globalValue)
                {
                    previousGlobals.Add(newGlobal - globalValue);
                }

                foreach (KeyValuePair <string, CircularArray> pair in previousEvents)
                {
                    if (pair.Value.IsIgnored)
                    {
                        ignoredKeys.Add(pair.Key);
                    }
                    else
                    {
                        pair.Value.Ignore();
                    }
                }
                foreach (string key in ignoredKeys)
                {
                    previousEvents.Remove(key);
                }
                foreach (KeyValuePair <string, uint> pair in completedEvents)
                {
                    CircularArray array;
                    if (previousEvents.TryGetValue(pair.Key, out array))
                    {
                        array.Add(pair.Value);
                        previousEvents[pair.Key] = array;
                    }
                    else
                    {
                        previousEvents[pair.Key] = new CircularArray(pair.Value);
                    }
                }
                completedEvents.Clear();
                ignoredKeys.Clear();

                // check mismanagement
                if (activeEvents.Count != 0)
                {
                    string events = "";
                    foreach (string key in activeEvents.Keys)
                    {
                        events += key + '|';
                    }
                    events = events.Substring(0, events.Length - 1);
                    throw new Exception("Mismanaged \"" + TRACKER_NAMES[trackedValue] + "\" statistics: " + events);                    // happens when begin() and end() don't correspond
                }
                globalValue = newGlobal;
            }
Esempio n. 14
0
    public void Add(float val)
    {
        if (buffer.IsFull())
        {
            avg -= buffer.GetAt(0);
        }

        var newVal = val / size;

        buffer.Add(newVal);
        avg += newVal;
    }
            public void Returns_An_Array_Of_Length_Equal_To_Number_Of_Added_Items_When_Less_Than_Or_Equal_Too_MaximumSize_Items_Are_Added()
            {
                var circularArray = new CircularArray <string>(10);

                circularArray.Add("1");
                circularArray.Add("2");
                circularArray.Add("3");
                circularArray.Add("4");
                circularArray.Add("5");
                circularArray.Add("6");
                circularArray.Add("7");
                circularArray.Add("8");

                // act
                var array = circularArray.GetArray();

                Assert.Equal(8, array.Length);
            }
    public override bool Update(T data)
    {
        buffer.Add(data);

        hasNext = buffer.IsFull();
        if (hasNext)
        {
            //dynamic temp = processor.Invoke(buffer);
            //total = (U)(total + temp);
            var temp = processor.Invoke(buffer);
            total = accumulator.Invoke(total, temp);

            totalCount++;
            next = temp;
        }

        return(hasNext);
    }
Esempio n. 17
0
        public RunningDFT(int size)
        {
            if (size % 2 != 0)
            {
                throw new ArgumentException("Size of a RunningDFT must be even.");
            }
            Size    = size;
            DFTSize = Size / 2 + 1;

            real = new List <float>(DFTSize);
            for (int k = 0; k < DFTSize; ++k)
            {
                real.Add(0);
            }

            realData = new CircularArray <List <float> >(size);
            for (int i = 0; i < Size; ++i)
            {
                realData.Add(new List <float>(DFTSize));
                for (int k = 0; k < DFTSize; ++k)
                {
                    realData.First().Add(0);
                }
            }

            imaginary = new List <float>(DFTSize);
            for (int k = 0; k < DFTSize; ++k)
            {
                imaginary.Add(0);
            }

            imaginaryData = new CircularArray <List <float> >(size);
            for (int i = 0; i < Size; ++i)
            {
                imaginaryData.Add(new List <float>(DFTSize));
                for (int k = 0; k < DFTSize; ++k)
                {
                    imaginaryData.First().Add(0);
                }
            }
            i = -1;
        }
        public void AddAndEnumerationWithMoreThanCapacityItemsWorksCorrectly()
        {
            CircularArray <int> carray = new CircularArray <int>(5, false);

            carray.Add(10, out int oldItem);
            carray.Add(20, out oldItem);
            carray.Add(30, out oldItem);
            carray.Add(40, out oldItem);
            carray.Add(50, out oldItem);
            carray.Add(60, out oldItem);

            int sum = 0;

            foreach (int item in carray)
            {
                sum += item;
            }

            Assert.Equal(20 + 30 + 40 + 50 + 60, sum);
        }
        public void AddReplacementsWorkCorrectly()
        {
            CircularArray <IPHostEntry> carray = new CircularArray <IPHostEntry>(5);

            IPHostEntry oldItem;
            bool        replaced = carray.Add(new IPHostEntry {
                HostName = "a"
            }, out oldItem);

            Assert.False(replaced);

            replaced = carray.Add(new IPHostEntry {
                HostName = "b"
            }, out oldItem);
            Assert.False(replaced);

            replaced = carray.Add(new IPHostEntry {
                HostName = "c"
            }, out oldItem);
            Assert.False(replaced);

            string nameSum = "";

            foreach (IPHostEntry item in carray)
            {
                nameSum += item.HostName;
            }

            Assert.Equal("abc", nameSum);

            replaced = carray.Add(new IPHostEntry {
                HostName = "d"
            }, out oldItem);
            Assert.False(replaced);

            replaced = carray.Add(new IPHostEntry {
                HostName = "e"
            }, out oldItem);
            Assert.False(replaced);

            replaced = carray.Add(new IPHostEntry {
                HostName = "f"
            }, out oldItem);
            Assert.True(replaced);

            replaced = carray.Add(new IPHostEntry {
                HostName = "g"
            }, out oldItem);
            Assert.True(replaced);

            nameSum = "";
            foreach (IPHostEntry item in carray)
            {
                nameSum += item.HostName;
            }

            Assert.Equal("cdefg", nameSum);

            replaced = carray.Add(new IPHostEntry {
                HostName = "g"
            }, out oldItem);
            Assert.True(replaced);
            Assert.Equal("c", oldItem.HostName);
        }
Esempio n. 20
0
            public void Returns_An_Array_Where_First_Item_Equals_The_Item_Added_MaximumSize_Items_Ago_When_More_Items_Than_MaximumSize_Are_Added()
            {
                var circularArray = new CircularArray <string>(10);

                circularArray.Add("1");
                circularArray.Add("2");
                circularArray.Add("3");
                circularArray.Add("4");
                circularArray.Add("5");
                circularArray.Add("6");
                circularArray.Add("7");
                circularArray.Add("8");
                circularArray.Add("9");
                circularArray.Add("10");
                circularArray.Add("11");

                // Act
                var array = circularArray.GetArray();

                Assert.Equal("2", array[0]);
            }
            public void Removes_All_Buckets()
            {
                var circularArray = new CircularArray <string>(10);

                circularArray.Add("1");
                circularArray.Add("2");
                circularArray.Add("3");
                circularArray.Add("4");
                circularArray.Add("5");
                circularArray.Add("6");
                circularArray.Add("7");
                circularArray.Add("8");
                circularArray.Add("9");
                circularArray.Add("10");
                circularArray.Add("11");

                // act
                circularArray.Clear();

                Assert.Equal(0, circularArray.Length);
            }
            public void Returns_An_Array_Of_Length_MaximumSize_When_More_Items_Than_MaximumSize_Are_Added()
            {
                var circularArray = new CircularArray <string>(10);

                circularArray.Add("1");
                circularArray.Add("2");
                circularArray.Add("3");
                circularArray.Add("4");
                circularArray.Add("5");
                circularArray.Add("6");
                circularArray.Add("7");
                circularArray.Add("8");
                circularArray.Add("9");
                circularArray.Add("10");
                circularArray.Add("11");

                // act
                var array = circularArray.GetArray();

                Assert.Equal(10, array.Length);
            }