arzmq-sys 0.6.3

Low-level bindings to the zeromq library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/**
* @file protoSpace.cpp
* 
* @brief This maintains a set of "Nodes" in n-dimensional Euclidean space.
*/
// Uncomment this to have SDT display bounding box iteration
//#define USE_SDT

#include "protoDebug.h"

#include "protoSpace.h"
#include <math.h>  // for "fabs()" 
#include <stdio.h> // for "printf()"
ProtoSpace::Node::Node()
{
}

ProtoSpace::Node::~Node()
{
}

ProtoSpace::Ordinate::Ordinate()
{
    memset(key, 0, sizeof(double)+sizeof(Node*));
}

ProtoSpace::Ordinate::~Ordinate()
{
}

ProtoSpace::ProtoSpace()
 : num_dimensions(0), ord_tree(NULL), node_count(0)
{
}

ProtoSpace::~ProtoSpace()
{
    if (NULL != ord_tree)
    {
        for (unsigned int i = 0; i < num_dimensions; i++)
            ord_tree[i].EmptyToPool(ord_pool);
        delete[] ord_tree;
        ord_tree = NULL;
    }
    ord_pool.Destroy();
}

void ProtoSpace::Empty()
{
    if (NULL != ord_tree)
    {
        for (unsigned int i = 0; i < num_dimensions; i++)
            ord_tree[i].EmptyToPool(ord_pool);
    }
}  // end ProtoSpace::Empty()

void ProtoSpace::Destroy()
{
    Iterator iterator(*this);
    if (!iterator.Init())
    {
        PLOG(PL_ERROR, "ProtoSpace::Destroy() ProtoSpace::Iterator.Init() error: %s\n", GetErrorString());
        return;
    }
    Node* node;
    while (NULL != (node = iterator.GetNextNode()))
    {
        RemoveNode(*node);
        delete node;
    }
    if (NULL != ord_tree)
    {
        for (unsigned int i = 0; i < num_dimensions; i++)
            ord_tree[i].EmptyToPool(ord_pool);
        delete[] ord_tree;
        ord_tree = NULL;
    }
    ord_pool.Destroy();
}  // end ProtoSpace::Destroy()

bool ProtoSpace::InsertNode(Node& node)
{
    if (0 == num_dimensions)
    {
        unsigned int numDimensions = node.GetDimensions();
        if (NULL == (ord_tree = new ProtoSortedTree[numDimensions]))
        {
            PLOG(PL_ERROR, "ProtoSpace::InsertNode() error: unable to allocate Ordinate tree array\n");
            return false;
        }
        num_dimensions = numDimensions;
    }
    else if (node.GetDimensions() != num_dimensions)
    {
        // (TBD) Allow space to be re-scoped if empty???
        PLOG(PL_ERROR, "ProtoSpace::InsertNode() error: Node dimensions does not match space!\n");
        return false;
    }
    
    // Get and Insert "Ordinate" entries for the node for each dimension
    for (unsigned int i = 0; i < num_dimensions; i++)
    {
        Ordinate* ord = static_cast<Ordinate*>(ord_pool.Get());
        if ((NULL == ord) && (NULL == (ord = new Ordinate)))
        {
            PLOG(PL_ERROR, "ProtoSpace::InsertNode() error: unable to allocate Ordinate\n");
            RemoveNode(node);
            return false;
        }
        ord->SetValue(node.GetOrdinate(i));
        ord->SetNode(&node);
        ord_tree[i].Insert(*ord);
    }
    node_count++;
    return true;
}  // end ProtoSpace::InsertNode();


bool ProtoSpace::RemoveNode(Node& node)
{
    if (0 == num_dimensions) return false;
    ASSERT(node.GetDimensions() == num_dimensions);
    bool result = false;
    for (unsigned int i = 0; i < num_dimensions; i++)
    {
        Ordinate tempOrd;
        tempOrd.SetNode(&node);
        tempOrd.SetValue(node.GetOrdinate(i));
        Ordinate* ord = static_cast<Ordinate*>(ord_tree[i].Find(tempOrd.GetKey(), tempOrd.GetKeysize()));
        if (NULL != ord)
        {
            ord_tree[i].Remove(*ord);
            ReturnOrdinateToPool(*ord);
            result = true;
        }
        else
        {
            //PLOG(PL_WARN, "ProtoSpace::RemoveNode() warning: no ordinate[%u] for node\n", i);
            continue;
        }
    }
    if (result) node_count--;
    return result;
}  // end ProtoSpace::RemoveNode()

bool ProtoSpace::ContainsNode(Node& node)
{
    if (0 == num_dimensions) return false;
    ASSERT(node.GetDimensions() == num_dimensions);
    bool result = true;
    for (unsigned int i = 0; i < num_dimensions; i++)
    {
        Ordinate tempOrd;
        tempOrd.SetNode(&node);
        tempOrd.SetValue(node.GetOrdinate(i));
        Ordinate* ord = static_cast<Ordinate*>(ord_tree[i].Find(tempOrd.GetKey(), tempOrd.GetKeysize()));
        if (NULL == ord)
        {
            //PLOG(PL_WARN, "ProtoSpace::RemoveNode() warning: no ordinate[%u] for node\n", i);
            return false;
        }
    }
    return result;
}  // end ProtoSpace::ContainsNode()


ProtoSpace::Iterator::Iterator(ProtoSpace& theSpace)
 : space(theSpace), orig(NULL), bbox_radius(0.0), x_factor(0.0),
   pos_it(NULL), neg_it(NULL)
{
}

ProtoSpace::Iterator::~Iterator()
{
    Destroy();
}

bool ProtoSpace::Iterator::Init(const double* originOrdinates)
{
    Destroy();
    // Allocate and set origin ordinates for iteration
    unsigned int dim = space.GetDimensions();
    if (NULL == (orig = new double[dim]))
    {
        PLOG(PL_ERROR, "ProtoSpace::Iterator::Init() error: unable to allocate 'orig' array: %s\n",
                       GetErrorString());
        return false;
    }
    if (NULL != originOrdinates)
        memcpy(orig, originOrdinates, dim*sizeof(double));
    else
        memset(orig, 0, dim*sizeof(double));
    
    // Allocate and init positive ordinate iterators
    if (NULL == (pos_it = new ProtoSortedTree::Iterator*[dim]))
    {
        PLOG(PL_ERROR, "ProtoSpace::Iterator::Init() error: unable to allocate 'pos_it' array: %s\n",
                       GetErrorString());
        Destroy();
        return false;
    }
    memset(pos_it, 0, dim*sizeof(ProtoSortedTree::Iterator*));
    Ordinate tempOrd;
    for (unsigned int i = 0; i < dim; i++)
    {
        tempOrd.SetValue(orig[i]);
        if (NULL == (pos_it[i] = new ProtoSortedTree::Iterator(space.ord_tree[i], 
                                                               false, 
                                                               tempOrd.GetKey(),
                                                               Ordinate::KEYBITS)))
        {
            Destroy();
            return false;
        }
    }
    
    // Allocate and init negative ordinate iterators
    if (NULL == (neg_it = new ProtoSortedTree::Iterator*[dim]))
    {
        PLOG(PL_ERROR, "ProtoSpace::Iterator::Init() error: unable to allocate 'pos_it' array: %s\n",
                       GetErrorString());
        Destroy();
        return false;
    }
    memset(neg_it, 0, dim*sizeof(ProtoSortedTree::Iterator*));
    for (unsigned int i = 0; i < dim; i++)
    {
        tempOrd.SetValue(orig[i]);
        if (NULL == (neg_it[i] = new ProtoSortedTree::Iterator(space.ord_tree[i], 
                                                               false, 
                                                               tempOrd.GetKey(),
                                                               Ordinate::KEYBITS)))
        {
            Destroy();
            return false;
        }
        neg_it[i]->Reverse();
    }

#ifdef USE_SDT
    // Create bounding box for SDT visualization
    printf("node ul position %f,%f label off\n", originOrdinates[0], originOrdinates[1]);
    printf("node ur position %f,%f label off\n", originOrdinates[0], originOrdinates[1]);
    printf("node ll position %f,%f label off\n", originOrdinates[0], originOrdinates[1]);
    printf("node lr position %f,%f label off\n", originOrdinates[0], originOrdinates[1]);
    printf("link ul,ur,blue,2\n");
    printf("link ul,ll,blue,2\n");
    printf("link ur,lr,blue,2\n");
    printf("link ll,lr,blue,2\n");
#endif // USE_SDT
        
    bbox_radius = 0.0;
    x_factor = sqrt((double)dim);
    
    return true;
}  // end ProtoSpace::Iterator::Init()

