コード例 #1
0
        /// <summary>
        /// Moves the enumerator that is assumed to be sorted by departure time to the first connection with a departure time larger than or equal to the given departure time.
        /// </summary>
        public static bool MoveToDepartureTime(this ConnectionsDb.Enumerator enumerator,
                                               uint departureTime)
        {
            if (enumerator == null)
            {
                throw new ArgumentNullException("enumerator");
            }

            if (enumerator.Count == 0)
            { // not possible to search and there's no need.
                return(false);
            }

            // do a binary search.
            uint lower = 0;
            uint upper = enumerator.Count - 1;

            // check bound.
            enumerator.MoveTo(upper);
            if (enumerator.DepartureTime < departureTime)
            { // no connection with a departure time after the given departure time.
                return(false);
            }

            // check lowest.
            enumerator.MoveTo(lower);
            if (enumerator.DepartureTime >= departureTime)
            { // the first connection is already the one with a departure time after the given departure time.
                return(true);
            }

            while (upper - lower > 1)
            {
                var position = (uint)((lower + upper) / 2);
                enumerator.MoveTo(position);
                if (enumerator.DepartureTime >= departureTime)
                { // position >= than target position.
                    upper = position;
                }
                else
                { // position > than target position.
                    lower = position;
                }
            }

            enumerator.MoveTo(upper);
            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Moves the given enumerator to the connection right before the current connection on the same trip.
        /// </summary>
        /// <remarks>This requires the connections to be sorted by departure time.</remarks>
        /// <returns></returns>
        public static bool MoveToPreviousConnection(this ConnectionsDb.Enumerator enumerator)
        {
            if (enumerator == null)
            {
                throw new ArgumentNullException("enumerator");
            }

            if (enumerator.Count == 0)
            { // not possible to search and there's no need.
                return(false);
            }

            var trip = enumerator.TripId;

            while (enumerator.MovePrevious())
            {
                if (enumerator.TripId == trip)
                {
                    return(true);
                }
            }
            return(false);
        }