1- namespace PolylineAlgorithm . Utility ;
1+ //
2+ // Copyright © Pete Sramek. All rights reserved.
3+ // Licensed under the MIT License. See LICENSE file in the project root for full license information.
4+ //
5+
6+ namespace PolylineAlgorithm . Utility ;
27
38using System ;
49using System . Collections . Concurrent ;
510using System . Collections . Generic ;
611using System . Linq ;
712
13+ /// <summary>
14+ /// Provides random generation and caching of coordinate collections and their encoded polyline representations.
15+ /// Useful for testing and benchmarking polyline algorithms with reproducible random data.
16+ /// </summary>
817internal static class RandomValueProvider {
918 private static readonly Random _random = new ( DateTime . Now . Millisecond ) ;
1019 private static readonly ConcurrentDictionary < int , PolylineCoordinateCollectionPair > _cache = new ( ) ;
1120 private static readonly PolylineEncoder _encoder = new ( ) ;
1221
22+ /// <summary>
23+ /// Gets a collection of random <see cref="Coordinate"/> instances of the specified count.
24+ /// The same collection is cached and reused for the same count value.
25+ /// </summary>
26+ /// <param name="count">The number of coordinates to generate.</param>
27+ /// <returns>An enumerable collection of random <see cref="Coordinate"/> objects.</returns>
1328 public static IEnumerable < Coordinate > GetCoordinates ( int count ) {
1429 var entry = GetCaheEntry ( count ) ;
1530
@@ -20,12 +35,25 @@ public static IEnumerable<Coordinate> GetCoordinates(int count) {
2035 }
2136 }
2237
38+ /// <summary>
39+ /// Gets a <see cref="Polyline"/> representing the encoded polyline for a random collection of coordinates of the specified count.
40+ /// The same polyline is cached and reused for the same count value.
41+ /// </summary>
42+ /// <param name="count">The number of coordinates to generate and encode.</param>
43+ /// <returns>A <see cref="Polyline"/> representing the encoded polyline.</returns>
2344 public static Polyline GetPolyline ( int count ) {
2445 var entry = GetCaheEntry ( count ) ;
2546
2647 return entry . Polyline ;
2748 }
2849
50+ /// <summary>
51+ /// Gets or creates a cached entry containing a collection of random coordinates and its encoded polyline for the specified count.
52+ /// </summary>
53+ /// <param name="count">The number of coordinates to generate and cache.</param>
54+ /// <returns>
55+ /// A <see cref="PolylineCoordinateCollectionPair"/> containing the coordinates and their encoded polyline.
56+ /// </returns>
2957 private static PolylineCoordinateCollectionPair GetCaheEntry ( int count ) {
3058 if ( _cache . TryGetValue ( count , out var entry ) ) {
3159 return entry ;
@@ -41,16 +69,36 @@ private static PolylineCoordinateCollectionPair GetCaheEntry(int count) {
4169 return entry ;
4270 }
4371
72+ /// <summary>
73+ /// Generates a random longitude value in the range [-180, 180), rounded to 5 decimal places.
74+ /// </summary>
75+ /// <returns>A random longitude value.</returns>
4476 private static double RandomLongitude ( ) {
4577 return Math . Round ( _random . Next ( - 180 , 180 ) + _random . NextDouble ( ) , 5 ) ;
4678 }
4779
80+ /// <summary>
81+ /// Generates a random latitude value in the range [-90, 90), rounded to 5 decimal places.
82+ /// </summary>
83+ /// <returns>A random latitude value.</returns>
4884 private static double RandomLatitude ( ) {
4985 return Math . Round ( _random . Next ( - 90 , 90 ) + _random . NextDouble ( ) , 5 ) ;
5086 }
5187
88+ /// <summary>
89+ /// Represents a pair of a collection of coordinates and its encoded polyline.
90+ /// </summary>
91+ /// <param name="coordinates">The collection of coordinates.</param>
92+ /// <param name="polyline">The encoded polyline.</param>
5293 private readonly struct PolylineCoordinateCollectionPair ( IEnumerable < Coordinate > coordinates , Polyline polyline ) {
94+ /// <summary>
95+ /// Gets the collection of coordinates.
96+ /// </summary>
5397 public IEnumerable < Coordinate > Coordinates { get ; } = coordinates ;
98+
99+ /// <summary>
100+ /// Gets the encoded polyline.
101+ /// </summary>
54102 public Polyline Polyline { get ; } = polyline ;
55103 }
56104}
0 commit comments