You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 8 месеци
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // This code is distributed under MIT license.
  2. // Copyright (c) 2010-2018 George Mamaladze
  3. // See license.txt or https://mit-license.org/
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace Gma.System.MouseKeyHook
  8. {
  9. /// <summary>
  10. /// Describes a sequence of generic objects.
  11. /// </summary>
  12. /// <typeparam name="T"></typeparam>
  13. public abstract class SequenceBase<T> : IEnumerable<T>
  14. {
  15. private readonly T[] _elements;
  16. /// <summary>
  17. /// Creates an instance of sequnce from sequnce elements.
  18. /// </summary>
  19. /// <param name="elements"></param>
  20. protected SequenceBase(params T[] elements)
  21. {
  22. _elements = elements;
  23. }
  24. /// <summary>
  25. /// Number of elements in the sequnce.
  26. /// </summary>
  27. public int Length
  28. {
  29. get { return _elements.Length; }
  30. }
  31. /// <inheritdoc />
  32. public IEnumerator<T> GetEnumerator()
  33. {
  34. return _elements.Cast<T>().GetEnumerator();
  35. }
  36. /// <inheritdoc />
  37. IEnumerator IEnumerable.GetEnumerator()
  38. {
  39. return GetEnumerator();
  40. }
  41. /// <inheritdoc />
  42. public override string ToString()
  43. {
  44. return string.Join(",", _elements);
  45. }
  46. /// <inheritdoc />
  47. protected bool Equals(SequenceBase<T> other)
  48. {
  49. if (_elements.Length != other._elements.Length) return false;
  50. return _elements.SequenceEqual(other._elements);
  51. }
  52. /// <inheritdoc />
  53. public override bool Equals(object obj)
  54. {
  55. if (ReferenceEquals(null, obj)) return false;
  56. if (ReferenceEquals(this, obj)) return true;
  57. if (obj.GetType() != GetType()) return false;
  58. return Equals((SequenceBase<T>) obj);
  59. }
  60. /// <inheritdoc />
  61. public override int GetHashCode()
  62. {
  63. unchecked
  64. {
  65. return (_elements.Length + 13) ^
  66. ((_elements.Length != 0
  67. ? _elements[0].GetHashCode() ^ _elements[_elements.Length - 1].GetHashCode()
  68. : 0) * 397);
  69. }
  70. }
  71. }
  72. }