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