Exemple #1
0
                static int CompareSendingTime(SendingTimeKey left, SendingTimeKey right)
                {
                    var result = left.SendingTime.CompareTo(right.SendingTime);

                    if (result == 0)
                    {
                        return(left.Index.CompareTo(right.Index));
                    }
                    return(result);
                }
Exemple #2
0
        public void Add(Order order)
        {
            _keyIndex.Add(order.Key, order);

            var key = new SendingTimeKey
            {
                SendingTime = order.SendingTime,
                Index       = _nextIndex++
            };

            _sendingTimeIndex.Add(key, order);
        }
Exemple #3
0
        SendingTimeKey[] GetKeysForRange(DateTime from, DateTime to)
        {
            var keys = _sendingTimeIndex.Keys.ToArray();
            // Array.BinarySearch returns
            // The index of the value if it is found.
            // A negative value if the value is not found.
            //  - The bitwise complement of the index of the first element that is larger than the value.
            //  - If there is no larger value it will the bitwise complement of the size of the array.
            var fromKey = new SendingTimeKey
            {
                SendingTime = from,
                Index       = 0 // Find the first key with this SendingTime
            };

            var lower = Array.BinarySearch(keys, fromKey, new KeyComparer());

            if (lower == ~keys.Length)
            {
                return(Array.Empty <SendingTimeKey>());
            }

            if (lower < 0)
            {
                lower = ~lower;
            }

            var toKey = new SendingTimeKey
            {
                SendingTime = to,
                Index       = int.MaxValue // Find the last key with this SendingTime
            };

            var upper = Array.BinarySearch(keys, lower, keys.Length - lower, toKey, new KeyComparer());

            if (upper < 0)
            {
                upper = ~upper;
            }

            var result = new SendingTimeKey[upper - lower];

            Array.Copy(keys, lower, result, 0, upper - lower);
            return(result);
        }