<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Altinet &#187; .NET</title>
	<atom:link href="https://www.altinet.hr/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.altinet.hr</link>
	<description></description>
	<lastBuildDate>Sat, 08 Apr 2017 09:28:02 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.0.15</generator>
	<item>
		<title>QueueSet, Hashed Queue, HashQueue, Weighted List, Hashed History or some other name, couldn&#8217;t find it online:)</title>
		<link>https://www.altinet.hr/queueset-hashed-queue-hashqueue-weighted-list-hashed-history-or-some-other-name-couldnt-find-it-online/</link>
		<comments>https://www.altinet.hr/queueset-hashed-queue-hashqueue-weighted-list-hashed-history-or-some-other-name-couldnt-find-it-online/#comments</comments>
		<pubDate>Mon, 03 Jan 2011 20:55:55 +0000</pubDate>
		<dc:creator><![CDATA[Bruno Samardzic]]></dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://www.run80.net/?p=54</guid>
		<description><![CDATA[Ok so i had trouble even expressing what I needed so i had to build it myself. So what is it? Well, it&#8217;s basically a hashed queue where when you enqueue again an already queued item, you basically put it in the first spot. It&#8217;s made as a collection but it still acts as a [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Ok so i had trouble even expressing what I needed so i had to build it myself. So what is it?<br />
Well, it&#8217;s basically a hashed queue where when you enqueue again an already queued item, you basically put it in the first spot. It&#8217;s made as a collection but it still acts as a queue. I needed it for my taskbar which automatically closes the least active item, or the one least used. so here you go:</p>
<pre class="brush: csharp; title: ; notranslate">
public class BindableQueueSet&lt;T&gt; : ICollection&lt;T&gt;, INotifyCollectionChanged,INotifyPropertyChanged
    {
        private readonly ObservableCollection&lt;Pair&gt; que;

        private int maxNumber;
        private readonly int capacity;

        #region Private and protected

        private bool AddItemCore(T item)
        {
            bool result;
            var found = que.FirstOrDefault(x =&gt; x.Value.Equals(item));
            if (found == null)
            {
                que.Add(new Pair(item, maxNumber++));
                result = true;
            }
            else
            {
                found.Weight = maxNumber++;
                result = false;
            }
            result = CleanUp() || result;
            return result;
        }


        private Pair Find(T item)
        {
            return que.FirstOrDefault(x =&gt; x.Value.Equals(item));
        }

        private bool CleanUp()
        {
            if (capacity &gt;= que.Count) return false;
            Pair lightestPair = que[0];
            foreach (var pair in que)
            {
                if (pair.Weight &lt; lightestPair.Weight)
                    lightestPair = pair;
            }
            que.Remove(lightestPair);
            return true;
        }

        #endregion

        public BindableQueueSet(int capacity)
        {
            if (capacity &lt; 1) throw new ArgumentException();
            this.capacity = capacity;
            que = new ObservableCollection&lt;Pair&gt;();
            que.CollectionChanged += (s, e) =&gt; OnCollectionChanged(e);
            ((INotifyPropertyChanged) que).PropertyChanged += (s, e) =&gt; OnPropertyChanged(e);
        }

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }

        void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {

            NotifyCollectionChangedEventArgs enew;
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Replace:
                    enew = new NotifyCollectionChangedEventArgs(e.Action,
                                                                e.NewItems != null &amp;&amp; e.NewItems.Count &gt; 0
                                                                    ? ((Pair) e.NewItems[0]).Value:default(T),
                                                                e.OldItems != null &amp;&amp; e.OldItems.Count &gt; 0
                                                                    ? ((Pair) e.OldItems[0]).Value:default(T),
                                                                e.NewStartingIndex);
                    break;
                case NotifyCollectionChangedAction.Add:
                    enew = new NotifyCollectionChangedEventArgs(e.Action,
                                                                e.NewItems != null &amp;&amp; e.NewItems.Count &gt; 0
                                                                    ? ((Pair)e.NewItems[0]).Value : default(T),
                                                                e.NewStartingIndex);
                    break;
                case NotifyCollectionChangedAction.Remove:
                    enew = new NotifyCollectionChangedEventArgs(e.Action,
                                                                e.OldItems != null &amp;&amp; e.OldItems.Count &gt; 0
                                                                    ? ((Pair)e.OldItems[0]).Value : default(T),
                                                                e.OldStartingIndex);
                    break;
                default:
                    enew = new NotifyCollectionChangedEventArgs(e.Action);
                    break;
            }
            if (CollectionChanged != null)
                CollectionChanged(this, enew);
        }



        public IEnumerator&lt;T&gt; GetEnumerator()
        {
            return new QueueEnum(que.GetEnumerator());
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(T item)
        {
            AddItemCore(item);
        }

        public void Clear()
        {
            que.Clear();
        }

        public bool Contains(T item)
        {
            return Find(item) != null;
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            while (arrayIndex &lt; array.Length)
            {
                AddItemCore(array[arrayIndex]);
                arrayIndex++;
            }
        }

        public bool Remove(T item)
        {
            var found = Find(item);
            return que.Remove(found);
        }

        public int Count
        {
            get { return que.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public event NotifyCollectionChangedEventHandler CollectionChanged;

        private class Pair
        {
            public int Weight { get; set; }
            public T Value { get; private set; }
            public Pair(T value, int weight)
            {
                Value = value;
                Weight = weight;
            }
        }

        private class QueueEnum : IEnumerator&lt;T&gt;
        {
            private readonly IEnumerator&lt;Pair&gt; queEnum;

            public QueueEnum(IEnumerator&lt;Pair&gt; queEnum)
            {
                this.queEnum = queEnum;
            }

            public void Dispose()
            {
                queEnum.Dispose();
            }

            public bool MoveNext()
            {
                return queEnum.MoveNext();
            }

            public void Reset()
            {
                queEnum.Reset();
            }

            public T Current
            {
                get { return queEnum.Current.Value; }
            }

            object IEnumerator.Current
            {
                get { return Current; }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
</pre>
]]></content:encoded>
			<wfw:commentRss>https://www.altinet.hr/queueset-hashed-queue-hashqueue-weighted-list-hashed-history-or-some-other-name-couldnt-find-it-online/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
