pub fn insert(&mut self, e: u32) -> bool {
        match self.internal_mut() {
            InternalMut::Empty => {
                if let Some(t) = Tiny::from_singleton(e) {
                    *self = SetU32::tiny(t);
                } else if e < 64 {
                    // println!("allocating dense from empty");
                    *self = SetU32::dense_for_mx(e+1);
                    self.insert(e);
                } else {
                    // println!("allocating from empty");
                    *self = SetU32::table_with_cap(1);
                    self.insert(e);
                }
                false
            }
            InternalMut::Tiny(mut t) => {
                if let Some(b) = t.insert(e) {
                    *self = SetU32::tiny(t);
                    b
                } else if e < 128 && t.max().unwrap() < 128 {
                    // println!("===allocating dense from tiny tinylen {} for e {}", t.len(), e);
                    let mx = if e > t.max().unwrap() { e } else { t.max().unwrap() };
                    *self = SetU32::dense_for_mx(mx);
                    for x in t {
                        // println!("===  inserting {}", x);
                        self.insert(x);
                    }
                    // println!("===  inserting e {}", e);
                    self.insert(e);
                    // println!("===should now have dense with len 2: {}", self.len());
                    false
                } else {
                    // println!("== allocating from tiny");
                    *self = SetU32::table_with_cap(1);
                    // self.mem_used();
                    for x in t {
                        // println!("== inserting {}", x);
                        self.insert(x);
                    }
                    // println!("== inserting e {}", e);
                    // self.mem_used();
                    // println!("== DONE");
                    self.insert(e);
                    false
                }
            }
            InternalMut::Dense { sz, a } => {
                let key = e as usize >> 5;
                let bit = 1 << (e & 31);
                if a.len() > key {
                    let was_here = a[key] & bit != 0;
                    a[key] = a[key] | bit;
                    if !was_here {
                        *sz += 1;
                    }
                    was_here
                } else {
                    if key > 3*(*sz as usize) {
                        let nkeys = a.iter().filter(|&&x| x != 0).count() as u32;
                        // println!("thing is getting sparse adding {} new cap {} from dense nkeys {}/{} with sz {}!",
                        //          e, log2(nkeys+1), nkeys, a.len(), *sz);
                        // It is getting sparse, so let us switch back
                        // to a non-hash table.
                        let mut new = SetU32::table_with_cap(log2(nkeys+1));
                        // println!("   Dense thing was {:?}",
                        //          DenseIter::new( *sz as usize, a ).collect::<Vec<_>>());
                        for x in DenseIter::new( *sz as usize, a ) {
                            new.insert(x);
                            // println!("      addinng {}",
                            //          x,
                            //          // new.iter().collect::<Vec<_>>(),
                            // );
                        }
                        new.insert(e);
                        *self = new;
                    } else {
                        *sz += 1;
                        unsafe { self.dense_increase_mx(e)[key] = bit };
                    }
                    false
                }
            }
            InternalMut::Table { sz, cap_ptr, a, available } => {
                // assert_eq!(available,
                //            a.iter().filter(|&x| x == &(0,0)).count() as u32);
                let (key, bits) = to_key_and_bit(e);
                // println!("   inserting {} with {}/{} available", e, available, a.len());
                if available < a.len() as u32 >> 4 {
                    let cap = a.len().trailing_zeros();
                    let newcap = cap + 1;
                    // println!("cap {} from {} -> {}", cap, a.len(), newcap);
                    let mut new = SetU32::table_with_cap(newcap);
                    if let InternalMut::Table { a: newa, sz: newsz, cap_ptr, .. } = new.internal_mut() {
                        *newsz = *sz;
                        for x in a.iter() {
                            let idx = p_insert_better(x.0, newa);
                            newa[idx.unwrap()] = *x;
                        }
                        let av = newa.iter().filter(|&x| x == &(0,0)).count() as u32;
                        *cap_ptr = (av << 5) | newcap;
                    }
                    *self = new;
                    return self.insert(e);
                }
                match p_insert_better(key, a) {
                    Inserted::Present(i) => {
                        if a[i].1 & bits == 0 {
                            a[i].1 = a[i].1 | bits;
                            *sz += 1;
                            // println!("=> key already here");
                            false
                        } else {
                            // println!("=> already here");
                            true
                        }
                    }
                    Inserted::EmptySpot(i) => {
                        a[i] = (key, bits);
                        *sz += 1;
                        *cap_ptr -= 32;
                        // println!("=> found an empty spot");
                        false
                    }
                    Inserted::NoRoom => {
                        // Need a bigger table!
                        // println!("=> need bigger table");
                        let mx = TableIter::new(*sz as usize, a).max().unwrap();
                        let mut new = match decide_set_type(mx, *sz) {
                            SetType::Dense => {
                                let mut new = SetU32::dense_for_mx(mx);
                                for x in self.iter() {
                                    new.insert(x);
                                }
                                new
                            }
                            SetType::Table => {
                                let cap = a.len().trailing_zeros();
                                let newcap = cap + 1;
                                // println!("XXX    {:?}", a);
                                // println!("XXX foo cap {} from {} -> {}", cap, a.len(), newcap);
                                let mut new = SetU32::table_with_cap(newcap);
                                if let InternalMut::Table { a: newa, sz: newsz, cap_ptr, .. } = new.internal_mut() {
                                    *newsz = *sz;
                                    for x in a.iter() {
                                        let idx = p_insert_better(x.0, newa);
                                        newa[idx.unwrap()] = *x;
                                    }
                                    let av = newa.iter().filter(|&x| x == &(0,0)).count() as u32;
                                    *cap_ptr = (av << 5) | newcap;
                                }
                                new
                            }
                        };
                        new.insert(e);
                        *self = new;
                        false
                    }
                }
            }
        }
    }
