コード例 #1
0
        private uint _nextConnectionId;  // holds the maximum connection id.

        /// <summary>
        /// Adds a connection.
        /// </summary>
        public uint Add(uint stop1, uint stop2, uint tripId, uint departureTime, uint arrivalTime)
        {
            if (arrivalTime <= departureTime)
            {
                throw new ArgumentException("Departure time must be smaller than arrival time.");
            }
            var duration = arrivalTime - departureTime;

            if (duration > CONNECTION_MAX_DURATION)
            {
                throw new ArgumentException(string.Format("A connection with a duration > {0}s cannot be stored.", CONNECTION_MAX_DURATION));
            }

            var id = _nextConnectionId;

            _nextConnectionId++;

            var size = _connections.Length;

            while ((id * CONNECTION_SIZE + CONNECTION_SIZE) > size)
            {
                size += CONNECTIONS_BLOCK_SIZE;
            }
            if (size != _connections.Length)
            {
                _connections.Resize(size);
            }

            _connections[id * CONNECTION_SIZE + 0] = stop1;
            _connections[id * CONNECTION_SIZE + 1] = stop2;
            _connections[id * CONNECTION_SIZE + 2] = tripId;
            _connections[id * CONNECTION_SIZE + 3] = ConnectionsDb.Encode(departureTime, duration);

            return(id);
        }
コード例 #2
0
 /// <summary>
 /// Creates a new transit db.
 /// </summary>
 public TransitDb()
 {
     _agencyAttributes = new AttributesIndex(AttributesIndexMode.ReverseStringIndex);
     _connectionsDb    = new ConnectionsDb();
     _stopsDb          = new StopsDb();
     _stopAttributes   = new AttributesIndex(AttributesIndexMode.ReverseStringIndex);
     _tripsDb          = new TripsDb();
     _tripAttributes   = new AttributesIndex(AttributesIndexMode.ReverseStringIndex);
     _transfersDbs     = new Dictionary <string, TransfersDb>();
     _schedulesDb      = new SchedulesDb();
     _shapesDb         = new ShapesDb();
 }
コード例 #3
0
        /// <summary>
        /// Deserializes from stream.
        /// </summary>
        public static TransitDb Deserialize(Stream stream)
        {
            var version = stream.ReadByte();

            if (version > 2)
            {
                throw new Exception("Cannot deserialize db, version # doesn't match.");
            }

            // read agencies attributes.
            var agencyAttributes = AttributesIndex.Deserialize(new LimitedStream(stream), true);

            // read connections db.
            var connectionsDb = ConnectionsDb.Deserialize(stream);

            // read schedules db.
            var schedulesDb = SchedulesDb.Deserialize(stream);

            // read stop attributes.
            var stopAttributes = AttributesIndex.Deserialize(new LimitedStream(stream), true);

            // read stop db.
            var stopsDb = StopsDb.Deserialize(stream);

            // read transfer db's.
            var transferDbsCount = stream.ReadByte();
            var transferDbs      = new Dictionary <string, TransfersDb>();

            for (var i = 0; i < transferDbsCount; i++)
            {
                var profileName = stream.ReadWithSizeString();
                var transferDb  = TransfersDb.Deserialize(stream);

                transferDbs.Add(profileName, transferDb);
            }

            // read trip attributes.
            var tripAttributes = AttributesIndex.Deserialize(new LimitedStream(stream), true);

            // write trips db.
            var tripsDb = TripsDb.Deserialize(stream);

            ShapesDb shapesDb = null;

            if (version != 1)
            { // write shapes db.
                shapesDb = ShapesDb.Deserialize(stream);
            }

            return(new TransitDb(agencyAttributes, connectionsDb, schedulesDb, stopAttributes, stopsDb, transferDbs,
                                 tripAttributes, tripsDb, shapesDb));
        }
