1+ using System ;
2+ using System . Collections ;
3+ using System . Collections . Generic ;
4+ using AuroraRgb . EffectsEngine ;
5+ using Common ;
6+ using Common . Devices ;
7+
8+ namespace AuroraRgb . Utils ;
9+
10+ public class DeviceKeyStore : IDictionary < DeviceKeys , SimpleColor >
11+ {
12+ public readonly SimpleColor [ ] ColorArray = new SimpleColor [ Effects . MaxDeviceId ] ;
13+ private readonly bool [ ] _keyExists = new bool [ Effects . MaxDeviceId ] ;
14+
15+ public ICollection < DeviceKeys > Keys => Enum . GetValues < DeviceKeys > ( ) ;
16+ public ICollection < SimpleColor > Values => ColorArray ;
17+
18+ public int Count => Effects . MaxDeviceId ;
19+ public bool IsReadOnly => false ;
20+
21+ public IEnumerator < KeyValuePair < DeviceKeys , SimpleColor > > GetEnumerator ( )
22+ {
23+ for ( var i = 0 ; i < Effects . MaxDeviceId ; i ++ )
24+ {
25+ if ( _keyExists [ i ] )
26+ yield return new KeyValuePair < DeviceKeys , SimpleColor > ( ( DeviceKeys ) i , ColorArray [ i ] ) ;
27+ }
28+ }
29+
30+ IEnumerator IEnumerable . GetEnumerator ( )
31+ {
32+ return GetEnumerator ( ) ;
33+ }
34+
35+ public void Add ( KeyValuePair < DeviceKeys , SimpleColor > item )
36+ {
37+ var index = GetEnumHash ( item . Key ) ;
38+ _keyExists [ index ] = true ;
39+ ColorArray [ index ] = item . Value ;
40+ }
41+
42+ public void Clear ( )
43+ {
44+ Array . Clear ( _keyExists , 0 , _keyExists . Length ) ;
45+ Array . Clear ( ColorArray , 0 , ColorArray . Length ) ;
46+ }
47+
48+ public bool Contains ( KeyValuePair < DeviceKeys , SimpleColor > item )
49+ {
50+ var index = GetEnumHash ( item . Key ) ;
51+ return _keyExists [ index ] ;
52+ }
53+
54+ public void CopyTo ( KeyValuePair < DeviceKeys , SimpleColor > [ ] array , int arrayIndex )
55+ {
56+ foreach ( var kvp in this )
57+ {
58+ array [ arrayIndex ++ ] = kvp ;
59+ }
60+ }
61+
62+ public bool Remove ( KeyValuePair < DeviceKeys , SimpleColor > item )
63+ {
64+ var exists = Contains ( item ) ;
65+ var index = GetEnumHash ( item . Key ) ;
66+ _keyExists [ index ] = false ;
67+ return exists ;
68+ }
69+
70+ public void Add ( DeviceKeys key , SimpleColor value )
71+ {
72+ var index = GetEnumHash ( key ) ;
73+ _keyExists [ index ] = true ;
74+ ColorArray [ index ] = value ;
75+ }
76+
77+ public bool ContainsKey ( DeviceKeys key )
78+ {
79+ var index = GetEnumHash ( key ) ;
80+ return _keyExists [ index ] ;
81+ }
82+
83+ public bool Remove ( DeviceKeys key )
84+ {
85+ var exists = ContainsKey ( key ) ;
86+ var index = GetEnumHash ( key ) ;
87+ _keyExists [ index ] = false ;
88+ return exists ;
89+ }
90+
91+ public bool TryGetValue ( DeviceKeys key , out SimpleColor value )
92+ {
93+ var index = GetEnumHash ( key ) ;
94+ if ( index == - 1 )
95+ {
96+ value = SimpleColor . Black ;
97+ return true ;
98+ }
99+
100+ if ( _keyExists [ index ] )
101+ {
102+ value = ColorArray [ index ] ;
103+ return true ;
104+ }
105+ value = SimpleColor . Transparent ;
106+ return false ;
107+ }
108+
109+ public SimpleColor this [ DeviceKeys key ]
110+ {
111+ get
112+ {
113+ var index = GetEnumHash ( key ) ;
114+ return ColorArray [ index ] ;
115+ }
116+ set
117+ {
118+ var index = GetEnumHash ( key ) ;
119+ _keyExists [ index ] = true ;
120+ ColorArray [ index ] = value ;
121+ }
122+ }
123+
124+ private static int GetEnumHash ( Enum obj )
125+ {
126+ return Convert . ToInt32 ( obj ) ;
127+ }
128+ }
0 commit comments