Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7801427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:56:57+00:00 2026-06-02T00:56:57+00:00

I need a requeue at the nth element where n is defined by an

  • 0

I need a requeue at the nth element where n is defined by an ordered key.

 ConcurrentQueue<KeyValuePair<string, SomeClass>> queue = new ConcurrentQueue<KeyValuePair<string, SomeClass>>();


 queue.RequeueByOrderedKey(key, element)

OR

 queue.RequeueN(index, element)

… since it looks necessary to implement this myself, I’m considering something based on public

  class Class1 : KeyedCollection<K,V>{}

   it'd be nice to have Class1 : OrderedKeyedCollection<K,V>{}

Here’s some code that I did. I’ll put it here for comment, and then probably move it to be an answer. Probably haven’t handled the concurrency stuff properly yet.

    public class QueueExt<TK, TV> : SortedList<TK, TV> {

        #region Constructors

        public QueueExt(Func<TV, TK> getKey = null) {
            GetKey = getKey;
        }

        private Func<TV, TK> GetKey = null;

        public QueueExt(int capacity, Func<TV, TK> getKey = null)
            : base(capacity) {
            GetKey = getKey;
        }

        public QueueExt(IComparer<TK> comparer, Func<TV, TK> getKey = null)
            : base(comparer) {
            GetKey = getKey;
        }

        public QueueExt(int capacity, IComparer<TK> comparer, Func<TV, TK> getKey = null)
            : base(capacity, comparer) {
            GetKey = getKey;
        }

        public QueueExt(IDictionary<TK, TV> dictionary, Func<TV, TK> getKey = null)
            : base(dictionary) {
            GetKey = getKey;
        }

        public QueueExt(IDictionary<TK, TV> dictionary, IComparer<TK> comparer, Func<TV, TK> getKey = null)
            : base(dictionary, comparer) {
            GetKey = getKey;
        }

        #endregion

        public TV Dequeue() {
            lock (this) {
                var first = this.ElementAt(0).Value;
                this.RemoveAt(0);
                return first;
            }
        }

        public void Requeue() {

            if (GetKey == null)
                throw new ArgumentNullException("Key getter lamda must not be null");

            lock (this) {
                var key = this.ElementAt(0).Key;
                var actualkey = GetKey(this.ElementAt(0).Value);
                if (!actualkey.Equals(key)) {
                    this.Enqueue(this.Dequeue());
                }
            }
        }

        public void Enqueue(TK key, TV item) {
            this.Add(key, item);
        }
        public void Enqueue(TV item) {
            if (GetKey == null)
                throw new ArgumentNullException("Key getter lamda must not be null");
            var key = GetKey(item);
            this.Add(key, item);
        }

        public TV Peek() {
            return this.ElementAt(0).Value;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:56:58+00:00Added an answer on June 2, 2026 at 12:56 am

    You can do this with BlockingCollection. You create your indexable queue and make it implement IProducerConsumerCollection. I show how to use BlockingCollection this way in my article Customizing Blocking Collection. I use a stack in the article, but you could easily enough replace the stack with your indexable queue.

    An alternative might be a concurrent priority queue. You can build a simple one with a heap and a lock. See my article A Generic Binary Heap. You’ll need to add the synchronization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to launch a new ASP.NET site on this server but need to be
need your help with PHP templating. I'm new to PHP (I'm coming from Perl+Embperl).
Need some help for creating a File and String search engine. The program needs
Need some help with extracting 2 pieces of information from the following string: viewed
Need a way for one service on a well-known Endpoint to return strings which
Need some help understanding the difference between the success and ajaxSuccess events documented here
Need to choose optimal data structure for fast work with list of locations and
Need some help please with this error : TypeError : 'unicode' object does not
Need help to convert code from asp control to input type to fetch file
Need to design a simple one for school. More specifically a Moore FSM. Im

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.