private IDictionary <MultisetField, MultisetField> ToMultiset() { Debug.Assert(this.values != null, "values is null"); var dict = new Dictionary <MultisetField, MultisetField>(); foreach (var v in this.values) { var field = new MultisetField(new SymbolToken(v.FieldNameSymbol.Text, v.FieldNameSymbol.Sid), v) { Count = 1, }; // assume that for most cases, the field name is unique. if (dict.TryGetValue(field, out var existing)) { existing.Count += 1; } else { dict.Add(field, field); } } return(dict); }
/// <inheritdoc /> /// <remarks> /// Struct equivalence is an expensive operation. /// </remarks> public override bool IsEquivalentTo(IonValue other) { if (!(other is IonStruct otherStruct)) { return(false); } if (NullFlagOn()) { return(other.IsNull); } if (other.IsNull || otherStruct.Count != Count) { return(false); } if (_values.Count != otherStruct._values.Count) { return(false); } var multiset = ToMultiset(); foreach (var v2 in otherStruct._values) { var field = new MultisetField(v2.FieldNameSymbol.Text, v2); if (!multiset.TryGetValue(field, out var mapped) || mapped.Count == 0) { return(false); } mapped.Count--; } return(true); }