/// Insert a number into the set.
///
/// Return a bool indicating if it was already present.
Insert a number into the set.
Return a bool indicating if it was already present.
Insert a number into the set . Return a bool indicating if it was already present .
pub fn insert(&mut self, e: u32) -> bool {
        match self.internal_mut() {
            InternalMut::Empty => {
                if let Some(t) = Tiny::from_singleton(e) {
                    *self = SetU32::tiny(t);
                } else if e < 64 {
                    // println!("allocating dense from empty");
                    *self = SetU32::dense_for_mx(e+1);
                    self.insert(e);
                } else {
                    // println!("allocating from empty");
                    *self = SetU32::table_with_cap(1);
                    self.insert(e);
                }
                false
            }
            InternalMut::Tiny(mut t) => {
                if let Some(b) = t.insert(e) {
                    *self = SetU32::tiny(t);
                    b
                } else if e < 128 && t.max().unwrap() < 128 {
                    // println!("===allocating dense from tiny tinylen {} for e {}", t.len(), e);
                    let mx = if e > t.max().unwrap() { e } else { t.max().unwrap() };
                    *self = SetU32::dense_for_mx(mx);
                    for x in t {
                        // println!("===  inserting {}", x);
                        self.insert(x);
                    }
                    // println!("===  inserting e {}", e);
                    self.insert(e);
                    // println!("===should now have dense with len 2: {}", self.len());
                    false
                } else {
                    // println!("== allocating from tiny");
                    *self = SetU32::table_with_cap(1);
                    // self.mem_used();
                    for x in t {
                        // println!("== inserting {}", x);
                        self.insert(x);
                    }
                    // println!("== inserting e {}", e);
                    // self.mem_used();
                    // println!("== DONE");
                    self.insert(e);
                    false
                }
            }
            InternalMut::Dense { sz, a } => {
                let key = e as usize >> 5;
                let bit = 1 << (e & 31);
                if a.len() > key {
                    let was_here = a[key] & bit != 0;
                    a[key] = a[key] | bit;
                    if !was_here {
                        *sz += 1;
                    }
                    was_here
                } else {
                    if key > 3*(*sz as usize) {
                        let nkeys = a.iter().filter(|&&x| x != 0).count() as u32;
                        // println!("thing is getting sparse adding {} new cap {} from dense nkeys {}/{} with sz {}!",
                        //          e, log2(nkeys+1), nkeys, a.len(), *sz);
                        // It is getting sparse, so let us switch back
                        // to a non-hash table.
                        let mut new = SetU32::table_with_cap(log2(nkeys+1));
                        // println!("   Dense thing was {:?}",
                        //          DenseIter::new( *sz as usize, a ).collect::<Vec<_>>());
                        for x in DenseIter::new( *sz as usize, a ) {
                            new.insert(x);
                            // println!("      addinng {}",
                            //          x,
                            //          // new.iter().collect::<Vec<_>>(),
                            // );
                        }
                        new.insert(e);
                        *self = new;
                    } else {
                        *sz += 1;
                        unsafe { self.dense_increase_mx(e)[key] = bit };
                    }
                    false
                }
            }
            InternalMut::Table { sz, cap_ptr, a, available } => {
                // assert_eq!(available,
                //            a.iter().filter(|&x| x == &(0,0)).count() as u32);
                let (key, bits) = to_key_and_bit(e);
                // println!("   inserting {} with {}/{} available", e, available, a.len());
                if available < a.len() as u32 >> 4 {
                    let cap = a.len().trailing_zeros();
                    let newcap = cap + 1;
                    // println!("cap {} from {} -> {}", cap, a.len(), newcap);
                    let mut new = SetU32::table_with_cap(newcap);
                    if let InternalMut::Table { a: newa, sz: newsz, cap_ptr, .. } = new.internal_mut() {
                        *newsz = *sz;
                        for x in a.iter() {
                            let idx = p_insert_better(x.0, newa);
                            newa[idx.unwrap()] = *x;
                        }
                        let av = newa.iter().filter(|&x| x == &(0,0)).count() as u32;
                        *cap_ptr = (av << 5) | newcap;
                    }
                    *self = new;
                    return self.insert(e);
                }
                match p_insert_better(key, a) {
                    Inserted::Present(i) => {
                        if a[i].1 & bits == 0 {
                            a[i].1 = a[i].1 | bits;
                            *sz += 1;
                            // println!("=> key already here");
                            false
                        } else {
                            // println!("=> already here");
                            true
                        }
                    }
                    Inserted::EmptySpot(i) => {
                        a[i] = (key, bits);
                        *sz += 1;
                        *cap_ptr -= 32;
                        // println!("=> found an empty spot");
                        false
                    }
                    Inserted::NoRoom => {
                        // Need a bigger table!
                        // println!("=> need bigger table");
                        let mx = TableIter::new(*sz as usize, a).max().unwrap();
                        let mut new = match decide_set_type(mx, *sz) {
                            SetType::Dense => {
                                let mut new = SetU32::dense_for_mx(mx);
                                for x in self.iter() {
                                    new.insert(x);
                                }
                                new
                            }
                            SetType::Table => {
                                let cap = a.len().trailing_zeros();
                                let newcap = cap + 1;
                                // println!("XXX    {:?}", a);
                                // println!("XXX foo cap {} from {} -> {}", cap, a.len(), newcap);
                                let mut new = SetU32::table_with_cap(newcap);
                                if let InternalMut::Table { a: newa, sz: newsz, cap_ptr, .. } = new.internal_mut() {
                                    *newsz = *sz;
                                    for x in a.iter() {
                                        let idx = p_insert_better(x.0, newa);
                                        newa[idx.unwrap()] = *x;
                                    }
                                    let av = newa.iter().filter(|&x| x == &(0,0)).count() as u32;
                                    *cap_ptr = (av << 5) | newcap;
                                }
                                new
                            }
                        };
                        new.insert(e);
                        *self = new;
                        false
                    }
                }
            }
        }
    }
