Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   data structures (https://forums.graalonline.com/forums/showthread.php?t=84745)

Mark Sir Link 03-19-2009 08:24 AM

data structures
 
boredom struck and I couldn't really find anything involving queues and stacks on the wiki (ActionMap has a push and pop which confused me) so I wrote up a queue real quick.

the most practical use I can think of for this right now is a spar system, where players joining the line are pushed on the queue, and you use a pop to get who's next in line.

I'll add a stack and some other data structures potentially tomorrow when I'm not tired.

queue:
PHP Code:

public function onCreated()
{
  
this.data = {null};
}

public function empty()
{
  if (
front() == null)
    return 
true;
  return 
false;
}

public function 
front()
{
  if(
this.data[0] == null)
    return 
null;
  else
    return 
this.data[0];
}

public function 
enqueueobj)
{
  
this.data.addobj);
}

public function 
dequeue()
{
  
this.data.remove(this.data[0]);
}

public function 
pushobj)
{
  
enqueue(obj);
}

public function 
pop()
{
  
temp.ret front();
  
dequeue();
  return 
ret;



napo_p2p 03-19-2009 08:40 AM

That's a fairly good exercise. I remember taking a data structures course a few years ago, and we had to implement these things from scratch (sort of similar to what you did). Any chance this is inspired by such a course? :p

Just some things I noticed:
You could simplify the empty() function:
PHP Code:

public function empty() { 
  return 
front() == null;


And, the front() function just needs to be:
PHP Code:

public function front() { 
    return 
this.data[0]; 


I have a feeling you may have ported that bit over from Java or something from the way you're dealing with the null case.

Mark Sir Link 03-19-2009 08:42 AM

Quote:

Originally Posted by napo_p2p (Post 1475853)
That's a fairly good exercise. I remember taking a data structures course a few years ago, and we had to implement these things from scratch (sort of similar to what you did). Any chance this is inspired by such a course? :p

Just some things I noticed:
You could simplify the empty() function:
PHP Code:

public function empty() { 
  return 
front() == null;


And, the front() function just needs to be:
PHP Code:

public function front() { 
    return 
this.data[0]; 


I have a feeling you may have ported that bit over from Java or something from the way you're dealing with the null case.


I've written mostly in java, haven't done data structures since sophomore year in high school when I took AP CS AB.

Was working on something earlier today, thought a queue would be useful, couldn't find a graal version anywhere.

Chompy 03-19-2009 04:12 PM

Reminds me of associative arrays ;o

http://forums.graalonline.com/forums...ad.php?t=68643

napo_p2p 03-19-2009 05:28 PM

Quote:

Originally Posted by Mark Sir Link (Post 1475854)
I've written mostly in java, haven't done data structures since sophomore year in high school when I took AP CS AB.

Ahh, same class :p.

Inverness 03-20-2009 05:05 AM

Stefan should simply add pushback(), pushfront(), popback(), and popfront() functions to the TGraalVar which would allow the array to be used as a queue or stack.


All times are GMT +2. The time now is 07:07 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.