Jump to content


Photo

[SOLVED] Last pushed stack value?


  • Please log in to reply
4 replies to this topic

#1 vinicius

vinicius

    GMC Member

  • GMC Member
  • 189 posts
  • Version:GM:Studio

Posted 22 September 2012 - 02:14 PM

With stack functions we can deal with stacks. We have ds_stack_top() to get the first stack value. So what I want is how to get the last value pushed onto the stack? Do we have a ds_stack_bottom() function ?
Thanks!

Edited by vinicius, 22 September 2012 - 10:26 PM.

  • 0

#2 Tennx

Tennx

    The Entrepreneur

  • GMC Member
  • 259 posts
  • Version:GM:Studio

Posted 22 September 2012 - 03:29 PM

Why do you use stacks? Stacks are meant to be FILO (First In Last Out), so there must not exist a function that reveals the first element until it is the last on stack.
You should use ds_list instead so you can make reference to the position directly, and the code runs faster.

Well, if you want to get the bottom element, you can copy your stack with

clone = ds_stack_create(); //Create an auxiliar stack
ds_stack_copy(clone, your_original_stack); //Copy your source data in this auxiliar stack

And then...

while(ds_stack_size(clone) > 1){
	ds_stack_pop(clone); //Bring out unnecessary elements until there is only one and...
}

Finally...

bottom_value = ds_stack_top(clone); //Last remaining element in the clone stack is the bottom value.
ds_stack_destroy(clone); //Get rid of the clone, you won't need it anymore.


In resume...

ds_stack_bottom(id):
var source;
source = argument0;
clone = ds_stack_create(); //Create an auxiliar stack
ds_stack_copy(clone, source); //Copy your source data in this auxiliar stack

while(ds_stack_size(clone) > 1){
	ds_stack_pop(clone); //Bring out unnecessary elements until there is only one and...
}

bottom_value = ds_stack_top(clone); //Last remaining element in the clone stack is the bottom value.

ds_stack_destroy(clone); //Get rid of the clone, you won't need it anymore.
return bottom_value;

Use that script, tell me if it works ;).

---

Oh, and by the way... next time you need some advice about functions, GML or code, go in here: http://gmc.yoyogames...php?showforum=2

Edited by Tennx, 22 September 2012 - 03:36 PM.

  • 0

#3 vinicius

vinicius

    GMC Member

  • GMC Member
  • 189 posts
  • Version:GM:Studio

Posted 22 September 2012 - 09:28 PM

Why do you use stacks? Stacks are meant to be FILO (First In Last Out), so there must not exist a function that reveals the first element until it is the last on stack.
You should use ds_list instead so you can make reference to the position directly, and the code runs faster.

Well, if you want to get the bottom element, you can copy your stack with

clone = ds_stack_create(); //Create an auxiliar stack
ds_stack_copy(clone, your_original_stack); //Copy your source data in this auxiliar stack

And then...

while(ds_stack_size(clone) > 1){
	ds_stack_pop(clone); //Bring out unnecessary elements until there is only one and...
}

Finally...

bottom_value = ds_stack_top(clone); //Last remaining element in the clone stack is the bottom value.
ds_stack_destroy(clone); //Get rid of the clone, you won't need it anymore.


In resume...

ds_stack_bottom(id):
var source;
source = argument0;
clone = ds_stack_create(); //Create an auxiliar stack
ds_stack_copy(clone, source); //Copy your source data in this auxiliar stack

while(ds_stack_size(clone) > 1){
	ds_stack_pop(clone); //Bring out unnecessary elements until there is only one and...
}

bottom_value = ds_stack_top(clone); //Last remaining element in the clone stack is the bottom value.

ds_stack_destroy(clone); //Get rid of the clone, you won't need it anymore.
return bottom_value;

Use that script, tell me if it works ;).

---

Oh, and by the way... next time you need some advice about functions, GML or code, go in here: http://gmc.yoyogames...php?showforum=2

Not worked, this script do the same as ds_stack_top(). .... And I know Stacks are FILO, so I´m using them. What I´m asking is why we do not have a function to see the last stack push? I know that I can use a variable like last_entry or something like that but why we do not have a function to deal with it? Or we have and I was not be able to find?
  • 0

#4 GameGeisha

GameGeisha

    GameGeisha

  • GMC Member
  • 2743 posts
  • Version:GM:Studio

Posted 22 September 2012 - 09:48 PM

We have ds_stack_top() to get the first stack value. So what I want is how to get the last value pushed onto the stack?

You're mixing up stacks with queues. The last value pushed onto the stack is the value returned by ds_stack_top(). If you need access to both ends, you have to use a list as Tennx suggested.

GameGeisha
  • 0

#5 vinicius

vinicius

    GMC Member

  • GMC Member
  • 189 posts
  • Version:GM:Studio

Posted 22 September 2012 - 10:25 PM


We have ds_stack_top() to get the first stack value. So what I want is how to get the last value pushed onto the stack?

You're mixing up stacks with queues. The last value pushed onto the stack is the value returned by ds_stack_top(). If you need access to both ends, you have to use a list as Tennx suggested.

GameGeisha

Hmmm, I misunderstood when reading the manual. So, thanks you guys on help me.
Flws!
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users