| 1 | use craterust-analyzer cargo xml-rs 0.8.19 crate/::attributerust-analyzer cargo xml-rs 0.8.19 attribute/::OwnedAttributerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#; |
| 2 | use craterust-analyzer cargo xml-rs 0.8.19 crate/::namerust-analyzer cargo xml-rs 0.8.19 name/::OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#; |
| 3 | |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::hash_maprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash_map/::RandomStaterust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/RandomState#; |
| 5 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#; |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/::BuildHasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher#; |
| 7 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/::Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#; |
| 8 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/::Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher#; |
| 9 | |
| 10 | /// An ordered set |
| 11 | pub(craterust-analyzer cargo xml-rs 0.8.19 crate/) struct AttributesSetrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet# { |
| 12 | vecrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#vec.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<OwnedAttributerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#>, |
| 13 | /// Uses a no-op hasher, because these u64s are hashes already |
| 14 | may_containrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#may_contain.: HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#<u64, U64HasherBuilderrust-analyzer cargo xml-rs 0.8.19 reader/indexset/U64HasherBuilder#>, |
| 15 | /// This is real hasher for the `OwnedName` |
| 16 | hasherrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#hasher.: RandomStaterust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/RandomState#, |
| 17 | } |
| 18 | |
| 19 | /// Use linear search and don't allocate `HashSet` if there are few attributes, |
| 20 | /// because allocation costs more than a few comparisons. |
| 21 | const HASH_THRESHOLDrust-analyzer cargo xml-rs 0.8.19 reader/indexset/HASH_THRESHOLD.: usize = 8; |
| 22 | |
| 23 | impl AttributesSetrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet# { |
| 24 | pub fn newrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]new().() -> Selfrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet] { |
| 25 | Selfrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet] { |
| 26 | vecrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#vec.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(), |
| 27 | hasherrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#hasher.: RandomStaterust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/RandomState#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/impl#[RandomState]new().(), |
| 28 | may_containrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#may_contain.: HashSetrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/HashSet#::defaultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S>`][Default]default().(), |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | fn hashrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]hash().(&self, val: &OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#) -> u64 { |
| 33 | let mut h = self.hasherrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#hasher..build_hasherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/impl#[RandomState][BuildHasher]build_hasher().(); |
| 34 | val.hashrust-analyzer cargo xml-rs 0.8.19 name/impl#[OwnedName][Hash]hash().(&mut h); |
| 35 | h.finishrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/random/impl#[DefaultHasher][Hasher]finish().() |
| 36 | } |
| 37 | |
| 38 | pub fn lenrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]len().(&self) -> usize { |
| 39 | self.vecrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#vec..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() |
| 40 | } |
| 41 | |
| 42 | pub fn containsrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]contains().(&self, name: &OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#) -> bool { |
| 43 | // fall back to linear search only on duplicate or hash collision |
| 44 | (self.vecrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#vec..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().HASH_THRESHOLDrust-analyzer cargo xml-rs 0.8.19 reader/indexset/HASH_THRESHOLD. || self.may_containrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#may_contain..containsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]contains().(&self.hashrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]hash().(name))) && |
| 45 | self.vecrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#vec..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]any().(move |a| &a.namerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#name. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().name) |
| 46 | } |
| 47 | |
| 48 | pub fn pushrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]push().(&mut self, attr: OwnedAttributerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#) { |
| 49 | if self.vecrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#vec..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().HASH_THRESHOLDrust-analyzer cargo xml-rs 0.8.19 reader/indexset/HASH_THRESHOLD. { |
| 50 | if self.vecrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#vec..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().HASH_THRESHOLDrust-analyzer cargo xml-rs 0.8.19 reader/indexset/HASH_THRESHOLD. { |
| 51 | self.may_containrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#may_contain..reserverust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]reserve().(HASH_THRESHOLDrust-analyzer cargo xml-rs 0.8.19 reader/indexset/HASH_THRESHOLD. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().2); |
| 52 | for attr in &self.vecrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#vec. { |
| 53 | self.may_containrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#may_contain..insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]insert().(self.hashrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]hash().(&attr.namerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#name.)); |
| 54 | } |
| 55 | } |
| 56 | self.may_containrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#may_contain..insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/set/impl#[`HashSet<T, S, A>`]insert().(self.hashrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]hash().(&attr.namerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#name.)); |
| 57 | } |
| 58 | self.vecrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#vec..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(attr); |
| 59 | } |
| 60 | |
| 61 | pub fn into_vecrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]into_vec().(self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<OwnedAttributerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#> { |
| 62 | self.vecrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#vec. |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 67 | fn indexsetrust-analyzer cargo xml-rs 0.8.19 reader/indexset/indexset().() { |
| 68 | let mut s = AttributesSetrust-analyzer cargo xml-rs 0.8.19 reader/indexset/AttributesSet#::newrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]new().(); |
| 69 | let not_here = OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# { |
| 70 | local_namerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#local_name.: "attr1000".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 71 | namespacerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#namespace.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("test".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), |
| 72 | prefixrust-analyzer cargo xml-rs 0.8.19 name/OwnedName#prefix.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 73 | }; |
| 74 | |
| 75 | // this test will take a lot of time if the `contains()` is linear, and the loop is quadratic |
| 76 | for i in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#50000 { |
| 77 | let name = OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# { |
| 78 | local_namerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#local_name.: formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("attr{i}"), namespacerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#namespace.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, prefixrust-analyzer cargo xml-rs 0.8.19 name/OwnedName#prefix.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 79 | }; |
| 80 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().s.containsrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]contains().(&name)); |
| 81 | |
| 82 | s.pushrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]push().(OwnedAttributerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute# { namerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#name., valuerust-analyzer cargo xml-rs 0.8.19 attribute/OwnedAttribute#value.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().() }); |
| 83 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().s.containsrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]contains().(¬_here)); |
| 84 | } |
| 85 | |
| 86 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(s.containsrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]contains().(&OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# { |
| 87 | local_namerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#local_name.: "attr1234".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), namespacerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#namespace.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, prefixrust-analyzer cargo xml-rs 0.8.19 name/OwnedName#prefix.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 88 | })); |
| 89 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(s.containsrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]contains().(&OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# { |
| 90 | local_namerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#local_name.: "attr0".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), namespacerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#namespace.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, prefixrust-analyzer cargo xml-rs 0.8.19 name/OwnedName#prefix.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 91 | })); |
| 92 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(s.containsrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[AttributesSet]contains().(&OwnedNamerust-analyzer cargo xml-rs 0.8.19 name/OwnedName# { |
| 93 | local_namerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#local_name.: "attr49999".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), namespacerust-analyzer cargo xml-rs 0.8.19 name/OwnedName#namespace.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, prefixrust-analyzer cargo xml-rs 0.8.19 name/OwnedName#prefix.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 94 | })); |
| 95 | } |
| 96 | |
| 97 | /// Hashser that does nothing except passing u64 through |
| 98 | struct U64Hasherrust-analyzer cargo xml-rs 0.8.19 reader/indexset/U64Hasher#(u64); |
| 99 | |
| 100 | impl Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher# for U64Hasherrust-analyzer cargo xml-rs 0.8.19 reader/indexset/U64Hasher# { |
| 101 | fn finishrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[U64Hasher][Hasher]finish().(&self) -> u64 { self.0rust-analyzer cargo xml-rs 0.8.19 reader/indexset/U64Hasher#0. } |
| 102 | fn writerust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[U64Hasher][Hasher]write().(&mut self, slice: &[u8]) { |
| 103 | for &v in slice { self.0rust-analyzer cargo xml-rs 0.8.19 reader/indexset/U64Hasher#0. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXorAssign<Self>`]bitxor_assign().^=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXorAssign<Self>`]bitxor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXorAssign<Self>`]bitxor_assign().u64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u64][`From<u8>`]from().(v) } // unused in practice |
| 104 | } |
| 105 | fn write_u64rust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[U64Hasher][Hasher]write_u64().(&mut self, i: u64) { |
| 106 | self.0rust-analyzer cargo xml-rs 0.8.19 reader/indexset/U64Hasher#0. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXorAssign<Self>`]bitxor_assign().^=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXorAssign<Self>`]bitxor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitXorAssign<Self>`]bitxor_assign().i; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 111 | struct U64HasherBuilderrust-analyzer cargo xml-rs 0.8.19 reader/indexset/U64HasherBuilder#; |
| 112 | |
| 113 | impl BuildHasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/BuildHasher# for U64HasherBuilderrust-analyzer cargo xml-rs 0.8.19 reader/indexset/U64HasherBuilder# { |
| 114 | type Hasherrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[U64HasherBuilder][BuildHasher]Hasher# = U64Hasherrust-analyzer cargo xml-rs 0.8.19 reader/indexset/U64Hasher#; |
| 115 | fn build_hasherrust-analyzer cargo xml-rs 0.8.19 reader/indexset/impl#[U64HasherBuilder][BuildHasher]build_hasher().(&self) -> U64Hasherrust-analyzer cargo xml-rs 0.8.19 reader/indexset/U64Hasher# { U64Hasherrust-analyzer cargo xml-rs 0.8.19 reader/indexset/U64Hasher#(0) } |
| 116 | } |