Raised This Month: $12 Target: $400
 3% 

[INC] Queue - FIFO


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
JoinedSenses
Senior Member
Join Date: Sep 2013
Old 11-03-2019 , 23:04   [INC] Queue - FIFO
Reply With Quote #1

Based on some discussion via SM discord, I created a queue methodmap which inherits from Handle and utilizes arraylist methods. Unlike ArrayStack, this methodmap is First In, First Out.

Code can be found here:
queue.inc

Simple example usage:

Code:
#include <sourcemod>
#include <queue>

public void OnPluginStart() {
	RegAdminCmd("sm_test", cmdTest, ADMFLAG_ROOT);
}

public Action cmdTest(int client, int args) {
	Queue q = new Queue();

	for (int i = 1; i <= 10; i++) {
		q.Push(i);	
	}

	while (!q.Empty) {
		ReplyToCommand(client, "%i", q.Pop());
	}

	delete q;

	return Plugin_Handled;
}
__________________

Last edited by JoinedSenses; 11-03-2019 at 23:58.
JoinedSenses is offline
Bara
AlliedModders Donor
Join Date: Apr 2012
Location: Germany
Old 11-04-2019 , 13:13   Re: [INC] Queue - FIFO
Reply With Quote #2

Thanks, will be useful for some stuff.
__________________
Discord (Bara#5006) | My Plugins (GitHub)
You like my work? Support is not a crime.
Bara is offline
HelpMe
Senior Member
Join Date: Jun 2013
Location: Home
Old 10-29-2020 , 07:36   Re: [INC] Queue - FIFO
Reply With Quote #3

Sadly pop is still O(n).
HelpMe is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 10-29-2020 , 17:50   Re: [INC] Queue - FIFO
Reply With Quote #4

Quote:
Originally Posted by HelpMe View Post
Sadly pop is still O(n).
Queue is based on ArrayList so it's not surprising.
__________________
MAGNAT2645 is offline
JoinedSenses
Senior Member
Join Date: Sep 2013
Old 01-23-2021 , 15:34   Re: [INC] Queue - FIFO
Reply With Quote #5

SM doesnt have LinkedList
__________________
JoinedSenses is offline
wopox3
Junior Member
Join Date: Dec 2013
Location: Russia
Old 01-27-2021 , 15:34   Re: [INC] Queue - FIFO
Reply With Quote #6

accept please https://github.com/JoinedSenses/SM-Queue/pull/2
wopox3 is offline
kossolax
AlliedModders Donor
Join Date: Jan 2008
Location: Belgium
Old 03-31-2021 , 07:24   Re: [INC] Queue - FIFO
Reply With Quote #7

Quote:
Originally Posted by JoinedSenses View Post
SM doesnt have LinkedList
This is not tested, but would be more smart for O(1) push & pop at the cost of some more memory usage (using circular buffer)


PHP Code:
methodmap Queue Handle {
    public 
Queue(int size=32) {
        
ArrayList data = new ArrayList(1size+3);
        
data.Set(00);        // tail
        
data.Set(10);        // head
        
data.Set(2size);    // Size
        
return view_as<Queue>(data);
    }
    
    
property int Tail {
        public 
get() { return view_as<ArrayList>(this).Get(0); }
        public 
set(const int value) { view_as<ArrayList>(this).Set(0value); }
    }
    
property int Head {
        public 
get() { return view_as<ArrayList>(this).Get(1); }
        public 
set(const int value) { view_as<ArrayList>(this).Set(1value); }
    }
    
property int Size {
        public 
get() { return view_as<ArrayList>(this).Get(2); }
    }
    
// ----
    
property bool isEmpty {
        public 
get() { return this.Head == this.Tail; }
    }
    
property bool isFull {
        public 
get() { return ((this.Head+1)%this.Size) == this.Tail; }
    }
    
// ----
    
public void Push(const int value) {
        
view_as<ArrayList>(this).Set(this.Head+3value);
        
this.Head = (this.Head 1) % this.Size;
    }
    public 
int Pop() {
        
int tmp view_as<ArrayList>(this).Get(this.Tail+3);
        
this.Tail = (this.Tail 1) % this.Size;
        return 
tmp;
    }
    public 
int Front() {
        return 
view_as<ArrayList>(this).Get(this.Tail+3);
    }


Last edited by kossolax; 03-31-2021 at 07:48.
kossolax is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:40.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode