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 7076491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:18:16+00:00 2026-05-28T06:18:16+00:00

I have to implement a kind of an array or sequence or list, which

  • 0

I have to implement a kind of an array or sequence or list, which supports the cheapest way of circulated forwarding and back winding of elements. See this example:

Original sequence: 1 2 3 4 5

Forwarded once: 5 1 2 3 4
Forwarded twice: 4 5 1 2 3

Same but opposite is for the back winding. What would be the cheapest and most Scala-style way of implementing this? In Java I could use LinkedList and it would do great… However, I could not find any definite answer for Scala.

Also, it also has to be easy to replace any given element by index, as in LinkedList.

UPDATE:

For the fastest, but not-so-idiomatic variant of algorithm (you know when you need it), refer to the answer of Petr Pudlák!!!

  • 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-05-28T06:18:17+00:00Added an answer on May 28, 2026 at 6:18 am

    Immutable implementation

    A ring buffer is a pair of an IndexedSeq and an Int pointer into this sequence. I provide code for a immutable version. Note that not all methods that might be useful are implemented; like the mutators that change the content of the IndexedSeq.

    With this implementation, shifting is just creating one new object. So it’s pretty efficient.

    Example code

    class RingBuffer[A](val index: Int, val data: IndexedSeq[A]) extends IndexedSeq[A] {
      def shiftLeft = new RingBuffer((index + 1) % data.size, data)
      def shiftRight = new RingBuffer((index + data.size - 1) % data.size, data)
      def length = data.length
      def apply(i: Int) = data((index + i) % data.size)
    }
    
    val rb = new RingBuffer(0, IndexedSeq(2,3,5,7,11))
    
    println("plain: " + rb)
    println("sl: " + rb.shiftLeft)
    println("sr: " + rb.shiftRight)
    

    Output

    plain: Main(2, 3, 5, 7, 11)
    sl: Main(3, 5, 7, 11, 2)
    sr: Main(11, 2, 3, 5, 7)
    

    Performance comparison to mutable implementations

    The OP mentions that you should look at the mutable implementations (e.g. this answer), if you need performance. This is not true in general. As always: It depends.

    Immutable

    • update: O(log n), which is basically the update complexity of the underlying IndexedSeq;
    • shifting: O(1), also involves creating a new object which may cost some cycles

    Mutable

    • update: O(1), array update, as fast as it gets
    • shifting: O(n), you have to touch every element once; fast implementations on primitive arrays might still win against the immutable version for small arrays, because of constant factor
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to implement a one linked list but it should put object in
I have a school project in which I have to implement a chat application,
In Perl, we have IO::ScalarArray for treating the elements of an array like the
I have an array of an anonymous type declared as: var list = new[]
I am having a Search text-box for which I have to implement a search
I have to implement the VinPower application. They offer a Java version, a C
I have to implement MPI system in a cluster. If anyone here has any
I have to implement a middleware system for file sharing, and it has to
I have to implement a IExceptionHandler for the Enteprise Library 4.1. In my particular
I have found that I often have to implement some sort of a scheduler

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.