当前位置: 首页>>代码示例>>C#>>正文


C# CircularBuffer.Peek方法代码示例

本文整理汇总了C#中CircularBuffer.Peek方法的典型用法代码示例。如果您正苦于以下问题:C# CircularBuffer.Peek方法的具体用法?C# CircularBuffer.Peek怎么用?C# CircularBuffer.Peek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CircularBuffer的用法示例。


在下文中一共展示了CircularBuffer.Peek方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Peek

        public void Peek()
        {
            var buffer = new CircularBuffer<int>(16);

            for(var i=0;i<8;i++)
                buffer.Put(i);

            var peek1 = new int[8];
            var peek2 = new int[8];

            buffer.Peek(peek1, 0, 8);
            buffer.Peek(peek2, 0, 8);

            for(var i = 0; i < 8; i++)
            {
                Assert.AreEqual(
                    peek1[i],
                    peek2[i]);
            }
        }
开发者ID:pjc0247,项目名称:Merona.cs,代码行数:20,代码来源:CircularBuffer.cs

示例2: CircularBufferGetPutSkipTest

        public void CircularBufferGetPutSkipTest()
        {
            {
                CircularBuffer<int> buffer = new CircularBuffer<int>(3, true);
                buffer.Put(1);
                buffer.Put(2);
                buffer.Put(3);
                buffer.Put(4);

                int get1 = buffer.Get();
                int get2 = buffer.Get();

                Assert.AreEqual(get1, 2);
                Assert.AreEqual(get2, 3);
            }
            {
                CircularBuffer<int> buffer = new CircularBuffer<int>(3, true);
                buffer.Put(1);
                buffer.Put(2);
                buffer.Put(3);
                buffer.Put(4);
                buffer.Put(5);

                int get1 = buffer.Peek(); // 3
                int get2 = buffer.Peek(0); // 3
                int get3 = buffer.Peek(1); // 4
                int get4 = buffer.Peek(2); // 5

                Assert.AreEqual(get1, 3);
                Assert.AreEqual(get2, 3);
                Assert.AreEqual(get3, 4);
                Assert.AreEqual(get4, 5);

                List<int> getRange = buffer.PeekRange(0);
                Assert.AreEqual(getRange.Count, 3);
                Assert.AreEqual(getRange[0], 3);
                Assert.AreEqual(getRange[1], 4);
                Assert.AreEqual(getRange[2], 5);
            }
        }
开发者ID:HongSeokHwan,项目名称:legacy,代码行数:40,代码来源:CircularBufferTest.cs

示例3: GetNearestIndex

        public static int GetNearestIndex(CircularBuffer<MarketDataPair> data, DateTime targetDateTime)
        {
            int leftPivot = 0;
            int rightPivot = data.Size;
            int mid = leftPivot;

            while (true)
            {
                if (leftPivot == rightPivot)
                {
                    Trace.Assert(leftPivot == mid);
                    return mid;
                }

                int curMid = (leftPivot + rightPivot) / 2;

                if (curMid == mid)
                {
                    return mid;
                }

                mid = curMid;

                MarketDataPair midPair = data.Peek(mid);

                if (midPair.CurDateTime == targetDateTime)
                {
                    break;
                }
                else if (midPair.CurDateTime > targetDateTime)
                {
                    rightPivot = mid;
                }
                else
                {
                    leftPivot = mid;
                }
            }

            return mid;
        }
开发者ID:HongSeokHwan,项目名称:legacy,代码行数:41,代码来源:CircularBufferUtil.cs

示例4: Deserialize

        public Packet Deserialize(CircularBuffer<byte> buffer)
        {
            try
            {
                var bytes = new byte[buffer.Size];
                buffer.Peek(bytes, 0, buffer.Size);

                var json = Encoding.UTF8.GetString(bytes);

                var stringReader = new StringReader(json);
                var reader = new JsonTextReader(stringReader);

                // 주의
                var field =
                    stringReader.GetType().GetField(
                        "_pos",
                        System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

                var depth = 0;
                var id = 0;
                var idField = true;
                var hasObject = false;

                try
                {
                    while (reader.Read())
                    {
                        switch (reader.TokenType)
                        {
                            case JsonToken.StartObject:
                                depth++;
                                hasObject = true;
                                break;
                            case JsonToken.EndObject:
                                depth--;

                                if (depth == 0)
                                    break;
                                break;

                            case JsonToken.PropertyName:
                                if (depth == 1 &&
                                    (String)reader.Value == "id")
                                    idField = true;
                                break;
                            case JsonToken.Integer:
                                if (idField)
                                {
                                    id = Convert.ToInt32(reader.Value);
                                    idField = false;
                                }
                                break;
                        }
                    }
                }
                catch (JsonReaderException e)
                {
                    // ignore
                }


                if (hasObject == false)
                    return null;
                if (depth != 0)
                    return null;

                var count = (int)field.GetValue(stringReader);
                buffer.Skip(count);

                var slicedJson = json.Substring(0, count);

                var packet =
                    (Packet)JsonConvert.DeserializeObject(
                        slicedJson, Packet.GetTypeById(id));

                packet.packetId = id;

                return packet;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return null;
        }
开发者ID:pjc0247,项目名称:Merona.JsonProtocol.cs,代码行数:85,代码来源:JsonMarshaler.cs


注:本文中的CircularBuffer.Peek方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。