1 | #include "node.h" |
---|
2 | |
---|
3 | // |
---|
4 | // assign value to coordinate[index] |
---|
5 | // |
---|
6 | RP_ERROR |
---|
7 | RpNode::set(int val, int index) |
---|
8 | { |
---|
9 | RP_ERROR err = RP_SUCCESS; |
---|
10 | |
---|
11 | if (index < m_dim) |
---|
12 | m_node[index] = val; |
---|
13 | else { |
---|
14 | RpAppendErr("RpNode::set: index out of bound"); |
---|
15 | RpPrintErr(); |
---|
16 | err = RP_ERR_OUTOFBOUND_INDEX; |
---|
17 | } |
---|
18 | |
---|
19 | return err; |
---|
20 | } |
---|
21 | |
---|
22 | // |
---|
23 | // assign node values |
---|
24 | // |
---|
25 | RP_ERROR |
---|
26 | RpNode::set(int dim, int* val, int id) |
---|
27 | { |
---|
28 | if (val == NULL) |
---|
29 | return RP_ERR_NULL_PTR; |
---|
30 | |
---|
31 | m_dim = dim; |
---|
32 | m_id = id; |
---|
33 | m_node.resize(m_dim); |
---|
34 | |
---|
35 | for (int i=0; i<m_dim; i++) { |
---|
36 | m_node[i] = val[i]; |
---|
37 | } |
---|
38 | |
---|
39 | return RP_SUCCESS; |
---|
40 | } |
---|
41 | |
---|
42 | // |
---|
43 | // get node values |
---|
44 | // Input: |
---|
45 | // node: array of int for storing coordinates |
---|
46 | // len: length of 'node' array |
---|
47 | // |
---|
48 | RP_ERROR |
---|
49 | RpNode::get(int* node, int len) |
---|
50 | { |
---|
51 | RP_ERROR err = RP_SUCCESS; |
---|
52 | |
---|
53 | if (node != NULL && len >= m_dim) |
---|
54 | for (int i=0; i<m_dim; i++) { |
---|
55 | node[i] = m_node[i]; |
---|
56 | } |
---|
57 | else |
---|
58 | err = RP_ERR_INVALID_ARRAY; |
---|
59 | |
---|
60 | return err; |
---|
61 | } |
---|
62 | |
---|
63 | RP_ERROR |
---|
64 | RpNode::get(int& val, int index) |
---|
65 | { |
---|
66 | RP_ERROR err = RP_SUCCESS; |
---|
67 | |
---|
68 | if (index < m_dim) |
---|
69 | val = m_node[index]; |
---|
70 | else { |
---|
71 | RpAppendErr("RpNode::set: index out of bound"); |
---|
72 | RpPrintErr(); |
---|
73 | err = RP_ERR_OUTOFBOUND_INDEX; |
---|
74 | } |
---|
75 | |
---|
76 | return err; |
---|
77 | } |
---|
78 | |
---|
79 | char* |
---|
80 | RpNode::serialize() |
---|
81 | { |
---|
82 | int nbytes = (m_node.size()) * sizeof(int); |
---|
83 | |
---|
84 | // total length of streams buffer |
---|
85 | char * buf = new char[nbytes]; |
---|
86 | if (buf == NULL) { |
---|
87 | RpAppendErr("RpNode::serialize: mem alloc failed"); |
---|
88 | RpPrintErr(); |
---|
89 | return buf; |
---|
90 | } |
---|
91 | |
---|
92 | serialize(buf, nbytes); |
---|
93 | |
---|
94 | return buf; |
---|
95 | } |
---|
96 | |
---|
97 | // |
---|
98 | // serialize with an input streams buffer |
---|
99 | // 4 bytes: x |
---|
100 | // 4 bytes: y |
---|
101 | // 4 bytes: z (if 3d node) |
---|
102 | // |
---|
103 | void |
---|
104 | RpNode::serialize(char* buf, int buflen) |
---|
105 | { |
---|
106 | char* ptr = buf; |
---|
107 | |
---|
108 | // do nothing |
---|
109 | if (buf == NULL || buflen < m_dim* ((signed int)sizeof(int))) { |
---|
110 | RpAppendErr("RpNode::serialize: buffer not valid"); |
---|
111 | RpPrintErr(); |
---|
112 | return; |
---|
113 | } |
---|
114 | |
---|
115 | // write to stream buffer |
---|
116 | for (int i=0; i<m_dim; i++) { |
---|
117 | memcpy((void*)ptr, (void*)&(m_node[i]), sizeof(int)); |
---|
118 | ptr += sizeof(int); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | void |
---|
123 | RpNode::deserialize(const char* buf) |
---|
124 | { |
---|
125 | if (buf == NULL) { |
---|
126 | RpAppendErr("RpNode::serialize: null buffer"); |
---|
127 | RpPrintErr(); |
---|
128 | return; |
---|
129 | } |
---|
130 | |
---|
131 | // write to stream buffer |
---|
132 | int i = 0; |
---|
133 | while (i < m_dim && buf) { |
---|
134 | memcpy((void*)&(m_node[i]), (void*)buf, sizeof(int)); |
---|
135 | buf += sizeof(int); |
---|
136 | i++; |
---|
137 | } |
---|
138 | |
---|
139 | return; |
---|
140 | } |
---|
141 | |
---|
142 | void RpNode::print() |
---|
143 | { |
---|
144 | for (int i=0; i<m_dim; i++) |
---|
145 | printf("%d ", m_node[i]); |
---|
146 | printf("\n"); |
---|
147 | } |
---|