-
Notifications
You must be signed in to change notification settings - Fork 22
/
BContextList.cs
55 lines (45 loc) · 1.87 KB
/
BContextList.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
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace Binject {
/// <summary>
/// A list specifically designed for <see cref="BinjectManager"/>. it holds a <see cref="RootIndex"/> and sorts
/// contexts by their use count.
/// </summary>
public class BContextList : List<BContext> {
public int RootIndex;
List<int> _points;
HashSet<Transform> _transforms;
public BContextList(int capacity) : base( capacity ) => Init();
void Init() {
_points = new( Capacity );
_transforms = new( Capacity, new ObjectReferenceEquality<Transform>() );
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public BContext GetRoot() => RootIndex >= Count ? this[0] : this[RootIndex];
public new void Add(BContext context) {
base.Add( context );
_points.Add( 0 );
_transforms.Add( context.transform );
}
public new bool Remove(BContext context) {
_points.RemoveAt( IndexOf( context ) );
_transforms.Remove( context.transform );
return base.Remove( context );
}
public bool ContainsTransform(Transform transform) => _transforms.Contains( transform );
public void AddPoint(int index) {
if (index == 0) return;
_points[index]++;
while (index > 0 && _points[index] > _points[index - 1]) {
(this[index], this[index - 1]) = (this[index - 1], this[index]);
(_points[index], _points[index - 1]) = (_points[index - 1], _points[index]);
index--;
}
}
}
class ObjectReferenceEquality<T> : IEqualityComparer<T> {
public bool Equals(T x, T y) => ReferenceEquals( x, y );
public int GetHashCode(T obj) => obj.GetHashCode();
}
}