1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System.Collections.Generic;
- using System.Linq;
- namespace Dbscan
- {
- /// <summary>
- /// An implementation of the <see cref="T:Dbscan.ISpatialIndex`1" /> using a simple <see cref="T:System.Collections.Generic.List`1" />
- /// to hold the elements and a linear search of all items to <see cref="M:Dbscan.ListSpatialIndex`1.Search(Dbscan.IPointData@,System.Double)" />
- /// for nearby items.
- /// </summary>
- /// <typeparam name="T">The type of items in the index.</typeparam>
- public class ListSpatialIndex<T> : ISpatialIndex<T> where T : IPointData
- {
- private readonly IReadOnlyList<T> _list;
- private readonly DistanceFunction _distanceFunction;
- /// <summary>
- /// Initializes a <see cref="T:Dbscan.ListSpatialIndex`1" /> with a collection of data, using the
- /// Euclidean distance between two points as the distance function to search for points
- /// in a given neighborhood.
- /// </summary>
- /// <param name="data">The collection of data to put into the index</param>
- public ListSpatialIndex(IEnumerable<T> data)
- : this(data, (DistanceFunction)DistanceFunctions.EuclideanDistance)
- {
- }
- /// <summary>
- /// Initializes a <see cref="T:Dbscan.ListSpatialIndex`1" /> with a collection of data, using the
- /// specified distanct function to search for points in a given neighborhood.
- /// </summary>
- /// <param name="data">The collection of data to put into the index</param>
- /// <param name="distanceFunction">The function used to determine if a point is within a specified distance of a given point.</param>
- public ListSpatialIndex(IEnumerable<T> data, DistanceFunction distanceFunction)
- {
- _list = data.ToList();
- _distanceFunction = distanceFunction;
- }
- /// <summary>
- /// Get all of the elements within the current <see cref="T:Dbscan.ListSpatialIndex`1" />.
- /// </summary>
- /// <returns>
- /// A list of every element contained in the <see cref="T:Dbscan.ListSpatialIndex`1" />.
- /// </returns>
- public IReadOnlyList<T> Search()
- {
- return _list;
- }
- /// <summary>
- /// Get all of the elements from this <see cref="T:Dbscan.ListSpatialIndex`1" />
- /// within a circle centered at the point <see cref="P:Dbscan.IPointData.Point" />
- /// with a radius of <paramref name="epsilon" />.
- /// </summary>
- /// <param name="p">The center of the search circle.</param>
- /// <param name="epsilon">The radius of the search circle.</param>
- /// <returns>
- /// A list of the points that are within the search area.
- /// </returns>
- public IReadOnlyList<T> Search(in IPointData p, double epsilon)
- {
- List<T> list = new List<T>();
- foreach (T item in _list)
- {
- DistanceFunction distanceFunction = _distanceFunction;
- IPointData p2 = item;
- if (distanceFunction(in p, in p2) < epsilon)
- {
- list.Add(item);
- }
- }
- return list;
- }
- IReadOnlyList<T> ISpatialIndex<T>.Search(in IPointData p, double epsilon)
- {
- return Search(in p, epsilon);
- }
- }
- }
|