rpn/src/stack.hpp

99 lines
2.4 KiB
C++
Raw Normal View History

#ifndef __stack_h__
#define __stack_h__
2014-02-12 11:26:26 +01:00
#include <string.h>
2022-02-07 16:36:27 +01:00
#include <algorithm>
#include <deque>
#include <map>
2022-02-07 16:36:27 +01:00
#include "object.hpp"
using namespace std;
2018-08-13 00:15:20 +02:00
/// @brief stack object, parens of program, storing execution stack values or programs
///
2022-02-07 16:36:27 +01:00
class rpnstack : public deque<object*> {
2018-08-12 15:06:04 +02:00
public:
2022-02-07 16:36:27 +01:00
rpnstack() {}
virtual ~rpnstack() {}
2017-05-02 10:47:03 +02:00
2018-08-13 00:15:20 +02:00
/// @brief copy a whole stack entry and push it back to another stack
///
/// @param from copy from
/// @param index_from index t ocopy from
/// @param to copy to
///
2022-02-07 16:36:27 +01:00
static void copy_and_push_back(rpnstack& from, unsigned int index_from, rpnstack& to) {
to.push_back(from[index_from]);
2018-08-12 15:06:04 +02:00
}
2022-02-07 16:36:27 +01:00
static void copy_and_push_front(rpnstack& from, unsigned int index_from, rpnstack& to) {
to.push_front(from[index_from]);
2018-08-12 15:06:04 +02:00
}
2022-02-07 16:36:27 +01:00
/// @brief pop back several entries
2018-08-13 00:15:20 +02:00
///
2022-02-07 16:36:27 +01:00
/// @param levels nb of entries
2018-08-13 00:15:20 +02:00
///
2022-02-07 16:36:27 +01:00
void pop_back(int levels) {
for (int i = 0; i < levels; i++) deque::pop_back();
}
2015-05-19 17:51:03 +02:00
2022-02-07 16:36:27 +01:00
/// @brief pop back 1 entry
2018-08-13 00:15:20 +02:00
///
2022-02-07 16:36:27 +01:00
/// @return retrieved object
2018-08-13 00:15:20 +02:00
///
2022-02-07 16:36:27 +01:00
object* pop_back() {
object* o = back();
pop_back(1);
return o;
2018-08-12 15:06:04 +02:00
}
2018-08-12 11:50:49 +02:00
};
2018-08-13 00:15:20 +02:00
/// @brief heap object, storing variables (=named object)
///
2022-02-07 16:36:27 +01:00
class heap : public map<string, object*> {
2018-08-12 15:06:04 +02:00
public:
heap() {}
virtual ~heap() {}
2018-08-13 00:15:20 +02:00
/// @brief get a variable
2022-02-07 16:36:27 +01:00
///
2018-08-13 00:15:20 +02:00
/// @param name the variable name
/// @param obj the variable content
/// @return true the variable was found
/// @return false the variable was not found
///
2022-02-07 16:36:27 +01:00
bool get(const string name, object*& obj) {
2018-08-12 15:06:04 +02:00
bool ret = false;
2022-02-07 16:36:27 +01:00
auto i = find(name);
if (i != end()) {
obj = i->second;
2018-08-12 15:06:04 +02:00
ret = true;
}
return ret;
2015-05-19 17:51:03 +02:00
}
2018-08-12 15:06:04 +02:00
2018-08-13 00:15:20 +02:00
/// @brief get a variable by its index in heap
2022-02-07 16:36:27 +01:00
///
2018-08-13 00:15:20 +02:00
/// @param num the variable index
/// @param name the variable name
/// @param obj the variable content
/// @return true the variable was found
/// @return false the variable was not found
///
2022-02-07 16:36:27 +01:00
bool get_by_index(int num, string& name, object*& obj) {
if (num >= 0 && num < (int)size()) {
2018-08-12 15:06:04 +02:00
object* local;
2022-02-07 16:36:27 +01:00
auto i = begin();
for (; num > 0; num--, i++)
;
obj = i->second;
2018-08-12 15:06:04 +02:00
return true;
} else
return false;
}
};
2018-08-12 11:50:49 +02:00
#endif // __stack_h__