void ProtoSpace::Iterator::Destroy()
{
    // empty our queue
    Ordinate* nextOrd;
    while (NULL != (nextOrd = static_cast<Ordinate*>(ord_tree.RemoveHead())))
        space.ReturnOrdinateToPool(*nextOrd);
    unsigned int dim = space.GetDimensions();
    if (NULL != pos_it)
    {
        for (unsigned int i = 0; i < dim; i++)
        {
            if (NULL != pos_it[i]) delete pos_it[i];
        }
        delete[] pos_it;
        pos_it = NULL;
    }
    if (NULL != neg_it)
    {
        for (unsigned int i = 0; i < dim; i++)
        {
            if (NULL != neg_it[i]) delete neg_it[i];
        }
        delete[] neg_it;
        neg_it = NULL;
    }
    if (NULL != orig)
    {
        delete[] orig;
        orig = NULL;
    }
}  // end ProtoSpace::Iterator::Destroy()


void ProtoSpace::Iterator::Reset(const double* originOrdinates)
{
    unsigned int dim = space.GetDimensions();
    ASSERT(NULL != orig);
    if (NULL != originOrdinates)
        memcpy(orig, originOrdinates, dim*sizeof(double));
    Ordinate tempOrd;
    for (unsigned int i = 0; i < dim; i++)
    {
        tempOrd.SetValue(orig[i]);
        pos_it[i]->Reset(false, tempOrd.GetKey(), Ordinate::KEYBITS);   
        neg_it[i]->Reset(false, tempOrd.GetKey(), Ordinate::KEYBITS);   
        neg_it[i]->Reverse(); 
    }
    // empty our queue
    Ordinate* nextOrd;
    while (NULL != (nextOrd = static_cast<Ordinate*>(ord_tree.RemoveHead())))
        space.ReturnOrdinateToPool(*nextOrd);
}  // end ProtoSpace::Iterator::Reset()

/**
 * Return the Node of those discovered by our expanding
 * bounding box that meets these criteria
 *    1) Closest location to <orig_x, orig_y>, _and_
 *    2) Falls within the current bounding box
 */  
