Entitas  0.40.0
Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
MatcherEquals.cs
1 namespace Entitas {
2 
3  public partial class Matcher<TEntity> {
4 
5  public override bool Equals(object obj) {
6  if(obj == null || obj.GetType() != GetType() ||
7  obj.GetHashCode() != GetHashCode()) {
8  return false;
9  }
10 
11  var matcher = (Matcher<TEntity>)obj;
12  if(!equalIndices(matcher.allOfIndices, _allOfIndices)) {
13  return false;
14  }
15  if(!equalIndices(matcher.anyOfIndices, _anyOfIndices)) {
16  return false;
17  }
18  if(!equalIndices(matcher.noneOfIndices, _noneOfIndices)) {
19  return false;
20  }
21 
22  return true;
23  }
24 
25  static bool equalIndices(int[] i1, int[] i2) {
26  if((i1 == null) != (i2 == null)) {
27  return false;
28  }
29  if(i1 == null) {
30  return true;
31  }
32  if(i1.Length != i2.Length) {
33  return false;
34  }
35 
36  for (int i = 0; i < i1.Length; i++) {
37  if(i1[i] != i2[i]) {
38  return false;
39  }
40  }
41 
42  return true;
43  }
44 
45  int _hash;
46  bool _isHashCached;
47 
48  public override int GetHashCode() {
49  if(!_isHashCached) {
50  var hash = GetType().GetHashCode();
51  hash = applyHash(hash, _allOfIndices, 3, 53);
52  hash = applyHash(hash, _anyOfIndices, 307, 367);
53  hash = applyHash(hash, _noneOfIndices, 647, 683);
54  _hash = hash;
55  _isHashCached = true;
56  }
57 
58  return _hash;
59  }
60 
61  static int applyHash(int hash, int[] indices, int i1, int i2) {
62  if(indices != null) {
63  for (int i = 0; i < indices.Length; i++) {
64  hash ^= indices[i] * i1;
65  }
66  hash ^= indices.Length * i2;
67  }
68 
69  return hash;
70  }
71  }
72 }