private void GenerateIndex()

in Source/Models/ResponseModels/DistanceMatrix.cs [533:627]


        private void GenerateIndex()
        {
            if (results == null)
            {
                cellIndex = null;
                return;
            }

            //Scan results and capture time intervals, origin and destination sizes. 
            numOrigins = -1;
            numDestinations = -1;
            TimeIntervals = new List<DateTime>();

            foreach (var c in results)
            {
                if(c.OriginIndex > numOrigins)
                {
                    numOrigins = c.OriginIndex;
                }

                if (c.DestinationIndex > numDestinations)
                {
                    numDestinations = c.DestinationIndex;
                }

                var date = c.DepartureTimeUtc;

                if (date != null && date.HasValue && !TimeIntervals.Contains(date.Value))
                {
                    TimeIntervals.Add(date.Value);
                }
            }

            numOrigins++;
            numDestinations++;

            if (TimeIntervals.Count == 0)
            {
                TimeIntervals.Add(DateTime.Now);
            }
            else
            {
                //Sort the time intervals.
                TimeIntervals.Sort();
            }

            if (numDestinations == 0 && numOrigins > 0)
            {
                numDestinations = numOrigins;
            }

            if (numOrigins > 0 && numDestinations > 0)
            {
                numTimeIntervals = TimeIntervals.Count;

                //Initialize cell indexer
                cellIndex = new int[numOrigins][][];

                Parallel.For(0, numOrigins, o =>
                {
                    cellIndex[o] = new int[numDestinations][];

                    for (int d = 0; d < numDestinations; d++)
                    {
                        cellIndex[o][d] = new int[numTimeIntervals];

                        for (int t = 0; t < numTimeIntervals; t++)
                        {
                            cellIndex[o][d][t] = -1;
                        }
                    }
                });

                //Loop through results and generate index.
                if (numTimeIntervals == 1)
                {
                    Parallel.For(0, results.Length, i =>
                    {
                        var cell = results[i];
                        cellIndex[cell.OriginIndex][cell.DestinationIndex][0] = i;
                    });
                }
                else
                {
                    Parallel.For(0, results.Length, i =>
                    {
                        var cell = results[i];
                        if (cell.DepartureTimeUtc != null && cell.DepartureTimeUtc.HasValue && !TimeIntervals.Contains(cell.DepartureTimeUtc.Value))
                        {
                            cellIndex[cell.OriginIndex][cell.DestinationIndex][TimeIntervals.IndexOf(cell.DepartureTimeUtc.Value)] = i;
                        }
                    });
                }
            }
        }