1+ // Copyright (c) TensorStack. All rights reserved.
2+ // Licensed under the Apache 2.0 License.
3+ using System ;
4+ using System . Collections ;
5+ using System . Collections . Generic ;
6+ using System . Diagnostics . CodeAnalysis ;
7+
8+ namespace TensorStack . Common
9+ {
10+ public class MapCollection < TKey , TValue > : IDictionary < TKey , TValue >
11+ {
12+ private readonly IDictionary < TKey , TValue > _collectionKeys ;
13+ private readonly IDictionary < TValue , TKey > _collectionValues ;
14+
15+ public MapCollection ( )
16+ {
17+ _collectionKeys = new Dictionary < TKey , TValue > ( ) ;
18+ _collectionValues = new Dictionary < TValue , TKey > ( ) ;
19+ }
20+
21+ public MapCollection ( IDictionary < TKey , TValue > collection ) : this ( )
22+ {
23+ foreach ( var item in collection )
24+ Add ( item . Key , item . Value ) ;
25+ }
26+
27+ public MapCollection ( IDictionary < TValue , TKey > collection ) : this ( )
28+ {
29+ foreach ( var item in collection )
30+ Add ( item . Value , item . Key ) ;
31+ }
32+
33+
34+ public ICollection < TKey > Keys => _collectionKeys . Keys ;
35+ public ICollection < TValue > Values => _collectionValues . Keys ;
36+ public int Count => _collectionKeys . Count ;
37+ public bool IsReadOnly => false ;
38+
39+
40+ public TValue this [ TKey key ]
41+ {
42+ get { return _collectionKeys [ key ] ; }
43+ set
44+ {
45+ if ( _collectionKeys . TryGetValue ( key , out var oldValue ) )
46+ _collectionValues . Remove ( oldValue ) ;
47+
48+ _collectionKeys [ key ] = value ;
49+ _collectionValues [ value ] = key ;
50+ }
51+ }
52+
53+
54+ public TKey this [ TValue key ]
55+ {
56+ get { return _collectionValues [ key ] ; }
57+ set
58+ {
59+ if ( _collectionValues . TryGetValue ( key , out var oldKey ) )
60+ _collectionKeys . Remove ( oldKey ) ;
61+
62+ _collectionValues [ key ] = value ;
63+ _collectionKeys [ value ] = key ;
64+ }
65+ }
66+
67+
68+ public void Add ( TKey key , TValue value )
69+ {
70+ _collectionKeys . Add ( key , value ) ;
71+ _collectionValues . Add ( value , key ) ;
72+ }
73+
74+ public void Add ( KeyValuePair < TKey , TValue > item )
75+ {
76+ _collectionKeys . Add ( item . Key , item . Value ) ;
77+ _collectionValues . Add ( item . Value , item . Key ) ;
78+ }
79+
80+ public bool TryAdd ( TKey key , TValue value )
81+ {
82+ return _collectionKeys . TryAdd ( key , value )
83+ && _collectionValues . TryAdd ( value , key ) ;
84+ }
85+
86+ public void Clear ( )
87+ {
88+ _collectionKeys . Clear ( ) ;
89+ _collectionValues . Clear ( ) ;
90+ }
91+
92+ public bool Contains ( KeyValuePair < TKey , TValue > item )
93+ {
94+ return _collectionKeys . Contains ( item ) ;
95+ }
96+
97+ public bool ContainsKey ( TKey key )
98+ {
99+ return _collectionKeys . ContainsKey ( key ) ;
100+ }
101+
102+ public bool ContainsKey ( TValue key )
103+ {
104+ return _collectionValues . ContainsKey ( key ) ;
105+ }
106+
107+ public void CopyTo ( KeyValuePair < TKey , TValue > [ ] array , int arrayIndex )
108+ {
109+ throw new NotImplementedException ( ) ;
110+ }
111+
112+ public IEnumerator < KeyValuePair < TKey , TValue > > GetEnumerator ( )
113+ {
114+ return _collectionKeys . GetEnumerator ( ) ;
115+ }
116+
117+ public bool Remove ( TKey key )
118+ {
119+ if ( _collectionKeys . TryGetValue ( key , out TValue value ) )
120+ {
121+ _collectionKeys . Remove ( key ) ;
122+ _collectionValues . Remove ( value ) ;
123+ return true ;
124+ }
125+ return false ;
126+ }
127+
128+ public bool Remove ( KeyValuePair < TKey , TValue > item )
129+ {
130+ return Remove ( item . Key ) ;
131+ }
132+
133+ public bool TryGetValue ( TKey key , [ MaybeNullWhen ( false ) ] out TValue value )
134+ {
135+ return _collectionKeys . TryGetValue ( key , out value ) ;
136+ }
137+
138+
139+ public bool TryGetValue ( TValue key , [ MaybeNullWhen ( false ) ] out TKey value )
140+ {
141+ return _collectionValues . TryGetValue ( key , out value ) ;
142+ }
143+
144+
145+ IEnumerator IEnumerable . GetEnumerator ( )
146+ {
147+ return GetEnumerator ( ) ;
148+ }
149+ }
150+ }
0 commit comments