OSDN Git Service

fix for FreeBSD 11.1.
[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         MNodePtr  ans;
75         ans = back () ();
76         pop_back ();
77         return ans.release ();
78     } else {
79         return NULL;
80     }
81 }
82
83 MNode*  MotorVector::get (size_type pos) {
84     // 0-base
85     if (pos < size ()) {
86         return at (pos) ();
87     } else {
88         return NULL;
89     }
90 }
91
92 void  MotorVector::put (size_type pos, MNode* val) {
93     if (pos < size ()) {
94     } else {
95         while (pos >= size ())
96             push (NULL);
97     }
98     at (pos) = val;
99 }
100
101 void  MotorVector::unshift (MNode* val) {
102     size_t  n = size ();
103     if (n > 0) {
104         push (get (n - 1));
105         for (size_t i = n - 1; i > 0; -- i) {
106             put (i, get (i - 1));
107         }
108         put (0, val);
109     } else {
110         push (val);
111     }
112 }
113
114 MNode*  MotorVector::shift () {
115     size_t  n = size ();
116     if (n > 0) {
117         MNodePtr  ans;
118         ans = get (0);
119         for (size_t i = 1; i < n; ++ i) {
120             put (i - 1, get (i));
121         }
122         pop ();
123         return ans.release ();
124     } else {
125         return NULL;
126     }
127 }