/// <summary> Returns the set of items that are in either this set or the provided set </summary> /// <example>{ 1, 2, 3 }.UnionWith({ 2, 3, 4 }) == { 1, 2, 3, 4 }</example> public SetList <T> UnionWith(SetList <T> other) { SetList <T> copy = this.Clone(); copy.AddRange(other._list); return(copy); }
/// <summary> Returns true if all items in the provided set are also in this set </summary> /// <example>{ 1, 2, 3, 4 }.IsSupersetOf({ 1, 2, 4 }) == true && { 1 }.IsSupersetOf({}) == true</example> public bool IsSupersetOf(SetList <T> other) { int pos; foreach (T item in other._list) { pos = _list.BinarySearch(item, _comparer); if (pos < 0) { return(false); } } return(true); }
/// <summary> Returns the items in this set that are not in the provided set </summary> /// <example>{ 1, 2, 3 }.RemoveAll({ 2, 3, 4 }) == { 1 }</example> public SetList <T> SubtractSet(SetList <T> other) { List <T> result = new List <T>(_list.Count); int pos; foreach (T item in _list) { pos = other._list.BinarySearch(item, _comparer); if (pos < 0) { result.Add(item); } } return(new SetList <T>(result, _comparer)); }
/// <summary> Returns the items in the provided set that are not in this set </summary> /// <example>{ 1, 2, 3 }.ComplementOf({ 2, 3, 4 }) == { 4 }</example> public SetList <T> ComplementOf(SetList <T> other) { List <T> result = new List <T>(other._list.Count); int pos; foreach (T item in other._list) { pos = _list.BinarySearch(item, _comparer); if (pos < 0) { result.Add(item); } } return(new SetList <T>(result, _comparer)); }
/// <summary> Returns the set of items that are in both this set and the provided set </summary> /// <example>{ 1, 2, 3 }.IntersectWith({ 2, 3, 4 }) == { 2, 3 }</example> public SetList <T> IntersectWith(SetList <T> other) { List <T> result = new List <T>(Math.Max(0, _list.Count - other._list.Count)); int pos; foreach (T item in other._list) { pos = _list.BinarySearch(item, _comparer); if (pos >= 0) { result.Add(item); } } return(new SetList <T>(result, _comparer)); }
/// <summary> Returns true if all items in this set are also in the provided set </summary> /// <example>{ 1, 2 }.IsEqualTo({ 1, 2 }) == true && {}.IsEqualTo({}) == true</example> public bool IsEqualTo(SetList <T> other) { if (_list.Count != other._list.Count) { return(false); } for (int i = 0; i < _list.Count; i++) { if (_comparer.Compare(_list[i], other._list[i]) != 0) { return(false); } } return(true); }
/// <summary> Returns the items in this set that are not in the provided set </summary> /// <example>{ 1, 2, 3 }.ExclusiveOrWith({ 2, 3, 4 }) == { 1, 4 }</example> public SetList <T> ExclusiveOrWith(SetList <T> other) { List <T> result = new List <T>(other._list.Count); int pos; foreach (T item in other._list) { pos = _list.BinarySearch(item, _comparer); if (pos < 0) { result.Add(item); } } foreach (T item in _list) { pos = other._list.BinarySearch(item, _comparer); if (pos < 0) { pos = result.BinarySearch(item, _comparer); result.Insert(~pos, item); } } return(new SetList <T>(result, _comparer)); }