Use std::stack.

This commit is contained in:
Naoki INADA 2009-06-22 15:59:02 +09:00
parent 9a77ab57f6
commit 87f5df1503

View File

@ -18,17 +18,21 @@
#include <map> #include <map>
#include <string> #include <string>
#include <stack>
#define MSGPACK_MAX_STACK_SIZE (1024) #define MSGPACK_MAX_STACK_SIZE (1024)
#include "msgpack/unpack_define.h" #include "msgpack/unpack_define.h"
using namespace std; using namespace std;
typedef struct unpack_user { struct array_context {
struct array_stack_type{unsigned int size, last;}; unsigned int size;
array_stack_type array_stack[MSGPACK_MAX_STACK_SIZE]; unsigned int last;
int array_current; stack_item(unsigned int size) : size(size), last(0)
{}
};
struct unpack_user {
stack<array_context> array_stack;
map<string, PyObject*> str_cache; map<string, PyObject*> str_cache;
~unpack_user() { ~unpack_user() {
@ -38,7 +42,7 @@ typedef struct unpack_user {
Py_DECREF(it->second); Py_DECREF(it->second);
} }
} }
} unpack_user; };
#define msgpack_unpack_struct(name) \ #define msgpack_unpack_struct(name) \
@ -60,7 +64,6 @@ typedef struct template_context template_context;
static inline msgpack_unpack_object template_callback_root(unpack_user* u) static inline msgpack_unpack_object template_callback_root(unpack_user* u)
{ {
u->array_current = -1;
return NULL; return NULL;
} }
@ -113,9 +116,7 @@ static inline int template_callback_false(unpack_user* u, msgpack_unpack_object*
static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_unpack_object* o) static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
{ {
if (n > 0) { if (n > 0) {
int cur = ++u->array_current; u->array_stack.push(stack_item(n));
u->array_stack[cur].size = n;
u->array_stack[cur].last = 0;
*o = PyList_New(n); *o = PyList_New(n);
} }
else { else {
@ -126,17 +127,13 @@ static inline int template_callback_array(unpack_user* u, unsigned int n, msgpac
static inline int template_callback_array_item(unpack_user* u, msgpack_unpack_object* c, msgpack_unpack_object o) static inline int template_callback_array_item(unpack_user* u, msgpack_unpack_object* c, msgpack_unpack_object o)
{ {
int cur = u->array_current; unsigned int n = u->array_stack.top().size;
int n = u->array_stack[cur].size; unsigned int &last = u->array_stack.top().last;
int last = u->array_stack[cur].last;
PyList_SetItem(*c, last, o); PyList_SetItem(*c, last, o);
last++; last++;
if (last >= n) { if (last >= n) {
u->array_current--; u->array_stack.pop();
}
else {
u->array_stack[cur].last = last;
} }
return 0; return 0;
} }