ProtoSpace::Node* ProtoSpace::Iterator::GetNextNode(double* distance)
{
    unsigned int dim = space.GetDimensions();
    while(1)
    {
        Ordinate* nextOrd = static_cast<Ordinate*>(ord_tree.GetHead());
        if (NULL != nextOrd)
        {
            Node* nextNode = nextOrd->GetNode();
            ASSERT(NULL != nextNode);
            bool inBox = true;
            if (bbox_radius >= 0.0)
            {
            
                // Is the "nextNode" within the bbox*x_factor
                // (Note "x_factor = radius * sqrt(num_dimensions))
                double xRadius = bbox_radius / x_factor;
                for (unsigned int i = 0; i < dim; i++)
                {
                    if ((nextNode->GetOrdinate(i) <= (orig[i] - xRadius)) ||
                        (nextNode->GetOrdinate(i) >= (orig[i] + xRadius)))
                    {
                        inBox = false;
                        break;
                    }
                }
            }
            if (inBox)
            {
                ord_tree.RemoveHead();
                if (NULL != distance) 
                {
                    *distance = sqrt(nextOrd->GetValue());
                }
                space.ReturnOrdinateToPool(*nextOrd);
                return nextNode;
            }
            
        }
        else if (bbox_radius < 0.0)
        {
            return NULL;  // finished
        }
        
        
        // A) Find the ordinate with smallest delta from origin
        //    to set "radius" of current bounding space
        double deltaMin = -1.0;
        unsigned int index = 0;  // which dimensional iterator was closest
        bool neg = false;        // Was it "pos_it" or "neg_it"
        for (unsigned int direction = 0; direction <= 1; direction++)
        {
            for (unsigned int i = 0 ; i < dim; i++)
            {
                Ordinate* ord = (0 == direction) ?
                                    static_cast<Ordinate*>(neg_it[i]->PeekPrevItem()) :
                                    static_cast<Ordinate*>(pos_it[i]->PeekNextItem());
                if (NULL != ord)
                {
                    Node* node = ord->GetNode();
                    ASSERT(NULL != node);
                    double delta = fabs(ord->GetValue() - orig[i]);
                    if ((delta < deltaMin) || (deltaMin < 0.0))
                    {
                        deltaMin = delta;
                        index = i;
                        neg = (0 == direction);
                    }
                }
            }
        }
        bbox_radius = deltaMin;
        if (deltaMin < 0.0) continue; 
        
        // Output new bounding box for SDT visualization
#ifdef USE_SDT
        printf("node ul position %f,%f\n", orig[0] - deltaMin, orig[1] - deltaMin);
        printf("node ur position %f,%f\n", orig[0] + deltaMin, orig[1] - deltaMin);
        printf("node ll position %f,%f\n", orig[0] - deltaMin, orig[1] + deltaMin);
        printf("node lr position %f,%f\n", orig[0] + deltaMin, orig[1] + deltaMin);
#endif // USE_SDT
                
        Node* node = neg ? static_cast<Ordinate*>(neg_it[index]->PeekPrevItem())->GetNode() :
                           static_cast<Ordinate*>(pos_it[index]->PeekNextItem())->GetNode();

        // B) Is the closest node on this axis within our current bounding box?
        bool inBox = true;
        for (unsigned int i = 0; i < dim; i++)
        {
            double ordDelta = fabs(orig[i] - node->GetOrdinate(i));
            if (ordDelta > deltaMin)
            {
                inBox = false;
                break;
            }
        }
        
        // C) Enqueue any nodes that lie _within_ the current bounding space
        if (inBox)
        {
            // a) calculate distance
            double distPartial = 0.0;
            for (unsigned j = 0; j < dim; j++)
            {
                double delta = node->GetOrdinate(j) - orig[j];
                distPartial += delta*delta;
            }
                    
            // b) get and init Ordinate 
            Ordinate* ord = space.GetOrdinateFromPool();
            if ((NULL == ord) && (NULL == (ord = new Ordinate)))
            {
                PLOG(PL_ERROR, "ProtoSpace::Iterator::GetNextNode() error: unable to allocate Ordinate: %s\n",
                        GetErrorString());
                return NULL;
            }
            ord->SetNode(node);
            ord->SetValue(distPartial);
            
            // c) if not already enqueued, enqueue it
            if (NULL == ord_tree.Find(ord->GetKey(), Ordinate::KEYBITS))
                ord_tree.Insert(*ord);
            else
                space.ReturnOrdinateToPool(*ord);
        }
        
        // D) Consume the applicable ordinate, expanding our bounding box
        if (neg)
            neg_it[index]->GetPrevItem();
        else
            pos_it[index]->GetNextItem();

#ifdef USE_SDT        
        printf("wait 1\n");
#endif // USE_SDT
                
    }  // end while(1)
  
}  // end ProtoSpace::Iterator::GetNextNode()