Jakash3’s C++ Stack Class

This is a custom c++ stack class I made.
Here’s the link: http://abacus.subluminal.net/~jakashthree/jstack/jstack.tar.gz
The purpose is to introduce more functions and functionality as opposed to the original STL stack class. I also aim to make this compatible with function passing by making a publicly accessible pointer that references the malloc allocated internal data of the stack. For i/o operations with arrays with no known length, I included a buflen member int that controls how many elements of the template type is to be input or output with this stack. It also supports stream operators for pushing and popping. Here’s the commented header:

#ifndef _JSTACK_H
#define    _JSTACK_H
#include <stdlib.h>
#include "jgc.h"
#include <string.h>

template <class type>
class jstack {
public:

    jstack() { data = (type*)malloc(sizeof(type)); len = 0; buflen = 1;}
    ~jstack() { clean();}

    /* Pointer to internal stack data */
    type* data;
    /* Stack length */
    int len;
    /* Length of buffer for i/o operations with this stack*/
    int buflen;

    /* Pops off value and stores it in t */
    jstack& operator>>(type& t);
    /* Pops block with element length of buflen and stores it in stack*/
    jstack& operator>>(jstack<type>& stack);
    jstack& operator>>(type* stack);
    /* Pushes t*/
    jstack& operator<<(const type t);
    /* Push stack data elements equal to buflen */
    jstack& operator<<(jstack<type> stack);
    /* Push number of items equal to buflen */
    jstack& operator<<(const type* items);
    /* Assign (copy) contents of stack to this stack */
    type* operator=(jstack<type> stack);
    /* Assign (copy) buflen number of element from stack to this stack */
    type* operator=(const type* stack);
    /* Comparisons. Content and len must be equal. 1 if equal, 0 if not equal */
    int operator==(const jstack<type> stack);
    int operator==(const type* stack);
    /* Same as above but with comparison result reversed */
    int operator!=(const jstack<type> stack);
    int operator!=(const type* stack);
    /* Push without assignement, returns reference to temporary stack */
    type* operator+(const type item);
    type* operator+(const type* items);
    type* operator+(jstack<type>& stack);
    /* Push with assignment */
    type* operator+=(const type item);
    type* operator+=(const jstack<type> stack);
    /* Pop without assignment. returns reference to temporary stack */
    type* operator-(int count);
    /* Pop */
    type* operator-=(int count);

    type top();
    /* Returns temp reference of block on top of stack. */
    type* top(int count);
    /* Returns block on top of stack into dest. */
    type* top(type* dest, int count);
    type* push(const type item);
    type* push(jstack<type> stack);
    type* push(const type* stack);
    type pop();
    /* Pops block off stack, returns termporary reference to popped off block. */
    type* pop(int count);
    /* Pops block equal to buflen off stack into dest.*/
    type* pop(type* dest);
    void clear();
    /* Checks if stack contains item */
    int has(const type item);
    /* Get index of first occurance of item from offset of top of stack */
    int firstof(const type item);
    /* Get index of last occurance of item from offset of top of stack */
    int lastof(const type item);
    /* Reverses the stack */
    type* rev();
    type* copy(type* dst);
    

};

#endif    /* _JSTACK_H */

Not that you’re going to read all of that ^
With this template class, you have to include jstack.cpp instead of jstack.h for magical reasons that I don’t have to explain because it’s magic.
Now here are some examples, assuming this is the starting code for each one:

#include "jstack.cpp"
#include <iostream>
using namespace std;
int main() {
jstack<char> str;
str << 'h' << 'e' << 'l' << 'l' << 'o' << '';
cout << str.data;
}
int main() {
char str1[] = "hello world";
jstack<char> str2;
str2.buflen = 12
str2 << str1;
cout << str2.data;
str2.pop(4);
str2 << '';
cout << str2.data;
}
int main() {
jstack<str> s;
s.buflen = 5;
s.push("hello");
char c;
s >> c;
cout << c;
}
int main() {
jstack<char> s;
s.buflen = 6;
s << "hello";
char* p = s.pop(3);
cout << p << endl << s.data;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: