| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Security.Cryptography; |
| | 4 | |
|
| | 5 | | namespace Itinero.Profiles; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Abstract representation of a profile. |
| | 9 | | /// </summary> |
| | 10 | | /// <remarks> |
| | 11 | | /// This is a single possible behaviour of a vehicle (e.g. 'bicycle.fastest'). |
| | 12 | | /// </remarks> |
| | 13 | | public abstract class Profile |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Gets the name of this profile. |
| | 17 | | /// </summary> |
| | 18 | | public abstract string Name { get; } |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Gets the edge factor for the given attributes. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="attributes">The attributes.</param> |
| | 24 | | /// <returns>An edge factor.</returns> |
| | 25 | | public abstract EdgeFactor Factor(IEnumerable<(string key, string value)> attributes); |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Gets a factor for the turn costs for the given attributes. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="attributes">The attributes.</param> |
| | 31 | | /// <returns>A turn cost factor.</returns> |
| | 32 | | public abstract TurnCostFactor TurnCostFactor(IEnumerable<(string key, string value)> attributes); |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Gets a stable hashcode, this hashcode doesn't change between runs. |
| | 36 | | /// </summary> |
| | 37 | | /// <remarks> |
| | 38 | | /// This uses the name property by default, if using profiles with unique names this default implementation is fine. |
| | 39 | | /// </remarks> |
| | 40 | | /// <returns>The stable hash code.</returns> |
| | 41 | | public override int GetHashCode() |
| 0 | 42 | | { |
| | 43 | | // TODO: a slow hash, this can be better. |
| | 44 | | // on the other hand this hash should only be computed once when the profile is added to a router db. |
| 0 | 45 | | var hash = Hasher.ComputeHash(System.Text.Encoding.Default.GetBytes(this.Name)); |
| 0 | 46 | | return BitConverter.ToInt32(hash, 0); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Compares this profiles to the given profile. Returns true if the profiles are identical functionally. |
| | 51 | | /// </summary> |
| | 52 | | /// <remarks> |
| | 53 | | /// This uses the name property by default, if using profiles with unique names this default implementation is fine. |
| | 54 | | /// </remarks> |
| | 55 | | /// <returns>True if the profiles have the same name.</returns> |
| | 56 | | public override bool Equals(object obj) |
| 0 | 57 | | { |
| 0 | 58 | | if (obj is Profile profile) |
| 0 | 59 | | { |
| 0 | 60 | | return profile.Name.Equals(this.Name); |
| | 61 | | } |
| | 62 | |
|
| 0 | 63 | | return false; |
| 0 | 64 | | } |
| | 65 | |
|
| 0 | 66 | | private static readonly MD5 Hasher = MD5.Create(); |
| | 67 | | } |