@@ -9,100 +9,102 @@ pub mod HashSet_ {
99 #[ cfg( not( feature = "no_std" ) ) ]
1010 use std:: collections;
1111
12+ use crate :: Global_ :: SR :: indexOutOfBounds;
1213 use crate :: NativeArray_ :: { array_from, Array } ;
13- use crate :: Native_ :: { default_eq_comparer, mkRefMut , seq_to_iter} ;
14+ use crate :: Native_ :: { default_eq_comparer, seq_to_iter} ;
1415 use crate :: Native_ :: { HashKey , LrcPtr , MutCell , Seq , Vec } ;
1516 use crate :: System :: Collections :: Generic :: IEqualityComparer_1 ;
1617
1718 use core:: fmt:: { Debug , Display , Formatter , Result } ;
1819 use core:: hash:: Hash ;
1920
20- type MutHashSet < T > = MutCell < collections:: HashSet < HashKey < T > > > ;
21-
2221 #[ derive( Clone ) ] //, Debug, Default, PartialEq, PartialOrd, Eq, Hash, Ord)]
23- pub struct HashSet < T : Clone > {
24- hash_set : LrcPtr < MutHashSet < T > > ,
22+ pub struct MutHashSet < T : Clone > {
23+ hash_set : MutCell < collections :: HashSet < HashKey < T > > > ,
2524 comparer : LrcPtr < dyn IEqualityComparer_1 < T > > ,
2625 }
2726
28- // impl<T> Default for HashSet<T>
29- // where
30- // T: Clone + Hash + PartialEq + 'static,
31- // {
32- // fn default() -> HashSet<T> {
33- // new_empty()
34- // }
35- // }
27+ pub type HashSet < T > = LrcPtr < MutHashSet < T > > ;
3628
37- impl < T : Clone > core:: ops:: Deref for HashSet < T > {
38- type Target = LrcPtr < MutHashSet < T > > ;
29+ impl < T : Clone > core:: ops:: Deref for MutHashSet < T > {
30+ type Target = MutCell < collections :: HashSet < HashKey < T > > > ;
3931 fn deref ( & self ) -> & Self :: Target {
4032 & self . hash_set
4133 }
4234 }
4335
44- impl < T : Clone + Debug > Debug for HashSet < T > {
36+ impl < T : Clone + Debug > Debug for MutHashSet < T > {
4537 fn fmt ( & self , f : & mut Formatter ) -> Result {
4638 write ! ( f, "{:?}" , self . hash_set) //TODO:
4739 }
4840 }
4941
50- impl < T : Clone + Debug > Display for HashSet < T > {
42+ impl < T : Clone + Debug > Display for MutHashSet < T > {
5143 fn fmt ( & self , f : & mut Formatter ) -> Result {
5244 write ! ( f, "{:?}" , self . hash_set) //TODO:
5345 }
5446 }
5547
48+ fn make_hash_set < T : Clone > (
49+ hash_set : collections:: HashSet < HashKey < T > > ,
50+ comparer : LrcPtr < dyn IEqualityComparer_1 < T > > ,
51+ ) -> HashSet < T > {
52+ LrcPtr :: new ( MutHashSet {
53+ hash_set : MutCell :: new ( hash_set) ,
54+ comparer,
55+ } )
56+ }
57+
5658 fn from_iter < T : Clone + ' static , I : Iterator < Item = T > > (
5759 iter : I ,
5860 comparer : LrcPtr < dyn IEqualityComparer_1 < T > > ,
5961 ) -> HashSet < T > {
6062 let it = iter. map ( |v| HashKey :: new ( v, comparer. clone ( ) ) ) ;
61- HashSet {
62- hash_set : mkRefMut ( collections:: HashSet :: from_iter ( it) ) ,
63- comparer,
64- }
63+ make_hash_set ( collections:: HashSet :: from_iter ( it) , comparer)
6564 }
6665
6766 fn to_iter < T : Clone > ( set : & HashSet < T > ) -> impl Iterator < Item = T > + ' _ {
6867 set. iter ( ) . map ( |k| k. key . clone ( ) )
6968 }
7069
70+ fn seq_to_hash_set < T : Clone + ' static > (
71+ seq : Seq < T > ,
72+ comparer : LrcPtr < dyn IEqualityComparer_1 < T > > ,
73+ ) -> collections:: HashSet < HashKey < T > > {
74+ seq_to_iter ( seq)
75+ . map ( |v| HashKey :: new ( v, comparer. clone ( ) ) )
76+ . collect ( )
77+ }
78+
7179 pub fn new_empty < T > ( ) -> HashSet < T >
7280 where
7381 T : Clone + Hash + PartialEq + ' static ,
7482 {
75- HashSet {
76- hash_set : mkRefMut ( collections:: HashSet :: new ( ) ) ,
77- comparer : default_eq_comparer :: < T > ( ) ,
78- }
83+ make_hash_set ( collections:: HashSet :: new ( ) , default_eq_comparer :: < T > ( ) )
7984 }
8085
8186 pub fn new_with_capacity < T > ( capacity : i32 ) -> HashSet < T >
8287 where
8388 T : Clone + Hash + PartialEq + ' static ,
8489 {
85- HashSet {
86- hash_set : mkRefMut ( collections:: HashSet :: with_capacity ( capacity as usize ) ) ,
87- comparer : default_eq_comparer :: < T > ( ) ,
88- }
90+ make_hash_set (
91+ collections:: HashSet :: with_capacity ( capacity as usize ) ,
92+ default_eq_comparer :: < T > ( ) ,
93+ )
8994 }
9095
9196 pub fn new_with_comparer < T : Clone > ( comparer : LrcPtr < dyn IEqualityComparer_1 < T > > ) -> HashSet < T > {
92- HashSet {
93- hash_set : mkRefMut ( collections:: HashSet :: new ( ) ) ,
94- comparer,
95- }
97+ make_hash_set ( collections:: HashSet :: new ( ) , comparer)
9698 }
9799
98100 pub fn new_with_capacity_comparer < T : Clone > (
99101 capacity : i32 ,
100102 comparer : LrcPtr < dyn IEqualityComparer_1 < T > > ,
101103 ) -> HashSet < T > {
102- HashSet {
103- hash_set : mkRefMut ( collections:: HashSet :: with_capacity ( capacity as usize ) ) ,
104+ make_hash_set (
105+ collections:: HashSet :: with_capacity ( capacity as usize ) ,
104106 comparer,
105- }
107+ )
106108 }
107109
108110 pub fn new_from_enumerable < T > ( seq : Seq < T > ) -> HashSet < T >
@@ -146,6 +148,89 @@ pub mod HashSet_ {
146148 set. get_mut ( ) . clear ( ) ;
147149 }
148150
151+ pub fn unionWith < T : Clone + ' static > ( set : HashSet < T > , other : Seq < T > ) {
152+ let other = seq_to_hash_set ( other, set. comparer . clone ( ) ) ;
153+ let hash_set = set. get_mut ( ) ;
154+ for key in other {
155+ hash_set. insert ( key) ;
156+ }
157+ }
158+
159+ pub fn intersectWith < T : Clone + ' static > ( set : HashSet < T > , other : Seq < T > ) {
160+ let other = seq_to_hash_set ( other, set. comparer . clone ( ) ) ;
161+ set. get_mut ( ) . retain ( |key| other. contains ( key) ) ;
162+ }
163+
164+ pub fn exceptWith < T : Clone + ' static > ( set : HashSet < T > , other : Seq < T > ) {
165+ let other = seq_to_hash_set ( other, set. comparer . clone ( ) ) ;
166+ let hash_set = set. get_mut ( ) ;
167+ for key in other {
168+ hash_set. remove ( & key) ;
169+ }
170+ }
171+
172+ pub fn symmetricExceptWith < T : Clone + ' static > ( set : HashSet < T > , other : Seq < T > ) {
173+ let other = seq_to_hash_set ( other, set. comparer . clone ( ) ) ;
174+ let hash_set = set. get_mut ( ) ;
175+ for key in other {
176+ if !hash_set. remove ( & key) {
177+ hash_set. insert ( key) ;
178+ }
179+ }
180+ }
181+
182+ pub fn overlaps < T : Clone + ' static > ( set : HashSet < T > , other : Seq < T > ) -> bool {
183+ let other = seq_to_hash_set ( other, set. comparer . clone ( ) ) ;
184+ set. iter ( ) . any ( |key| other. contains ( key) )
185+ }
186+
187+ pub fn setEquals < T : Clone + ' static > ( set : HashSet < T > , other : Seq < T > ) -> bool {
188+ let other = seq_to_hash_set ( other, set. comparer . clone ( ) ) ;
189+ set. len ( ) == other. len ( ) && set. is_subset ( & other)
190+ }
191+
192+ pub fn copyTo < T : Clone > ( set : HashSet < T > , dest : Array < T > ) {
193+ copyTo2 ( set, dest, 0 )
194+ }
195+
196+ pub fn copyTo2 < T : Clone > ( set : HashSet < T > , dest : Array < T > , destIndex : i32 ) {
197+ let count = set. len ( ) as i32 ;
198+ copyTo3 ( set, dest, destIndex, count)
199+ }
200+
201+ pub fn copyTo3 < T : Clone > ( set : HashSet < T > , dest : Array < T > , destIndex : i32 , count : i32 ) {
202+ let set_len = set. len ( ) as i32 ;
203+ let dest_len = dest. len ( ) as i32 ;
204+ if destIndex < 0 || count < 0 || count > set_len || destIndex + count > dest_len {
205+ panic ! ( "{}" , indexOutOfBounds( ) ) ;
206+ }
207+ let mut dest = dest. get_mut ( ) ;
208+ let it = to_iter ( & set) . take ( count as usize ) ;
209+ for ( offset, item) in it. enumerate ( ) {
210+ dest[ destIndex as usize + offset] = item;
211+ }
212+ }
213+
214+ pub fn isSubsetOf < T : Clone + ' static > ( set : HashSet < T > , other : Seq < T > ) -> bool {
215+ let other = seq_to_hash_set ( other, set. comparer . clone ( ) ) ;
216+ set. is_subset ( & other)
217+ }
218+
219+ pub fn isSupersetOf < T : Clone + ' static > ( set : HashSet < T > , other : Seq < T > ) -> bool {
220+ let other = seq_to_hash_set ( other, set. comparer . clone ( ) ) ;
221+ set. is_superset ( & other)
222+ }
223+
224+ pub fn isProperSubsetOf < T : Clone + ' static > ( set : HashSet < T > , other : Seq < T > ) -> bool {
225+ let other = seq_to_hash_set ( other, set. comparer . clone ( ) ) ;
226+ set. len ( ) < other. len ( ) && set. is_subset ( & other)
227+ }
228+
229+ pub fn isProperSupersetOf < T : Clone + ' static > ( set : HashSet < T > , other : Seq < T > ) -> bool {
230+ let other = seq_to_hash_set ( other, set. comparer . clone ( ) ) ;
231+ set. len ( ) > other. len ( ) && set. is_superset ( & other)
232+ }
233+
149234 pub fn entries < T : Clone > ( set : HashSet < T > ) -> Array < T > {
150235 array_from ( Vec :: from_iter ( to_iter ( & set) ) )
151236 }
0 commit comments