コード例 #4
0
 /// <summary>
 /// Creates a new transit db.
 /// </summary>
 private TransitDb(AttributesIndex agencyAttributes, ConnectionsDb connectionsDb, SchedulesDb schedulesDb, AttributesIndex stopAttributes,
                   StopsDb stopsDb, Dictionary <string, TransfersDb> transferDbs, AttributesIndex tripAttributes, TripsDb tripsDb, ShapesDb shapesDb)
 {
     _agencyAttributes = agencyAttributes;
     _connectionsDb    = connectionsDb;
     _schedulesDb      = schedulesDb;
     _stopAttributes   = stopAttributes;
     _stopsDb          = stopsDb;
     _transfersDbs     = transferDbs;
     _tripAttributes   = tripAttributes;
     _tripsDb          = tripsDb;
     _shapesDb         = shapesDb;
 }
コード例 #5
0
        /// <summary>
        /// Sorts the connections.
        /// </summary>
        public void Sort(DefaultSorting sorting, Action <uint, uint> switchConnections)
        {
            _sorting = sorting;

            if (_nextConnectionId > 0)
            {
                _connectionsOrder.Resize(_nextConnectionId);

                for (uint i = 0; i < _nextConnectionId; i++)
                {
                    _connectionsOrder[i] = i;
                }

                QuickSort.Sort((connection) =>
                {
                    uint departureTime, duration;
                    ConnectionsDb.DecodeDepartureTimeAndDuration(
                        _connections[connection * CONNECTION_SIZE + 3],
                        out departureTime, out duration);
                    uint tripId = _connections[connection * CONNECTION_SIZE + 2];
                    if (sorting == DefaultSorting.DepartureTime)
                    {
                        return((long)departureTime * int.MaxValue + tripId);
                    }
                    return((long)(departureTime + duration) * int.MaxValue + tripId);
                },
                               (connection1, connection2) =>
                {
                    var value0 = _connections[connection1 * CONNECTION_SIZE + 0];
                    var value1 = _connections[connection1 * CONNECTION_SIZE + 1];
                    var value2 = _connections[connection1 * CONNECTION_SIZE + 2];
                    var value3 = _connections[connection1 * CONNECTION_SIZE + 3];
                    _connections[connection1 * CONNECTION_SIZE + 0] = _connections[connection2 * CONNECTION_SIZE + 0];
                    _connections[connection1 * CONNECTION_SIZE + 1] = _connections[connection2 * CONNECTION_SIZE + 1];
                    _connections[connection1 * CONNECTION_SIZE + 2] = _connections[connection2 * CONNECTION_SIZE + 2];
                    _connections[connection1 * CONNECTION_SIZE + 3] = _connections[connection2 * CONNECTION_SIZE + 3];
                    _connections[connection2 * CONNECTION_SIZE + 0] = value0;
                    _connections[connection2 * CONNECTION_SIZE + 1] = value1;
                    _connections[connection2 * CONNECTION_SIZE + 2] = value2;
                    _connections[connection2 * CONNECTION_SIZE + 3] = value3;

                    if (switchConnections != null)
                    {
                        switchConnections((uint)connection1, (uint)connection2);
                    }
                }, 0, _nextConnectionId - 1);

                QuickSort.Sort((connection) =>
                {
                    uint departureTime, duration;
                    ConnectionsDb.DecodeDepartureTimeAndDuration(
                        _connections[_connectionsOrder[connection] * CONNECTION_SIZE + 3],
                        out departureTime, out duration);
                    uint tripId = _connections[_connectionsOrder[connection] * CONNECTION_SIZE + 2];
                    if (sorting != DefaultSorting.DepartureTime)
                    {
                        return((long)departureTime * int.MaxValue + tripId);
                    }
                    return((long)(departureTime + duration) * int.MaxValue + tripId);
                },
                               (connection1, connection2) =>
                {
                    var value = _connectionsOrder[connection1];
                    _connectionsOrder[connection1] = _connectionsOrder[connection2];
                    _connectionsOrder[connection2] = value;
                }, 0, _nextConnectionId - 1);
            }
        }