OSDN Git Service

4e881efac3610eaee867b09f13760866a7654fa2
[hmh/hhml.git] / lib / motorvar.cc
1 #include "motorvar.h"
2 #include "ustring.h"
3 #include <iostream>
4
5 using namespace  std;
6
7 void  MotorSet::set (const ustring& name) {
8     insert (name);
9 }
10
11 void  MotorSet::unset (const ustring& name) {
12     MotorSet::iterator  it = find (name);
13     if (it != end ())
14         erase (it);
15 }
16
17 bool  MotorSet::get (const ustring& name) {
18     MotorSet::iterator  it = find (name);
19     return (it != end ());
20 }
21
22 void  MotorVar::setVar (const ustring& name, MNode* val) {
23     std::pair<MotorVar::iterator, bool>  x;
24     erase (name);
25     x = insert (MotorVar::value_type (name, MNodePtr ()));
26     x.first->second = val;
27 }
28
29 MNode*  MotorVar::getVar (const ustring& name) {
30     MotorVar::iterator  it = find (name);
31     if (it == end ()) {
32         return NULL;
33     } else {
34         return it->second ();
35     }
36 }
37
38 void  MotorVar::eraseVar (const ustring& name) {
39 #ifdef HEAPDEBUG_VERBOSE
40     std::cerr << "MotorVar::eraseVar (" << name << ":(" << std::hex << getVar (name) << std::dec << ")";
41     getVar (name)->dump (std::cerr);    // XXX
42     std::cerr << ")\n";
43 #endif /* DEBUG */
44     erase (name);
45 }
46
47 bool  MotorVar::defined (const ustring& name) {
48     MotorVar::iterator  it = find (name);
49     return (it != end ());
50 }
51
52 void  MotorErrorVar::setVar (const ustring& name) {
53     erase (name);
54     insert (boost::unordered_map<ustring, bool>::value_type (name, true));
55 }
56
57 bool  MotorErrorVar::getVar (const ustring& name) {
58     MotorErrorVar::iterator  it = find (name);
59     if (it == end ()) {
60         return false;
61     } else {
62         return true;
63     }
64 }
65
66 void  MotorVector::push (MNode* val) {
67     MNodePtr*  p = new MNodePtr;
68     *p = val;
69     push_back (p);
70 }
71
72 MNode*  MotorVector::pop () {
73     if (size () > 0) {
74         MNode*  ans = back () ();
75         pop_back ();
76         return ans;
77     } else {
78         return NULL;
79     }
80 }
81
82 MNode*  MotorVector::get (size_type pos) {
83     // 0-base
84     if (pos < size ()) {
85         return at (pos) ();
86     } else {
87         return NULL;
88     }
89 }
90
91 void  MotorVector::put (size_type pos, MNode* val) {
92     if (pos < size ()) {
93     } else {
94         while (pos >= size ())
95             push (NULL);
96     }
97     at (pos) = val;
98 }
99
100 void  MotorVector::unshift (MNode* val) {
101     size_t  n = size ();
102     if (n > 0) {
103         push (get (n - 1));
104         for (size_t i = n - 1; i > 0; -- i) {
105             put (i, get (i - 1));
106         }
107         put (0, val);
108     } else {
109         push (val);
110     }
111 }
112
113 MNode*  MotorVector::shift () {
114     size_t  n = size ();
115     if (n > 0) {
116         MNode*  ans = get (0);
117         for (size_t i = 1; i < n; ++ i) {
118             put (i - 1, get (i));
119         }
120         pop ();
121         return ans;
122     } else {
123         return NULL;
124     }
125 }