예제 #1
0
파일: HashBag.cs 프로젝트: ggeurts/nhive
        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();
            }
        }
예제 #2
0
파일: HashBag.cs 프로젝트: ggeurts/nhive
        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);
        }
예제 #3
0
파일: HashBag.cs 프로젝트: ggeurts/nhive
        /// <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);
        }
예제 #4
0
        public override bool Equals(object obj)
        {
            if (!(obj is Multiplicity <T>))
            {
                return(false);
            }
            Multiplicity <T> other = (Multiplicity <T>)obj;

            return(Equals(other));
        }
예제 #5
0
파일: HashBag.cs 프로젝트: ggeurts/nhive
        public virtual int ContainsCount(T item)
        {
            Multiplicity <T> p = new Multiplicity <T>(item, 0);

            if (dict.Find(ref p))
            {
                return(p.Count);
            }

            return(0);
        }
예제 #6
0
파일: HashBag.cs 프로젝트: ggeurts/nhive
        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);
        }
예제 #7
0
파일: HashBag.cs 프로젝트: ggeurts/nhive
        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++;
        }
예제 #8
0
파일: HashBag.cs 프로젝트: ggeurts/nhive
        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();
                }
            }
        }
예제 #9
0
 public bool Equals(Multiplicity <T> other)
 {
     return(EqualityComparer <T> .Default.Equals(this._value, other._value) &&
            this._count == other._count);
 }
예제 #10
0
 /// <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));
 }
예제 #11
0
 /// <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));
 }
예제 #12
0
파일: HashBag.cs 프로젝트: ggeurts/nhive
        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();
            }
        }
예제 #13
0
 public override T Map(Multiplicity<T> kvp) 
 { 
     return kvp.Value; 
 }