pub fn insert ( & mut self , e : u32 ) -> bool { match self . internal_mut ( ) { InternalMut :: Empty => { if let Some ( t ) = Tiny :: from_singleton ( e ) { * self = SetU32 :: tiny ( t ) ; } else if e < 64 { // println!("allocating dense from empty"); * self = SetU32 :: dense_for_mx ( e + 1 ) ; self . insert ( e ) ; } else { // println!("allocating from empty"); * self = SetU32 :: table_with_cap ( 1 ) ; self . insert ( e ) ; } false } InternalMut :: Tiny ( mut t ) => { if let Some ( b ) = t . insert ( e ) { * self = SetU32 :: tiny ( t ) ; b } else if e < 128 && t . max ( ) . unwrap ( ) < 128 { // println!("===allocating dense from tiny tinylen {} for e {}", t.len(), e); let mx = if e > t . max ( ) . unwrap ( ) { e } else { t . max ( ) . unwrap ( ) } ; * self = SetU32 :: dense_for_mx ( mx ) ; for x in t { // println!("===  inserting {}", x); self . insert ( x ) ; } // println!("===  inserting e {}", e); self . insert ( e ) ; // println!("===should now have dense with len 2: {}", self.len()); false } else { // println!("== allocating from tiny"); * self = SetU32 :: table_with_cap ( 1 ) ; // self.mem_used(); for x in t { // println!("== inserting {}", x); self . insert ( x ) ; } // println!("== inserting e {}", e); // self.mem_used(); // println!("== DONE"); self . insert ( e ) ; false } } InternalMut :: Dense { sz , a } => { let key = e as usize >> 5 ; let bit = 1 << ( e & 31 ) ; if a . len ( ) > key { let was_here = a [ key ] & bit != 0 ; a [ key ] = a [ key ] | bit ; if ! was_here { * sz += 1 ; } was_here } else { if key > 3 * ( * sz as usize ) { let nkeys = a . iter ( ) . filter ( | & & x | x != 0 ) . count ( ) as u32 ; // println!("thing is getting sparse adding {} new cap {} from dense nkeys {}/{} with sz {}!", //          e, log2(nkeys+1), nkeys, a.len(), *sz); // It is getting sparse, so let us switch back // to a non-hash table. let mut new = SetU32 :: table_with_cap ( log2 ( nkeys + 1 ) ) ; // println!("   Dense thing was {:?}", //          DenseIter::new( *sz as usize, a ).collect::<Vec<_>>()); for x in DenseIter :: new ( * sz as usize , a ) { new . insert ( x ) ; // println!("      addinng {}", //          x, //          // new.iter().collect::<Vec<_>>(), // ); } new . insert ( e ) ; * self = new ; } else { * sz += 1 ; unsafe { self . dense_increase_mx ( e ) [ key ] = bit } ; } false } } InternalMut :: Table { sz , cap_ptr , a , available } => { // assert_eq!(available, //            a.iter().filter(|&x| x == &(0,0)).count() as u32); let ( key , bits ) = to_key_and_bit ( e ) ; // println!("   inserting {} with {}/{} available", e, available, a.len()); if available < a . len ( ) as u32 >> 4 { let cap = a . len ( ) . trailing_zeros ( ) ; let newcap = cap + 1 ; // println!("cap {} from {} -> {}", cap, a.len(), newcap); let mut new = SetU32 :: table_with_cap ( newcap ) ; if let InternalMut :: Table { a : newa , sz : newsz , cap_ptr , .. } = new . internal_mut ( ) { * newsz = * sz ; for x in a . iter ( ) { let idx = p_insert_better ( x . 0 , newa ) ; newa [ idx . unwrap ( ) ] = * x ; } let av = newa . iter ( ) . filter ( | & x | x == & ( 0 , 0 ) ) . count ( ) as u32 ; * cap_ptr = ( av << 5 ) | newcap ; } * self = new ; return self . insert ( e ) ; } match p_insert_better ( key , a ) { Inserted :: Present ( i ) => { if a [ i ] . 1 & bits == 0 { a [ i ] . 1 = a [ i ] . 1 | bits ; * sz += 1 ; // println!("=> key already here"); false } else { // println!("=> already here"); true } } Inserted :: EmptySpot ( i ) => { a [ i ] = ( key , bits ) ; * sz += 1 ; * cap_ptr -= 32 ; // println!("=> found an empty spot"); false } Inserted :: NoRoom => { // Need a bigger table! // println!("=> need bigger table"); let mx = TableIter :: new ( * sz as usize , a ) . max ( ) . unwrap ( ) ; let mut new = match decide_set_type ( mx , * sz ) { SetType :: Dense => { let mut new = SetU32 :: dense_for_mx ( mx ) ; for x in self . iter ( ) { new . insert ( x ) ; } new } SetType :: Table => { let cap = a . len ( ) . trailing_zeros ( ) ; let newcap = cap + 1 ; // println!("XXX    {:?}", a); // println!("XXX foo cap {} from {} -> {}", cap, a.len(), newcap); let mut new = SetU32 :: table_with_cap ( newcap ) ; if let InternalMut :: Table { a : newa , sz : newsz , cap_ptr , .. } = new . internal_mut ( ) { * newsz = * sz ; for x in a . iter ( ) { let idx = p_insert_better ( x . 0 , newa ) ; newa [ idx . unwrap ( ) ] = * x ; } let av = newa . iter ( ) . filter ( | & x | x == & ( 0 , 0 ) ) . count ( ) as u32 ; * cap_ptr = ( av << 5 ) | newcap ; } new } } ; new . insert ( e ) ; * self = new ; false } } } } }
Insert a number into the set.
Insert a number into the set .
&mut self
e
u32
&mut self
e
u32