| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.IO; |
| | 3 | | using System.Linq; |
| | 4 | | using Itinero.IO; |
| | 5 | | using Itinero.Profiles; |
| | 6 | | using Itinero.Profiles.Serialization; |
| | 7 | | using Itinero.Routing.Costs.Caches; |
| | 8 | |
|
| | 9 | | namespace Itinero.Network.Profiles; |
| | 10 | |
|
| | 11 | | internal class RoutingNetworkProfilesConfiguration |
| | 12 | | { |
| | 13 | | private readonly Dictionary<string, (Profile profile, EdgeFactorCache cache)> _profiles; |
| | 14 | |
|
| 0 | 15 | | public RoutingNetworkProfilesConfiguration() |
| 0 | 16 | | { |
| 0 | 17 | | _profiles = new Dictionary<string, (Profile profile, EdgeFactorCache cache)>(); |
| 0 | 18 | | } |
| | 19 | |
|
| 0 | 20 | | private RoutingNetworkProfilesConfiguration( |
| 0 | 21 | | Dictionary<string, (Profile profile, EdgeFactorCache cache)> profiles) |
| 0 | 22 | | { |
| 0 | 23 | | _profiles = new Dictionary<string, (Profile profile, EdgeFactorCache cache)>(profiles); |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | public RoutingNetworkProfilesConfiguration Clone() |
| 0 | 27 | | { |
| 0 | 28 | | return new(_profiles); |
| 0 | 29 | | } |
| | 30 | |
|
| | 31 | | public bool HasProfile(string name) |
| 0 | 32 | | { |
| 0 | 33 | | return _profiles.ContainsKey(name); |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | public bool AddProfile(Profile profile) |
| 0 | 37 | | { |
| 0 | 38 | | if (_profiles.ContainsKey(profile.Name)) |
| 0 | 39 | | { |
| 0 | 40 | | return false; |
| | 41 | | } |
| | 42 | |
|
| 0 | 43 | | _profiles[profile.Name] = (profile, new EdgeFactorCache()); |
| 0 | 44 | | return true; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | internal bool TryGetProfileHandlerEdgeTypesCache(Profile profile, out EdgeFactorCache? cache) |
| 0 | 48 | | { |
| 0 | 49 | | cache = null; |
| 0 | 50 | | if (!_profiles.TryGetValue(profile.Name, out var profileValue)) |
| 0 | 51 | | { |
| 0 | 52 | | return false; |
| | 53 | | } |
| | 54 | |
|
| 0 | 55 | | cache = profileValue.cache; |
| 0 | 56 | | return true; |
| 0 | 57 | | } |
| | 58 | |
|
| 0 | 59 | | public IEnumerable<Profile> Profiles => _profiles.Values.Select(x => x.profile); |
| | 60 | |
|
| | 61 | | public void WriteTo(Stream stream, IProfileSerializer profileSerializer) |
| 0 | 62 | | { |
| | 63 | | // write version #. |
| 0 | 64 | | stream.WriteVarInt32(1); |
| | 65 | |
|
| | 66 | | // write number of profiles. |
| 0 | 67 | | stream.WriteVarInt32(_profiles.Count); |
| | 68 | |
|
| | 69 | | // write profiles. |
| 0 | 70 | | foreach (var (profile, _) in _profiles.Values) |
| 0 | 71 | | { |
| 0 | 72 | | stream.WriteProfile(profile, profileSerializer); |
| 0 | 73 | | } |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | public static RoutingNetworkProfilesConfiguration |
| | 77 | | ReadFrom(Stream stream, IProfileSerializer profileSerializer) |
| 0 | 78 | | { |
| | 79 | | // verify version #. |
| 0 | 80 | | var version = stream.ReadVarInt32(); |
| 0 | 81 | | if (version != 1) |
| 0 | 82 | | { |
| 0 | 83 | | throw new InvalidDataException("Invalid version #."); |
| | 84 | | } |
| | 85 | |
|
| | 86 | | // read number of profiles. |
| 0 | 87 | | var profileCount = stream.ReadVarInt32(); |
| | 88 | |
|
| | 89 | | // write profiles. |
| 0 | 90 | | var profiles = new Dictionary<string, (Profile profile, EdgeFactorCache cache)>(profileCount); |
| 0 | 91 | | for (var p = 0; p < profileCount; p++) |
| 0 | 92 | | { |
| 0 | 93 | | var profile = profileSerializer.ReadFrom(stream); |
| 0 | 94 | | profiles[profile.Name] = (profile, new EdgeFactorCache()); |
| 0 | 95 | | } |
| | 96 | |
|
| 0 | 97 | | return new RoutingNetworkProfilesConfiguration(profiles); |
| 0 | 98 | | } |
| | 99 | | } |