Roll Implementation
4 posters
Page 1 of 1
Roll Implementation
I am a little confused about how the roll function is executed.
According to the spec:
So if I am performing a single roll to depth 3 on a stack
1 2 3 4 5 6 7 8 9 10 (1 is top of stack), is the result
2 3 1 4 5 6 7 8 9 10, or is it
2 3 4 1 5 6 7 8 9 10. Is the depth of 3 evaluated before or after the top value is removed.
For a depth of -3, I can see possible results of
3 1 2 4 5 6 7 8 9 10
or
4 1 2 3 5 6 7 8 9 10
Which cases are right? I hope this isn't too confusing.
According to the spec:
roll: Pops the top two values off the stack and "rolls" the remaining stack entries to a depth equal to the second value popped, by a number of rolls equal to the first value popped. A single roll to depth n is defined as burying the top value on the stack n deep and bringing all values above it up by 1 place. A negative number of rolls rolls in the opposite direction. A negative depth is an error and the command is ignored.
So if I am performing a single roll to depth 3 on a stack
1 2 3 4 5 6 7 8 9 10 (1 is top of stack), is the result
2 3 1 4 5 6 7 8 9 10, or is it
2 3 4 1 5 6 7 8 9 10. Is the depth of 3 evaluated before or after the top value is removed.
For a depth of -3, I can see possible results of
3 1 2 4 5 6 7 8 9 10
or
4 1 2 3 5 6 7 8 9 10
Which cases are right? I hope this isn't too confusing.
captncraig- Posts : 5
Join date : 2009-09-18
Re: Roll Implementation
quite frankly, I have no idea. ^_^; I'll get back to you if I figure it out, my self.
Doing Online High School Diploma
Stanley High School has years of online learning experience and takes pride in providing quality education to all diploma holders of Stanley High School from all across the globe. Stanley High School provides the perfect medium to earn an accredited high school diploma for students who were not able to continue with their education, home-school students or working adults.
Why choose Stanley High School?
1. Earn a High School Diploma: Customize your educational plans and earn a diploma using 21st century technology.
2. Schedule with Flexibility: Complete courses at your own pace and at times that best fit their daily schedules.
3. Recover Credit: Earn credits to get back on track for graduation within your own school district. Enrollment is not dependent upon traditional school calendars, so courses can be started at any time during the year.
4. Apply Home School Credits: You can enroll in SHS and transfer credits earned through home school study to our diploma program.
5. Prior to credits being awarded, courses must be submitted for review and approval.
6. Select from Advanced or Remedial Courses: Choose courses that meet your academic needs and support your learning style.
High School Diploma Online.
Why choose Stanley High School?
1. Earn a High School Diploma: Customize your educational plans and earn a diploma using 21st century technology.
2. Schedule with Flexibility: Complete courses at your own pace and at times that best fit their daily schedules.
3. Recover Credit: Earn credits to get back on track for graduation within your own school district. Enrollment is not dependent upon traditional school calendars, so courses can be started at any time during the year.
4. Apply Home School Credits: You can enroll in SHS and transfer credits earned through home school study to our diploma program.
5. Prior to credits being awarded, courses must be submitted for review and approval.
6. Select from Advanced or Remedial Courses: Choose courses that meet your academic needs and support your learning style.
High School Diploma Online.
Re: Roll Implementation
captncraig wrote:I am a little confused about how the roll function is executed.
So if I am performing a single roll to depth 3 on a stack
1 2 3 4 5 6 7 8 9 10 (1 is top of stack), is the result
2 3 1 4 5 6 7 8 9 10, or is it
2 3 4 1 5 6 7 8 9 10. Is the depth of 3 evaluated before or after the top value is removed.
For a depth of -3, I can see possible results of
3 1 2 4 5 6 7 8 9 10
or
4 1 2 3 5 6 7 8 9 10
Which cases are right? I hope this isn't too confusing.
I’ll give you a very detailed answer that’s hopefully helpful:
Let’s assume your stack is:
[1 2 3 4 5 6 7]
You want to roll n1 times from a depth of n2, in your case n1=1, n2=3
So you put the elements [1 3] on top of the "data" stack. The top of the stack is always shown on the left side.
- Code:
execute: []...........data: [ 1 3 1 2 3 4 5 6 7 ]
......................temp: [---------------------------]
pointer:....................[---------------------------]
The interpreter reads in the instruction, which is the roll command, and
knows it needs the next two elements from the "data" stack to execute the roll operation:
- Code:
execute: rol[]........data: [ 1 3 1 2 3 4 5 6 7 ]
......................temp: [---------------------------]
pointer:....................[---------------------------]
rol pops off the first element of the "data" stack, the number of rolls:
- Code:
execute: rol[ 1 ].....data: [ 3 1 2 3 4 5 6 7 ]
......................temp: [------------------------]
pointer:....................[------------------------]
Then it pops off the next element off the "data" stack, the depth of the roll:
- Code:
execute: rol[ 3 1 ]..data: [ 1 2 3 4 5 6 7 ]
......................temp: [---------------------]
pointer:....................[---------------------]
Now we have the depth on top of the "execute" stack, which points to the
roll depth, visualized by the "pointer" stack.
The first element that got popped off points to the depth.
- Code:
execute: rol[ 3 1 ]..data: [ 1 2 3 4 5 6 7 ]
.....................temp: [---------------------]
pointer:....................[-------^-------------]
The pointer element gets popped off the "execute" stack.
- Code:
execute: rol[ 1 ].....data: [ 1 2 3 4 5 6 7 ]
......................temp: [---------------------]
pointer:....................[-------^-------------]
The second element that got popped off from the "data" stack tells
how many rolls (towards the top of the stack) are to be executed.
Each roll the topmost element of the "data" stack gets popped off, then the elements from the pointer location to the top move up one position and the topmost element (that just got popped off) gets inserted into the temp stack at the pointer location.
Then the element in the "execute" stack is decreased by 1.
The "temp" stack is just added for easier visualization of the operation:
- Code:
execute: rol[ 0 ].....data: [ 2 3 . 4 5 6 7 ]
......................temp: [-------1-------------]
pointer:....................[-------^-------------]
The "temp" element gets pushed into the "data" stack and the roll operation is repeated until the element in the "execute" stack is zero:
- Code:
execute: rol[ 0 ]....data: [ 2 3 1 4 5 6 7 ]
.....................temp: [---------------------]
pointer:...................[-------^-------------]
If the element in the "execute" stack is zero, the "execute" stack and the pointer get deleted/emptied:
- Code:
execute: []..........data: [ 2 3 1 4 5 6 7 ]
.....................temp: [---------------------]
pointer:...................[---------------------]
If your roll depth is negative (in your example -3) the command is ignored.
But, if the number of rolls is negative (like -3 in your example) the command is
executed like this:
I’ll only give the short form now. A negative number rolls in the opposite direction.
- Code:
execute: rol [3 -3]..[1 2 3 4 5 6 7]
..........................^
execute: rol [-2]....[3 1 2 4 5 6 7]
..........................^
execute: rol [-1]....[2 3 1 4 5 6 7]
..........................^
execute: rol [ 0]....[1 2 3 4 5 6 7]
..........................^
So, in this example the stack would look the same after the roll operation.
Now let’s roll the stack -3 times from depth 4:
- Code:
negative roll direction
--->>>
[1 2 3 4 5 6 7]
-------^-------
[4 1 2 3 5 6 7]
-------^-------
[3 4 1 2 5 6 7]
-------^-------
[2 3 4 1 5 6 7]
-------^-------
The same, but in positive direction:
- Code:
positive roll direction
<<<---
[1 2 3 4 5 6 7]
-------^-------
[2 3 4 1 5 6 7]
-------^-------
[3 4 1 2 5 6 7]
-------^-------
[4 1 2 3 5 6 7]
-------^-------
If a command needs values from the stack, then these values get popped off before anything else is done. Otherwise the command couldn’t get executed. Just imagine that for commands like ROL there always need to be values put into the execute stack to enable the command to proceed according to these values.
After the execute stack is filled the command can proceed doing what its supposed to do.
I hope this description is detailed enough.
Edit: Finally the formatting is correct. Somehow spaces are not treated properly, so I had to fill everything up with periods and dashes. I hope it’s still legible
Last edited by DIN99 on Sun Apr 19, 2015 7:25 am; edited 2 times in total (Reason for editing : Formatting)
DIN99- Posts : 4
Join date : 2015-04-17
Location : Germany
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|