Thread: How to shift elements of an array?

  1. #16
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    But since you can do operations on both side of it like suggested, for example pushbase popbase and the ordinary push and pop. It would not follow the LIFO principle, at least it should be signified in some way as a special case of a stack, no?

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Stacks are FILO structs in my way of thinking - Queue's are FIFO, and I don't see a really striking resemblance to either one, here.

    One more vote for the for or while loop. Any elegance it may appear to lack compared to something like a circular buffer, is more than made up for by the supreme elegance of it's intuitive simplicity.
    Last edited by Adak; 06-07-2010 at 09:58 PM.

  3. #18
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Symantec arguments aside, how on earth is the case where there is a 4096 value container and two pointer re-assignments and two value assignments somehow less elegant than 4094 reads and 4096 writes? wow.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by jeffcobb View Post
    Symantec arguments aside, how on earth is the case where there is a 4096 value container and two pointer re-assignments and two value assignments somehow less elegant than 4094 reads and 4096 writes? wow.
    How about every other case where I actually want to do anything else with the data? Like for example, finding one specific element?
    Code:
    array[ X ] = 5;
    You know, every other normal operation you'd be doing...


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Subsonics View Post
    But since you can do operations on both side of it like suggested, for example pushbase popbase and the ordinary push and pop. It would not follow the LIFO principle, at least it should be signified in some way as a special case of a stack, no?
    You're describing a "Deque" (Double-ended queue).
    These can either be used as a stack or a queue, or both at once (useful for some algorithms I know of). Calling it "a stack" might be wrong, but only in so far as it's much more than that.
    Point taken anyway, ta.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #21
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Subsonics View Post
    But since you can do operations on both side of it like suggested, for example pushbase popbase and the ordinary push and pop. It would not follow the LIFO principle, at least it should be signified in some way as a special case of a stack, no?
    Quote Originally Posted by Adak View Post
    Stacks are FILO structs in my way of thinking - Queue's are FIFO, and I don't see a really striking resemblance to either one, here.
    Now, I know y'all ain't that dumb. Perhaps I should have said "stackish" and "que-esque". No one is going to write something and go, "Well, I can't add a pop from bottom because that disobeys the laws of FILO" or something.

    Quote Originally Posted by quzah View Post
    How about every other case where I actually want to do anything else with the data? Like for example, finding one specific element?
    Code:
    array[ X ] = 5;
    As mentioned several times already, X would be retrieved via a get/set() to do the dead simple arithmetic from the current first element. If you want the 5 element, that's first_element+5-1.

    Haven't you people done any actual programming? Or are you recommending one intentionally stick one's head in the sand whilst coding? Sheesh.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #22
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Hey, MK it's cool. When stack came up the first time in the thread I thought of it in the most basic classic form, which is why I objected to the use of it in this application. So any debate about it was just to clarify my point.

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by MK27 View Post
    As mentioned several times already, X would be retrieved via a get/set() to do the dead simple arithmetic from the current first element. If you want the 5 element, that's first_element+5-1.

    Haven't you people done any actual programming? Or are you recommending one intentionally stick one's head in the sand whilst coding? Sheesh.
    BFD. I don't care if you use get or set, it's still slower than me using an array. Which was the whole point of my post. You using a function call to access some member, no matter how you do it, isn't ever going to be faster than my array access. EVER.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well since the OP specified operating on the ends of the container and once you have more than a few items, doing two operations will always be faster than two + any extra operations to rotate the contents. Or as you put it ALWAYS. Heh. Since no operations on the middle of the container were specified, none were considered and any extra operations on the middle for the simple purpose of using a specific type are by definition wasted.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    array[ x ] = 5;

    Is always going to be faster than:

    put( 5, x );

    It doesn't matter if it's at the beginning, end, or middle. None of that actually matters though, because none of the implementations suggested, by anyone, actually do what they asked to do:

    Shift an array, and zero fill the items that were shifted away.

    Furthermore, the OP hasn't responded at all to this thread, so the speculation as to "only the ends are accessed" isn't accurate at all. They didn't say anything about only accessing end members.


    Quzah.
    Last edited by quzah; 06-08-2010 at 07:24 PM.
    Hope is the first step on the road to disappointment.

  11. #26
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Actually we all screwed the pooch on this one, including you Quzah. Before you hit the reply button in indignation, reread what the OP wrote:

    How would I then use the value to produce a second array that has the depth from 1 to 3cm and then have zeros in the elements after 3cm so that the new array is the same size as he original?
    IOW he wanted a second array, not a shifted version of the first one. Stacks and for loops and array access suddenly mean little which is probably why he/she never responded. We were giving him answers to a question he did not ask. Heh.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. how to make array elements shift one to the right?
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2002, 11:28 PM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM

Tags for this Thread