public virtual void RemoveAll <U>(SCG.IEnumerable <U> items) where U : T { #warning Improve if items is a counting bag updatecheck(); bool mustRaise = (ActiveEvents & (EventTypeEnum.Changed | EventTypeEnum.Removed)) != 0; RaiseForRemoveAllHandler raiseHandler = mustRaise ? new RaiseForRemoveAllHandler(this) : null; foreach (U item in items) { Multiplicity <T> p = new Multiplicity <T>(item, 0); if (dict.Find(ref p)) { Size--; if (p.Count == 1) { dict.Remove(p); } else { p.Count--; dict.Update(p); } if (mustRaise) { raiseHandler.Remove(p.Value); } } } if (mustRaise) { raiseHandler.Raise(); } }
public virtual bool Remove(T item, out T removeditem) { updatecheck(); Multiplicity <T> p = new Multiplicity <T>(item, 0); if (dict.Find(ref p)) { removeditem = p.Value; Size--; if (p.Count == 1) { dict.Remove(p); } else { p.Count--; dict.Update(p); } if (ActiveEvents != 0) { raiseForRemove(removeditem); } return(true); } removeditem = default(T); return(false); }
/// <summary> /// /// </summary> /// <param name="item"></param> /// <param name="olditem"></param> /// <returns></returns> public virtual bool Update(T item, out T olditem) { Multiplicity <T> p = new Multiplicity <T>(item, 0); updatecheck(); //Note: we cannot just do dict.Update: we have to lookup the count before we //know what to update with. There is of course a way around if we use the //implementation of hashset -which we do not want to do. //The hashbag is moreover mainly a proof of concept if (dict.Find(ref p)) { olditem = p.Value; p.Value = item; dict.Update(p); if (ActiveEvents != 0) { raiseForUpdate(item, olditem, p.Count); } return(true); } olditem = default(T); return(false); }
public override bool Equals(object obj) { if (!(obj is Multiplicity <T>)) { return(false); } Multiplicity <T> other = (Multiplicity <T>)obj; return(Equals(other)); }
public virtual int ContainsCount(T item) { Multiplicity <T> p = new Multiplicity <T>(item, 0); if (dict.Find(ref p)) { return(p.Count); } return(0); }
public virtual bool Find(ref T item) { Multiplicity <T> p = new Multiplicity <T>(item, 0); if (dict.Find(ref p)) { item = p.Value; return(true); } return(false); }
private void add(ref T item) { Multiplicity <T> p = new Multiplicity <T>(item, 1); if (dict.Find(ref p)) { p.Count++; dict.Update(p); item = p.Value; } else { dict.Add(p); } Size++; }
public virtual void RemoveAllCopies(T item) { updatecheck(); Multiplicity <T> p = new Multiplicity <T>(item, 0); if (dict.Find(ref p)) { Size -= p.Count; dict.Remove(p); if ((ActiveEvents & EventTypeEnum.Removed) != 0) { raiseItemsRemoved(p.Value, p.Count); } if ((ActiveEvents & EventTypeEnum.Changed) != 0) { raiseCollectionChanged(); } } }
public bool Equals(Multiplicity <T> other) { return(EqualityComparer <T> .Default.Equals(this._value, other._value) && this._count == other._count); }
/// <summary> /// Get the hash code of the entry /// </summary> /// <param name="obj">The entry</param> /// <returns>The hash code of the entry value</returns> public int GetHashCode(Multiplicity <T> obj) { return(_valueEqualityComparer.GetHashCode(obj.Value)); }
/// <summary> /// Test two entries for equality /// </summary> /// <param name="x">First entry</param> /// <param name="y">Second entry</param> /// <returns>True if entry values are equal</returns> public bool Equals(Multiplicity <T> x, Multiplicity <T> y) { return(_valueEqualityComparer.Equals(x.Value, y.Value)); }
public virtual void RetainAll <U>(SCG.IEnumerable <U> items) where U : T { updatecheck(); HashBag <T> res = new HashBag <T>(ItemEqualityComparer); foreach (U item in items) { Multiplicity <T> p = new Multiplicity <T>(item); if (dict.Find(ref p)) { Multiplicity <T> q = p; if (res.dict.Find(ref q)) { if (q.Count < p.Count) { q.Count++; res.dict.Update(q); res.Size++; } } else { q.Count = 1; res.dict.Add(q); res.Size++; } } } if (Size == res.Size) { return; } CircularQueue <T> wasRemoved = null; if ((ActiveEvents & EventTypeEnum.Removed) != 0) { wasRemoved = new CircularQueue <T>(); foreach (Multiplicity <T> p in dict) { int removed = p.Count - res.ContainsCount(p.Value); if (removed > 0) #warning We could send bag events here easily using a CircularQueue of (should?) { for (int i = 0; i < removed; i++) { wasRemoved.Enqueue(p.Value); } } } } dict = res.dict; Size = res.Size; if ((ActiveEvents & EventTypeEnum.Removed) != 0) { raiseForRemoveAll(wasRemoved); } else if ((ActiveEvents & EventTypeEnum.Changed) != 0) { raiseCollectionChanged(); } }
public override T Map(Multiplicity<T> kvp) { return kvp.Value; }