| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using Itinero.Profiles.EdgeTypesMap; |
| | 4 | | using Itinero.Routing.Costs.Caches; |
| | 5 | |
|
| | 6 | | namespace Itinero.Profiles; |
| | 7 | |
|
| | 8 | | internal class RouterDbProfileConfiguration |
| | 9 | | { |
| | 10 | | private readonly Dictionary<string, (Profile profile, EdgeFactorCache cache, TurnCostFactorCache turnCostFactorCache |
| | 11 | | private readonly RouterDb _routerDb; |
| | 12 | |
|
| 152 | 13 | | public RouterDbProfileConfiguration(RouterDb routerDb) |
| 152 | 14 | | { |
| 152 | 15 | | _routerDb = routerDb; |
| 152 | 16 | | _profiles = new Dictionary<string, (Profile profile, EdgeFactorCache cache, TurnCostFactorCache turnCostFactorCa |
| 152 | 17 | | } |
| | 18 | |
|
| | 19 | | internal IEnumerable<string> GetProfileNames() |
| 0 | 20 | | { |
| 0 | 21 | | return _profiles.Keys.ToList(); |
| 0 | 22 | | } |
| | 23 | |
|
| | 24 | | internal bool HasProfile(string name) |
| 0 | 25 | | { |
| 0 | 26 | | return _profiles.ContainsKey(name); |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | public void AddProfiles(IEnumerable<Profile> profiles) |
| 0 | 30 | | { |
| 0 | 31 | | foreach (var profile in profiles) |
| 0 | 32 | | { |
| 0 | 33 | | _profiles[profile.Name] = (profile, new EdgeFactorCache(), new TurnCostFactorCache()); |
| 0 | 34 | | } |
| | 35 | |
|
| 0 | 36 | | this.UpdateEdgeTypeMap(); |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | internal bool TryGetProfileHandlerEdgeTypesCache(string profileName, out EdgeFactorCache? cache, out TurnCostFactorC |
| 47 | 40 | | { |
| 47 | 41 | | cache = null; |
| 47 | 42 | | turnCostFactorCache = null; |
| 47 | 43 | | if (!_profiles.TryGetValue(profileName, out var profileValue)) |
| 47 | 44 | | { |
| 47 | 45 | | return false; |
| | 46 | | } |
| | 47 | |
|
| 0 | 48 | | cache = profileValue.cache; |
| 0 | 49 | | turnCostFactorCache = profileValue.turnCostFactorCache; |
| 0 | 50 | | return true; |
| 47 | 51 | | } |
| | 52 | |
|
| | 53 | | private void UpdateEdgeTypeMap() |
| 0 | 54 | | { |
| | 55 | | // only update the edge type map when it is based on the active profiles. |
| 0 | 56 | | if (_routerDb.EdgeTypeMap is not ProfilesEdgeTypeMap) |
| 0 | 57 | | { |
| 0 | 58 | | return; |
| | 59 | | } |
| | 60 | |
|
| | 61 | | // update edge type map to include the new profile(s). |
| 0 | 62 | | var edgeTypeMap = new ProfilesEdgeTypeMap(_profiles.Values.Select(x => x.profile)); |
| 0 | 63 | | _routerDb.EdgeTypeMap = edgeTypeMap; |
| 0 | 64 | | } |
| | 65 | |
|
| 0 | 66 | | public IEnumerable<Profile> Profiles => _profiles.Values.Select(x => x.profile); |
| | 67 | | } |