File: CNearTree.h

package info (click to toggle)
neartree 5.1.1+dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,380 kB
  • sloc: cpp: 12,313; ansic: 5,956; makefile: 495
file content (1727 lines) | stat: -rw-r--r-- 70,046 bytes parent folder | download
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
/*
 *  CNearTree.h
 *  NearTree
 *
 *  Based on TNear.h C++ Template
 *  Copyright 2001 Larry Andrews.  All rights reserved
 *
 *  C Version created by Herbert J. Bernstein on 11/29/08
 *  with permission from Larry Andrews.
 *  Copyright 2008 Larry Andrews and Herbert J. Bernstein. 
 *  All rights reserved.
 *
 *  Revised 30 May 2009, release with full containerization of C++
 *                       version and KNear/Far in C++ and C, LCA + HJB
 */

/**********************************************************************
 *                                                                    *
 * YOU MAY REDISTRIBUTE THE CNearTree API UNDER THE TERMS OF THE LGPL *
 *                                                                    *
 **********************************************************************/

/************************* LGPL NOTICES *******************************
 *                                                                    *
 * This library is free software; you can redistribute it and/or      *
 * modify it under the terms of the GNU Lesser General Public         *
 * License as published by the Free Software Foundation; either       *
 * version 2.1 of the License, or (at your option) any later version. *
 *                                                                    *
 * This library is distributed in the hope that it will be useful,    *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
 * Lesser General Public License for more details.                    *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License along with this library; if not, write to the Free         *
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,    *
 * MA  02110-1301  USA                                                *
 *                                                                    *
 **********************************************************************/

/* Notices from original C++ template:
 
 Nearest Neighbor algorithm after Kalantari and McDonald,
 (IEEE Transactions on Software Engineering, v. SE-9, pp.
 631-634,1983)
 modified to use recursion instead of a double-linked tree
 and simplified so that it does a bit less checking for
 things like is the distance to the right less than the
 distance to the left; it was found that these checks little
 to no difference.
 
 copyright by Larry Andrews, 2001
 may be freely distributed or used as long as this copyright notice
 is included
 */

#ifndef CNEARTREE_H_INCLUDED
#define CNEARTREE_H_INCLUDED

#ifdef __cplusplus

extern "C" {
    
#endif
    
#ifdef CNEARTREE_USE_FAR
#include <malloc.h>
#define CNEARTREE_FAR __far
#define CNEARTREE_MALLOC _fmalloc
#define CNEARTREE_FREE _ffree
#define CNEARTREE_MEMSET _fmemset
#define CNEARTREE_MEMMOVE _fmemmove
#else
#include <stdlib.h>
#define CNEARTREE_FAR
#define CNEARTREE_MALLOC malloc
#define CNEARTREE_FREE free
#define CNEARTREE_MEMSET memset
#define CNEARTREE_MEMMOVE memmove
#endif
    
#include <limits.h>
#include <float.h>
#include <math.h>
#ifndef USE_LOCAL_HEADERS
#include <CVector.h>
#else
#include "CVector.h"
#endif
#ifndef CVECTOR_FAR
#define CVECTOR_FAR CNEARTREE_FAR
#endif
    
    
    
#ifndef USE_LOCAL_HEADERS
#include <rhrand.h>
#else
#include "rhrand.h"
#endif
    
    
    /* function returns */
#define CNEARTREE_SUCCESS       0
#define CNEARTREE_MALLOC_FAILED 1
#define CNEARTREE_BAD_ARGUMENT  2
#define CNEARTREE_NOT_FOUND     4
#define CNEARTREE_FREE_FAILED   8
#define CNEARTREE_CVECTOR_FAILED   16
    
    
    /* flags 
     0 for n data, n children
     */
    
#define CNEARTREE_FLAG_LEFT_DATA   1L          /* 0x0001 */
#define CNEARTREE_FLAG_RIGHT_DATA  2L          /* 0x0002 */
#define CNEARTREE_FLAG_LEFT_CHILD  4L          /* 0x0004 */
#define CNEARTREE_FLAG_RIGHT_CHILD 8L          /* 0x0008 */
    
#define CNEARTREE_DATA_OR_CHILDREN 15L         /* 0x000F */
    
#define CNEARTREE_TYPE_DOUBLE      16L         /* 0x0010 */
#define CNEARTREE_TYPE_INTEGER     32L         /* 0x0020 */
#define CNEARTREE_TYPE_STRING      64L         /* 0x0040 */
    
#define CNEARTREE_TYPE            112L         /* 0x0070 */
    
#define CNEARTREE_NORM_UNKNOWN    128L         /* 0x0080 */
#define CNEARTREE_NORM_L1         256L         /* 0x0100 */
#define CNEARTREE_NORM_L2         512L         /* 0x0200 */
#define CNEARTREE_NORM_LINF      1024L         /* 0x0400 */
#define CNEARTREE_NORM_SPHERE    2048L         /* 0x0800 */
#define CNEARTREE_NORM_HSPHERE   4096L         /* 0x1000 */
#define CNEARTREE_NORM_HAMMING   8192L         /* 0x2000 */
#define CNEARTREE_NORM_L2LAZY   16384L         /* 0x4000 */
    
#define CNEARTREE_NORM          32640L         /* 0x7F80 */
    
    
/*  Execution Control Flags */
    
#define CNTF_NOPREPRUNE    0x10000L     /*flag to supress all search prepruning */
#define CNTF_FORCEPREPRUNE 0x20000L     /*flag to force search prepruning       */
#define CNTF_NOFLIP        0x40000L     /*flag to suppress flips on insert      */
#define CNTF_FORCEFLIP     0x80000L     /*flag to force flips on insert         */
#define CNTF_NODEFER      0x100000L     /*flag to prevent deferred insert       */
#define CNTF_SKNN         0x200000L     /*flag to use spherical KNN             */

    
#define CNEARTREE_XFLAGS  0x3F0000L     /*mask for execution flags */
    
#ifdef CNEARTREE_FORCEPREPRUNE
  #define CNFT_FLAGDEFAULTPRUNE  CNTF_FORCEPREPRUNE
  #ifdef CNEARTREE_NOPREPRUNE
    #error "CNEARTREE_NOPREPRUNE conflicts with  CNEARTREE_FORCEPREPRUNE"
  #endif
#else
  #ifdef CNEARTREE_NOPREPRUNE
    #define  CNFT_FLAGDEFAULTPRUNE  CNTF_NOPREPRUNE
  #else
    #define  CNFT_FLAGDEFAULTPRUNE  0
  #endif
#endif
    
#ifdef CNEARTREE_FORCEFLIP
  #define CNFT_FLAGDEFAULTFLIP  CNTF_FORCEFLIP
  #ifdef CNEARTREE_NOFLIP
    #error "CNEARTREE_NOFLIP conflicts with  CNEARTREE_FORCEFLIP"
  #endif
#else
  #ifdef CNEARTREE_NOFLIP
    #define CNFT_FLAGDEFAULTFLIP CNTF_NOFLIP
  #else
    #define CNFT_FLAGDEFAULTFLIP 0
  #endif
#endif
    
#ifdef CNEARTREE_NODEFER
    #define CNFT_FLAGDEFAULTDEFER CNTF_NODEFER
#else
    #define CNFT_FLAGDEFAULTDEFER 0
#endif
    
#define CNTF_FLAGSDEFAULT  (CNFT_FLAGDEFAULTPRUNE|CNFT_FLAGDEFAULTFLIP|CNFT_FLAGDEFAULTDEFER)
    
    
#define CNEARTREE_FLIP          0     /* Not used, defined for compatibility */
#define CNEARTREE_DEFER_ALL     0     /* Not used, defined for compatibility */
    
    
    
    typedef struct _CNearTreeNode {
        size_t           m_indexLeft;     /* index of left coords in m_CoordStore  
                                             and of left object in m_ObjectStore   */
        size_t           m_indexRight;    /* index of right coords in m_CoordStore 
                                             and of right object in m_ObjectStore  */
        double           m_dMaxLeft;      /* longest distance from the left object
                                             to anything below it in the tree      */
        double           m_dMaxRight;     /* longest distance from the right object 
                                             to anything below it in the tree      */
        struct _CNearTreeNode CNEARTREE_FAR * m_pLeftBranch;  
                                          /* tree descending from the left object  */
        struct _CNearTreeNode CNEARTREE_FAR * m_pRightBranch; 
                                          /* tree descending from the right object */
        long              m_iflags;       /* flags                   */
        size_t            m_iTreeSize;    /* size of this node tree  */
#ifdef CNEARTREE_INSTRUMENTED
        size_t            m_Height;        /* height of this node     */
#endif

    } CNearTreeNode;
    
    
    typedef CNearTreeNode CNEARTREE_FAR * CNearTreeNodeHandle;   
    
    typedef struct {
        CNearTreeNodeHandle m_ptTree;     /* pointer to the actual tree                  */
        size_t           m_szdimension;   /* dimension of the coordinates                */
        size_t           m_szsize;        /* size of this tree                           */
        size_t           m_szdepth;       /* depth of this tree                          */
        long             m_iflags;        /* flags                                       */
        CVectorHandle    m_ObjectStore;   /* all inserted objects                        */
        CVectorHandle    m_CoordStore;    /* all inserted coordinates                    */
        CVectorHandle    m_DelayedIndices;/* objects queued for insertion                */

        CRHrand          m_rhr;           /* random number generator                     */
        double           m_DiamEstimate;  /* estimated diameter */
        double           m_SumSpacings;   /* sum of spacings at time of insertion */
        double           m_SumSpacingsSq; /* sum of spacings squared at time of insertion */
        double           m_DimEstimate;   /* estimated dimension */
        double           m_DimEstimateEsd;/* estimated dimension estimated standard deviation */
#ifdef CNEARTREE_INSTRUMENTED
        size_t           m_NodeVisits;    /* number of node visits */
#endif
    } CNearTree;
    
    typedef CNearTree     CNEARTREE_FAR * CNearTreeHandle;
    
/* Distance Macros for L2LAZY */
    
    
#define CNTM_Abs(x) (((x)<0.)?(-(x)):(x))
#define CNTM_DistL2sq(distsq,treehandle,coord1,coord2) {     \
        size_t index;                                     \
        size_t treedim; \
        long treetype;  \
        double CNEARTREE_FAR * dcoord1 = NULL; \
        double CNEARTREE_FAR * dcoord2 = NULL; \
        int CNEARTREE_FAR * icoord1 = NULL; \
        int CNEARTREE_FAR * icoord2 = NULL; \
        treedim = (treehandle)->m_szdimension; \
        treetype = (treehandle)->m_iflags&CNEARTREE_TYPE; \
        if (treetype == CNEARTREE_TYPE_DOUBLE) { \
            dcoord1 = (double CNEARTREE_FAR *)(coord1);\
            dcoord2 = (double CNEARTREE_FAR *)(coord2);\
        } else if (treetype == CNEARTREE_TYPE_INTEGER) { \
            icoord1 = (int CNEARTREE_FAR *)(coord1); \
            icoord2 = (int CNEARTREE_FAR *)(coord2); \
        } \
        distsq = 0.; \
        if (treetype == CNEARTREE_TYPE_DOUBLE) { \
            distsq = (dcoord1[0]-dcoord2[0])*(dcoord1[0]-dcoord2[0]); \
            \
            for (index=1; index < treedim; index++) { \
                distsq += (dcoord1[index]-dcoord2[index]) \
                *(dcoord1[index]-dcoord2[index]); \
            } \
        } else if (treetype == CNEARTREE_TYPE_INTEGER) { \
            distsq = ((double)(icoord1[0]-icoord2[0]))*((double)(icoord1[0]-icoord2[0])); \
            \
            for (index=1; index < treedim; index++) { \
                distsq += ((double)(icoord1[index]-icoord2[index])) \
                *((double)(icoord1[index]-icoord2[index])); \
            } \
        }  \
        }

#define CNTM_DistL2(dist,treehandle,coord1,coord2) {     \
        size_t index;                                     \
        double distsq=0.;                                    \
        size_t treedim; \
        long treetype;  \
        double CNEARTREE_FAR * dcoord1 = NULL; \
        double CNEARTREE_FAR * dcoord2 = NULL; \
        int CNEARTREE_FAR * icoord1 = NULL; \
        int CNEARTREE_FAR * icoord2 = NULL; \
        treedim = (treehandle)->m_szdimension; \
        treetype = (treehandle)->m_iflags&CNEARTREE_TYPE; \
        if (treetype == CNEARTREE_TYPE_DOUBLE) { \
            dcoord1 = (double CNEARTREE_FAR *)(coord1);\
            dcoord2 = (double CNEARTREE_FAR *)(coord2);\
        } else if (treetype == CNEARTREE_TYPE_INTEGER) { \
            icoord1 = (int CNEARTREE_FAR *)(coord1); \
            icoord2 = (int CNEARTREE_FAR *)(coord2); \
        } \
        if (treedim == 1) { \
            if (treetype == CNEARTREE_TYPE_DOUBLE) { \
                (dist) = CNTM_Abs(dcoord1[0]-dcoord2[0]); \
            } else if (treetype == CNEARTREE_TYPE_INTEGER) { \
                (dist) = CNTM_Abs((double)(icoord1[0]-icoord2[0])); \
            } \
        } else { \
            if (treetype == CNEARTREE_TYPE_DOUBLE) { \
                distsq = (dcoord1[0]-dcoord2[0])*(dcoord1[0]-dcoord2[0]); \
                \
                for (index=1; index < treedim; index++) { \
                    distsq += (dcoord1[index]-dcoord2[index]) \
                    *(dcoord1[index]-dcoord2[index]); \
                } \
            } else if (treetype == CNEARTREE_TYPE_INTEGER) { \
                distsq = ((double)(icoord1[0]-icoord2[0]))*((double)(icoord1[0]-icoord2[0])); \
                \
                for (index=1; index < treedim; index++) { \
                    distsq += ((double)(icoord1[index]-icoord2[index])) \
                    *((double)(icoord1[index]-icoord2[index])); \
                } \
            }  \
            (dist) = sqrt(distsq);\
        }\
        }
  
#define CNTM_DistL1(dist,treehandle,coord1,coord2) {\
        size_t index;   \
        size_t treedim; \
        long treetype;  \
        double CNEARTREE_FAR * dcoord1 = NULL; \
        double CNEARTREE_FAR * dcoord2 = NULL; \
        int CNEARTREE_FAR * icoord1 = NULL; \
        int CNEARTREE_FAR * icoord2 = NULL; \
        \
        treedim = treehandle->m_szdimension; \
        treetype = treehandle->m_iflags&CNEARTREE_TYPE; \
        (dist) = 0.; \
        \
        if (treetype == CNEARTREE_TYPE_DOUBLE) { \
            dcoord1 = (double CNEARTREE_FAR *)(coord1);\
            dcoord2 = (double CNEARTREE_FAR *)(coord2);\
        } else if (treetype == CNEARTREE_TYPE_INTEGER) { \
            icoord1 = (int CNEARTREE_FAR *)(coord1); \
            icoord2 = (int CNEARTREE_FAR *)(coord2); \
        } \
       if (treetype == CNEARTREE_TYPE_DOUBLE) { \
            (dist)= fabs(dcoord1[0]-dcoord2[0]); \
            for (index=1; index < treedim; index++) { \
                (dist) += CNTM_Abs(dcoord1[index]-dcoord2[index]); \
            } \
        } else if (treetype == CNEARTREE_TYPE_INTEGER) { \
            (dist) = fabs((double)(icoord1[0]-icoord2[0])); \
            for (index=1; index < treedim; index++) { \
                (dist) += CNTM_Abs((double)(dcoord1[index]-dcoord2[index])); \
            } \
        }   \
    }

    
    /*
     =======================================================================
     double CNearTreeDistsq(void CNEARTREE_FAR * coord1, 
     void CNEARTREE_FAR * coord2,  
     size_t treedim, 
     int treetype)
     
     function to return the square of the Euclidean distance between two 
     coordinate vectors.  
     
     THIS FUNCTION IS DEPRECATED
     
     treedim -- the dimension of the vectors
     
     treetype -- and integer flag for type of the vectors
     CNEARTREE_TYPE_DOUBLE for double
     CNEARTREE_TYPE_INTEGER for integer
     
     =======================================================================
     */
    
    double CNearTreeDistsq(void CNEARTREE_FAR * coord1,
                           void CNEARTREE_FAR * coord2, 
                           size_t treedim, 
                           long treetype);

     /*
     =======================================================================
     double CNearTreeDist(const CNearTreeHandle treehandle, 
     void CNEARTREE_FAR * coord1, 
     void CNEARTREE_FAR * coord2)
     
     function to return the distance (L1, L2 or L-infinity) between two 
     coordinate vectors according to the parameters of the given tree  
     For L2LAZY tree this returns the L2 norm distance.
     
     =======================================================================
     */
    
    double CNearTreeDist(const CNearTreeHandle treehandle, 
                              void CNEARTREE_FAR * coord1,
                              void CNEARTREE_FAR * coord2);
    /*
     =======================================================================
     int CNearTreeSetNorm(const CNearTreeHandle treehandle, int treenorm);
     
     function to set the norm to use used for this tree
     
     treenorm should be one of CNEARTREE_NORM_L1 for an L-1 norm
     CNEARTREE_NORM_L2 for an L-2 norm
     CNEARTREE_NORM_LINF for an L-infinity norm
     CNEARTREE_NORM_LAZY for an L-1 norm used for L-2 searching
     
     the function returns CNEARTREE_BAD_ARGUMENT for an invalid argument
     CNEARTREE_SUCCESS (0) otherwise
     =======================================================================
     */
    
    int CNearTreeSetNorm(const CNearTreeHandle treehandle, int treenorm);
    
    
    /*
     =======================================================================
     int CNearTreeNodeCreate ( const CNearTreeHandle treehandle,  
     CNearTreeNodeHandle CNEARTREE_FAR * treenodehandle) 
     
     Create a CNearTreeNode
     
     returns a pointer to the newly allocated block of memory as a 
     CNearTreeNodeHandle in *treenodehandle
     
     flags are inherited from the treehandle  
     
     creates an empty tree with no right or left node and with the dMax-below
     set to negative values so that any match found will be stored since it will
     greater than the negative value
     
     =======================================================================
     */
    
    int CNearTreeNodeCreate ( const CNearTreeHandle treehandle,  
                             CNearTreeNodeHandle CNEARTREE_FAR * treenodehandle);
    
    
    /*
     =======================================================================
     int CNearTreeCreate ( CNearTreeHandle CNEARTREE_FAR * treehandle, 
     size_t treedim, long treetype)
     
     Create a CNearTree
     
     returns a pointer to the newly allocated block of memory as a 
     CNearTreeHandle in *treehandle
     
     
     treedim -- the dimension of the vectors
     treetype -- double or integer flag for type of the vectors ored with norm
                 and ored with execution flags
     CNEARTREE_TYPE_DOUBLE for double
     CNEARTREE_TYPE_INTEGER for integer
     CNEARTREE_TYPE_STRING for strings
     ored with
     CNEARTREE_NORM_L1        for the sum of the absolute values
     CNEARTREE_NORM_L2        for the square root of the sum of the squares
     CNEARTREE_NORM_LINF      for the max
     CNEARTREE_NORM_SPHERE    for norm as spherical angular distance
     CNEARTREE_NORM_HSPHERE   for norm as hemispherical angular distance
     CNEARTREE_NORM_HAMMING   for norm as string hamming distance
     ored with
     CNTF_NOPREPRUNE    0x10000L    flag to supress all search prepruning
     CNTF_FORCEPREPRUNE 0x20000L    flag to force search prepruning
     CNTF_NOFLIP        0x40000L    flag to suppress flips on insert
     CNTF_FORCEFLIP     0x80000L    flag to force flips on insert
     CNTF_NODEFER      0x100000L    flag to prevent deferred insert 
     
     creates an empty tree with no right or left node and with the dMax-below
     set to negative values so that any match found will be stored since it will
     greater than the negative value
     
     =======================================================================
     */
    
    int CNearTreeCreate(CNearTreeHandle CNEARTREE_FAR * treehandle, 
                        size_t treedim, 
                        long treetype);
    
    /*
     =======================================================================
     int CNearTreeFree ( CNearTreeHandle CNEARTREE_FAR * treehandle )
     
     Free a CNearTree
     
     recursively frees the NearTree with the handle *treehandle
     and nulls the treehandle.
     
     note that the objects referenced are not freed.
     =======================================================================
     */
    
    int CNearTreeFree(CNearTreeHandle CNEARTREE_FAR * treehandle);
    
    /*
     =======================================================================
     int CNearTreeClear ( const CNearTreeHandle treehandle )
     
     Clear a CNearTree
     
     Clears the NearTree with the handle *treehandle
     
     note that the objects referenced are not freed.
     =======================================================================
     */
    
    int CNearTreeClear ( const CNearTreeHandle treehandle );
    
    
    /*
     =======================================================================
     size_t CNearTreeSize (const CNearTreeHandle treehandle)
     
     Macro to get Tree Size with no error checking
     
     =======================================================================
     */
    
#define CNearTreeSize(treehandle) \
((treehandle)?(((treehandle)->m_CoordStore)?(CVectorSize((treehandle)->m_CoordStore)):0):0)
    
    /*
     =======================================================================
     int CNearTreeGetSize (const CNearTreeHandle treehandle, size_t CNEARTREE_FAR * size)
     
     Return the number of objects in the tree in size
     
     =======================================================================
     */
    
    int CNearTreeGetSize (const CNearTreeHandle treehandle, size_t CNEARTREE_FAR * size);
    
    /*
     =======================================================================
     int CNearTreeGetDelayedSize (const CNearTreeHandle treehandle, size_t CNEARTREE_FAR * size)
     
     Return the number of objects in the delay queue tree in size
     This is a deprecated alternate name for CNearTreeGetDeferredSize
     
     =======================================================================
     */
    
    int CNearTreeGetDelayedSize (const CNearTreeHandle treehandle, size_t CNEARTREE_FAR * size);

    /*
     =======================================================================
     int CNearTreeGetDeferredSize (const CNearTreeHandle treehandle, size_t CNEARTREE_FAR * size)
     
     Return the number of objects in the delay queue tree in size
     
     =======================================================================
     */
    
    int CNearTreeGetDeferredSize ( const CNearTreeHandle treehandle, size_t CNEARTREE_FAR * size );
    
    /*
     =======================================================================
     int CNearTreeGetTotalSize (const CNearTreeHandle treehandle, size_t CNEARTREE_FAR * size)
     
     Return the number of objects in both in the tree and in the delay queue tree in size
     Identical to CNearTreeGetSize, retained to support older code
     
     =======================================================================
     */
    
#define CNearTreeGetTotalSize(treehandle,size) CNearTreeGetSize(treehandle,size)
    /*
     =======================================================================
     int CNearTreeGetDepth (const CNearTreeHandle treehandle, size_t CNEARTREE_FAR * depth)
     
     Return the depth of the tree in depth
     
     =======================================================================
     */
    
    int CNearTreeGetDepth (const CNearTreeHandle treehandle, size_t CNEARTREE_FAR * depth);

    /*
     =======================================================================
     int CNearTreeGetHeight (const CNearTreeHandle treehandle, size_t CNEARTREE_FAR * height)
     
     Return the height of the tree in height, of instrumented, otherwise the depth
     
     =======================================================================
     */
    
    
    int CNearTreeGetHeight ( const CNearTreeHandle treehandle, size_t CNEARTREE_FAR * height );

    
    /*
     =======================================================================

     int CNearTreeGetFlags(const CNearTreeHandle treehandle, 
     long CNEARTREE_FAR *flags, 
     const long mask)
     
     int CNearTreeSetFlags(const CNearTreeHandle treehandle,
     const long flags,
     const long mask)
     
     functions to get or set the execution control flags:
     
        CNTF_NOPREPRUNE    to supress all search prepruning
        CNTF_FORCEPREPRUNE to force search prepruning      
        CNTF_NOFLIP        flag to suppress flips on insert
        CNTF_FORCEFLIP     flag to force flips on insert
        CNTF_NODEFER       flag to prevent deferred insert
    
     The desired flags may be ored.  If mask is no zero it is
     used as a bit mask, so a call with a flags of zero and a
     mask equal to a particular flag, clears that flag.
     =======================================================================
     */
    
    int CNearTreeGetFlags(const CNearTreeHandle treehandle, 
                          long CNEARTREE_FAR *flags, 
                          const long mask);
    
    int CNearTreeSetFlags(const CNearTreeHandle treehandle,
                          const long flags,
                          const long mask);
    
    /*
     =======================================================================
     int CNearTreeGetMeanSpacing ( const CNearTreeHandle treehandle, 
     double CNEARTREE_FAR * spacing  )
     
     Get an estimate of the spacing of points
     
     
     =======================================================================
     */
    
    int CNearTreeGetMeanSpacing ( const CNearTreeHandle treehandle, 
                                 double CNEARTREE_FAR * spacing  );
    
    /*
     =======================================================================
     int CNearTreeGetVarSpacing ( const CNearTreeHandle treehandle, 
     double CNEARTREE_FAR * varspacing  )
     
     Get an estimate of variance of the spacing of points
     
     
     =======================================================================
     */
    int CNearTreeGetVarSpacing ( const CNearTreeHandle treehandle, 
                                double CNEARTREE_FAR * varspacing  );
    
    /*
     =======================================================================
     int CNearTreeCount(const CNearTreeHandle treehandle, 
     size_t CNEARTREE_FAR * count)
     =======================================================================
     */
    
    int CNearTreeCount(const CNearTreeHandle treehandle, 
                       size_t CNEARTREE_FAR * count);
                        
    
#ifndef CNEARTREE_INSTRUMENTED
    /*
     =======================================================================
     int CNearTreeGetNodeVisits (  const CNearTreeHandle treehandle,
     size_t CNEARTREE_FAR * visits)
     
     Get the number of visits to nodes
     Dummy version to return 0
     
     
     =======================================================================
     */
    int CNearTreeGetNodeVisits ( const CNearTreeHandle treehandle,
                                size_t CNEARTREE_FAR * visits);
    
#endif
    
#ifdef CNEARTREE_INSTRUMENTED
    /*
     =======================================================================
     int CNearTreeGetNodeVisits ( const CNearTreeHandle treehandle,
     size_t CNEARTREE_FAR * visits)
     
     Get the number of visits to nodes
     
     
     =======================================================================
     */
    int CNearTreeGetNodeVisits ( const CNearTreeHandle treehandle,
                                size_t CNEARTREE_FAR * visits);
    /*
     =======================================================================
     int CNearTreeSetNodeVisits (  const CNearTreeHandle treehandle,
     const size_t visits )
     
     Set the number of visits to nodes
     
     
     =======================================================================
     */
    int CNearTreeSetNodeVisits (  const CNearTreeHandle treehandle,
                                const size_t visits );
#endif    
    
    /*
     =======================================================================
     int CNearTreeGetDiamEstimate (  const CNearTreeHandle treehandle,
     double CNEARTREE_FAR * diamest )
     
     Get an estimate of the diameter
     
     
     =======================================================================
     */
    int CNearTreeGetDiamEstimate ( const CNearTreeHandle treehandle,
                                  double CNEARTREE_FAR * diamest );
    
    /*
     =======================================================================
     int CNearTreeGetDimEstimateEsd ( const CNearTreeHandle treehandle,
     double CNEARTREE_FAR * dimestesd )
     
     Get the current best estimate of the dimension esd
     
     =======================================================================
     */
    int CNearTreeGetDimEstimateEsd ( const CNearTreeHandle treehandle,
                                    double CNEARTREE_FAR * dimestesd );
    
    
    /*
     =======================================================================
     int CNearTreeGetDimEstimate ( const CNearTreeHandle treehandle,
     double CNEARTREE_FAR * diamest,
     const double DimEstimateEsd )
     
     Get an estimate of the dimension of the collection of points
     in the tree, to within the specified esd
     
     
     =======================================================================
     */
    int CNearTreeGetDimEstimate ( const CNearTreeHandle treehandle,
                                 double CNEARTREE_FAR * dimest,
                                 const double DimEstimateEsd );
    
    /*
     =======================================================================
    int CNearTreeNodeCount(const CNearTreeNodeHandle treenodehandle, 
                           size_t CNEARTREE_FAR * count)
     =======================================================================
     */
    
    int CNearTreeNodeCount(const CNearTreeNodeHandle treenodehandle, 
                           size_t CNEARTREE_FAR * count);
    /*
     =======================================================================
     int CNearTreeImmediateInsert ( const CNearTreeHandle treehandle, 
     const void CNEARTREE_FAR * coord, 
     const void CNEARTREE_FAR * obj )
     
     Function to insert some "point" as an object into a CNearTree for
     later searching, but immediately, not into the queue used for
     normal insertions
     
     coord is a coordinate vector for an object, obj, to be inserted into a
     Neartree.  A static copy of the coordinates and a pointer to the
     object are inserted
     
     Three possibilities exist: put the datum into the left
     position (first test),into the right position, or else
     into a node descending from the nearer of those positions
     when they are both already used.
     
     return 0 for success, nonzero for an error
     
     =======================================================================
     */
    
    int CNearTreeImmediateInsert( const CNearTreeHandle treehandle,
                        const void CNEARTREE_FAR * coord, 
                        const void CNEARTREE_FAR * obj );
    
    /*  
     =======================================================================
     int CNearTreeNodeReInsert_Flip ( const CNearTreeHandle treehandle,
     const const CNearTreeNodeHandle treenodehandle, 
     size_t CNEARTREE_FAR * depth)
     
     Function to reinsert the elements from a detached a node into a CNearTree 
     for later searching
     
     treehandle is the handle of the overall neartree being used
     
     treenodehandle is the handle of the node in the existing tree at which to start
     the insertion
     
     pntn is the handle of the previously detached node from which to 
     extract the nodes for insertion
     
     depth is used to keep track of the depth at which the insertion is done
     
     return 0 for success, nonzero for an error
     
     =======================================================================
     */
    
    int CNearTreeNodeReInsert_Flip( const CNearTreeHandle treehandle,
                                   const CNearTreeNodeHandle treenodehandle,
                                   const CNearTreeNodeHandle pntn,
                                   size_t CNEARTREE_FAR * depth);
        
        
    
    
    /*
     =======================================================================
     int CNearTreeNodeInsert ( const CNearTreeHandle treehandle,
     const const CNearTreeNodeHandle treenodehandle, 
     size_t index;
     size_t CNEARTREE_FAR * depth)
     
     Function to insert some "point" as an object into a node in a CNearTree for
     later searching
     
     treenodehandle is the handle of the node at which to start the insertion
     
     index is the index of the object and coordinates to add from 
     treehandle->m_ObjectStore and treehandle->CoordStore into a NearTree.
     A static copy of the coordinates and a pointer to the
     object are inserted
     
     Three possibilities exist: put the datum into the left
     position (first test),into the right position, or else
     into a node descending from the nearer of those positions
     when they are both already used.
     
     depth is used to keep track of the depth at which the insertion is done
     
     return 0 for success, nonzero for an error
     
     =======================================================================
     =======================================================================
     int CNearTreeNodeInsert_Flip ( const CNearTreeHandle treehandle,
     const const CNearTreeNodeHandle treenodehandle, 
     size_t index;
     size_t CNEARTREE_FAR * depth)
     
     Function to insert some "point" as an object into a node in a CNearTree for
     later searching
     
     treenodehandle is the handle of the node at which to start the insertion
     
     index is the index of the object and coordinates to add from 
     treehandle->m_ObjectStore and treehandle->CoordStore into a NearTree.
     A static copy of the coordinates and a pointer to the
     object are inserted
     
     Three possibilities exist: put the datum into the left
     position (first test),into the right position, or else
     into a node descending from the nearer of those positions
     when they are both already used.
     
     depth is used to keep track of the depth at which the insertion is done
     
     return 0 for success, nonzero for an error
     
     =======================================================================
        */
    
    int CNearTreeNodeInsert( const CNearTreeHandle treehandle,
                            const CNearTreeNodeHandle treenodehandle,
                            const size_t index, 
                            size_t CNEARTREE_FAR * depth );
    int CNearTreeNodeInsert_Flip( const CNearTreeHandle treehandle,
                            const CNearTreeNodeHandle treenodehandle,
                            const size_t index, 
                            size_t CNEARTREE_FAR * depth );    
    
    /*
     =======================================================================
     int CNearTreeInsert ( const CNearTreeHandle treehandle, 
     const void CNEARTREE_FAR * coord, 
     const void * obj )
     
     Function to queue some "point" as an object for future insertion
     into a CNearTree for later searching
     
     coord is a coordinate vector for an object, obj, to be inserted into a
     Neartree.  A static copy of the coordinates and a pointer to the
     object are queued for insertion.  The exact order of insertion
     is not predetermined.  It will be partially randomized to help to
     balance the tree.
     
     The insertions will be completed by a call to 
     CNearTreeCompleteDelayedInsert(const CNearTreeHandle treehandle) 
     or by execution of any search.
     
     return 0 for success, nonzero for an error
     
     =======================================================================
     
     */
    
    int CNearTreeInsert( const CNearTreeHandle treehandle,
                               const void CNEARTREE_FAR * coord, 
                               const void * obj );
    
    /*
     =======================================================================
     int CNearTreeDelayedInsert ( const CNearTreeHandle treehandle, 
     const void CNEARTREE_FAR * coord, 
     const void * obj )
     
     *** THIS IS A DEPRECATED ALTLERNATE CALL TO CNearTreeInsert ***
     
     Function to queue some "point" as an object for future insertion
     into a CNearTree for later searching
     
     coord is a coordinate vector for an object, obj, to be inserted into a
     Neartree.  A static copy of the coordinates and a pointer to the
     object are queued for insertion.  The exact order of insertion
     is not predetermined.  It will be partially randomized to help to
     balance the tree.
     
     The insertions will be completed by a call to 
     CNearTreeCompleteDelayedInsert(const CNearTreeHandle treehandle) 
     or by execution of any search.
     
     return 0 for success, nonzero for an error
     
     =======================================================================
     */
    
    int CNearTreeDelayedInsert( const CNearTreeHandle treehandle,
                               const void CNEARTREE_FAR * coord, 
                               const void CNEARTREE_FAR * obj );
        
    /*
     =======================================================================
     int CNearTreeCompleteDelayedInsert ( const CNearTreeHandle treehandle )
     
     Function to dequeue the "points" queued as an objects for future insertion
     into a CNearTree for later searching
     
     return 0 for success, nonzero for an error
     
     =======================================================================
     */
    
    int CNearTreeCompleteDelayedInsert( const CNearTreeHandle treehandle );
    
    /*
     =======================================================================
     int CNearTreeZeroIfEmpty (const CNearTreeHandle treehandle)
     
     Test for an empty CNearTree, returning 0 in that case
     
     =======================================================================
     */
    
    int CNearTreeZeroIfEmpty (const CNearTreeHandle treehandle);
    
    /*
     =======================================================================
     int CNearTreeNearestNeighbor ( const CNearTreeHandle treehandle, 
     const double dRadius,  
     void CNEARTREE_FAR *  CNEARTREE_FAR * coordClosest,
     void CNEARTREE_FAR * CNEARTREE_FAR * objClosest,   
     const void CNEARTREE_FAR * coord )
     
     Function to search a Neartree for the object closest to some probe point, coord. This function
     is only here so that the function CNearTreeNearest can be called without having dRadius const
     
     dRadius is the maximum search radius - any point farther than dRadius from the probe
     point will be ignored
     
     coordClosest is the coordinate vector into which the coordinates of the nearest point
     will be stored
     
     objClosest is the address into which a pointer to the object associated with coordClosest
     will be stored
     
     coord  is the probe point
     
     the return value is true only if a point was found
     
     this version searches down the shortest branch first
     
     =======================================================================
 
     =======================================================================
     int CNearTreeLeftNearestNeighbor ( const CNearTreeHandle treehandle, 
     const double dRadius,  
     void CNEARTREE_FAR *  CNEARTREE_FAR * coordClosest,
     void CNEARTREE_FAR * CNEARTREE_FAR * objClosest,   
     const void CNEARTREE_FAR * coord )
     
     Function to search a Neartree for the object closest to some probe point, coord. This function
     is only here so that the function CNearTreeNearest can be called without having dRadius const
     
     dRadius is the maximum search radius - any point farther than dRadius from the probe
     point will be ignored
     
     coordClosest is the coordinate vector into which the coordinates of the nearest point
     will be stored
     
     objClosest is the address into which a pointer to the object associated with coordClosest
     will be stored
     
     coord  is the probe point
     
     the return value is true only if a point was found
     
     this version searches down the left branch first
     
     =======================================================================
     */
    
    int CNearTreeNearestNeighbor (const CNearTreeHandle treehandle, 
                                  const double dRadius,  
                                  void CNEARTREE_FAR * CNEARTREE_FAR * coordClosest,
                                  void CNEARTREE_FAR * CNEARTREE_FAR * objClosest, 
                                  const void CNEARTREE_FAR * coord );
    int CNearTreeLeftNearestNeighbor (const CNearTreeHandle treehandle, 
                                  const double dRadius,  
                                  void CNEARTREE_FAR * CNEARTREE_FAR * coordClosest,
                                  void CNEARTREE_FAR * CNEARTREE_FAR * objClosest, 
                                  const void CNEARTREE_FAR * coord );
    /*
     =======================================================================
     int CNearTreeFarthestNeighbor ( const CNearTreeHandle treehandle, 
     void CNEARTREE_FAR *  CNEARTREE_FAR * coordFarthest,
     void CNEARTREE_FAR * CNEARTREE_FAR * objFarthest,   
     const void CNEARTREE_FAR * coord )
     
     Function to search a Neartree for the object farthest some probe point, coord.
     
     coordClosest is the coordinate vector into which the coordinates of the nearest point
     will be stored
     
     objClosest is the address into which a pointer to the object associated with coordClosest
     will be stored
     
     coord  is the probe point
     
     the return value is true only if a point was found
     
     =======================================================================
     */
    
    int CNearTreeFarthestNeighbor (const CNearTreeHandle treehandle, 
                                   void CNEARTREE_FAR * CNEARTREE_FAR * coordFarthest,
                                   void CNEARTREE_FAR * CNEARTREE_FAR * objFarthest,   
                                   const void CNEARTREE_FAR * coord );
    /*
     =======================================================================
     int CNearTreeFindInSphere ( const CNearTreeHandle treehandle, 
     const double CNEARTREE_FAR * dRadius,
     CVectorHandle coordClosest,
     CVectorHandle objClosest,
     const void CNEARTREE_FAR * coord,
     int resetcount)
     
     Function to search a Neartree for the set of objects closer to some probe point, coord,
     than dRadius. This is only here so that objClosest can be cleared before starting the work.
     
     dRadius is the maximum search radius - any point farther than dRadius from the probe
     point will be ignored
     
     coordClosest is a vector of pointers to coordinate tuples and is the 
     returned set of nearest points to the probe point that can be found in the Neartree
     
     objClosest is a vector of objects and is the returned set of nearest points
     to the probe point that can be found in the Neartree
     
     coord  is the probe point
     
     resetcount should be non-zero to clear objClosest on entry
     
     return value is 0 if points were found
     
     =======================================================================
     */
    
    int CNearTreeFindInSphere ( const CNearTreeHandle treehandle,
                               const double dRadius,
                               CVectorHandle coordClosest,
                               CVectorHandle objClosest,
                               const void * coord,
                               int resetcount);
    
    /*
     =======================================================================
     int CNearTreeFindTreeInSphere ( const CNearTreeHandle treehandle, 
     const double CNEARTREE_FAR * dRadius,
     CNearTreeHandle foundClosest,
     const void CNEARTREE_FAR * coord,
     int resetcount)
     
     Function to search a Neartree for the set of objects closer to some probe point, coord,
     than dRadius.
     
     dRadius is the maximum search radius - any point farther than dRadius from the probe
     point will be ignored
     foundClosest is an existing CNearTree to which the found points will be added
     coord  is the probe point
     resetcount should be non-zero to clear found closest on entry
     return value is 0 if points were found
     
     =======================================================================
     */
    int CNearTreeFindTreeInSphere ( const CNearTreeHandle treehandle,
                                   const double dRadius,
                                   CNearTreeHandle foundClosest,
                                   const void CNEARTREE_FAR * coord,
                                   int resetcount);
    
    /*
     =======================================================================
     int CNearTreeFindOutSphere ( const CNearTreeHandle treehandle, 
     const double CNEARTREE_FAR * dRadius,
     CVectorHandle coordOutside,
     CVectorHandle objOutside,
     const void CNEARTREE_FAR * coord,
     int resetcount)
     
     Function to search a Neartree for the set of objects further from some probe point, coord,
     than dRadius. 
     
     dRadius is the maximum search radius - any point closer than dRadius from the probe
     point will be ignored
     
     coordOutside is a vector of pointers to coordinate tuples and is the 
     returned set of distant points from the probe point that can be found in the Neartree
     
     objOutside is a vector of objects and is the returned set of distant points
     from the probe point that can be found in the Neartree
     
     coord  is the probe point
     
     resetcount should be non-zero to clear objClosest on entry
     
     return value is 0 if points were found
     
     =======================================================================
     */
    
    int CNearTreeFindOutSphere ( const CNearTreeHandle treehandle,
                                const double dRadius,
                                CVectorHandle coordOutside,
                                CVectorHandle objOutside,
                                const void * coord,
                                int resetcount);
    
    /*
     =======================================================================
     int CNearTreeFindTreeOutSphere ( const CNearTreeHandle treehandle, 
     const double dRadius,
     CNearTreeHandle foundOutside,
     const void CNEARTREE_FAR * coord,
     int resetcount)
     
     Function to search a Neartree for the set of objects further from some probe point, coord,
     than dRadius.
     
     dRadius is the maximum search radius - any point closer than dRadius from the probe
     point will be ignored
     foundOutside is an existing CNearTree to which the found points will be added
     coord  is the probe point
     resetcount should be non-zero to clear found closest on entry
     return value is 0 if points were found
     
     =======================================================================
     */
    int CNearTreeFindTreeOutSphere ( const CNearTreeHandle treehandle,
                                    const double dRadius,
                                    CNearTreeHandle foundOutside,
                                    const void CNEARTREE_FAR * coord,
                                    int resetcount);
    
    /*
     =======================================================================
     int CNearTreeFindInAnnulus ( const CNearTreeHandle treehandle, 
     const double dRadiusInner,
     const double dRadiusOuter,
     CVectorHandle coordInRing,
     CVectorHandle objInRing,
     const void CNEARTREE_FAR * coord,
     int resetcount)
     
     Function to search a Neartree for the set of objects closer to some probe point, coord,
     than dRadiusOuter and further than dRadiusInner. 
     
     dRadiusInner is the minimum search radius - any point closer than dRadius from the probe
     point will be ignored
     dRadiusOuter is the maximum search radius - any point further than dRadius from the probe
     point will be ignored
     coordInRing is a vector of pointers to coordinate tuples of nearest points
     objInRing is a vector of objects and is the returned set of nearest points
     to the probe point that can be found in the Neartree
     coord  is the probe point
     resetcount should be non-zero to clear coordInRing and objInRing on entry
     return value is 0 if points were found
     
     =======================================================================
     */
    int CNearTreeFindInAnnulus ( const CNearTreeHandle treehandle,
                                const double dRadiusInner,
                                const double dRadiusOuter,
                                CVectorHandle coordInRing,
                                CVectorHandle objInRing,
                                const void CNEARTREE_FAR * coord,
                                int resetcount);
    /*
     =======================================================================
     int CNearTreeFindTreeInAnnulus ( const CNearTreeHandle treehandle, 
     const double dRadiusInner,
     const double dRadiusOuter,
     CNearTreeHandle foundInRing,
     const void CNEARTREE_FAR * coord,
     int resetcount)
     
     Function to search a Neartree for the set of objects closer to some probe point, coord,
     than dRadiusOuter and further than dRadiusInner. 
     
     dRadiusInner is the minimum search radius - any point closer than dRadius from the probe
     point will be ignored
     dRadiusOuter is the maximum search radius - any point further than dRadius from the probe
     point will be ignored
     foundInRing is an existing CNearTree to which the found points will be added
     coord  is the probe point
     resetcount should be non-zero to clear found InRing on entry
     return value is 0 if points were found
     
     =======================================================================
     */
    int CNearTreeFindTreeInAnnulus ( const CNearTreeHandle treehandle,
                                    const double dRadiusInner,
                                    const double dRadiusOuter,
                                    CNearTreeHandle foundInRing,
                                    const void CNEARTREE_FAR * coord,
                                    int resetcount);
    
    
    /*
     =======================================================================

     int CNearTreeSortIn(CVectorHandle metrics, 
     CVectorHandle indices, 
     double metric, 
     size_t index, 
     size_t k);
     
     CNearTreeSortIn inserts a new metric and index into the vectors
     metrics and indices, sorted on non-decreasing metric,
     with the size of the vectors capped at k, or uncapped if k = 0;

     =======================================================================
     */

    int CNearTreeSortIn(CVectorHandle metrics,
                        CVectorHandle indices,
                        double metric,
                        size_t index,
                        size_t k);
    /*
     =======================================================================
     
     int CNearTreeSortIn2(CVectorHandle metrics,
     CVectorHandle indices,
     double metric,
     size_t index,
     size_t k,
     int shell,
     size_t sSizeCur,
     double dRadiusCur);
     
     CNearTreeSortIn2 inserts a new metric and index into the vectors
     metrics and indices, sorted on non-decreasing metric,
     with the size of the vectors capped at k, or uncapped if k = 0;
     
     =======================================================================
     */
    
    int CNearTreeSortIn2(CVectorHandle metrics,
                         CVectorHandle indices,
                         double metric,
                         size_t index,
                         size_t k,
                         int shell,
                         size_t sSizeCur,
                         double dRadiusCur);
    
    /*
     =======================================================================
     int CNearTreeFindKNearest ( const CNearTreeHandle treehandle,
     const size_t k,
     const double dRadius,
     CVectorHandle coordClosest,
     CVectorHandle objClosest,
     const void * coord,
     int resetcount);
     
     Function to search a Neartree for the set of objects closer to some probe point, coord,
     than dRadius.
     
     k is the maximum number of closest neighbors to return.  Finds this many if passible.
     
     dRadius is the maximum search radius - any point further than dRadius from the probe
     point will be ignored
     
     coordClosest is a vector of pointers to coordinate tuples and is the
     returned set of farthest points from the probe point that can be found in the Neartree
     
     objClosest is a vector of objects and is the returned set of farthest points
     from the probe point that can be found in the Neartree
     
     coord  is the probe point
     
     resetcount should be non-zero to clear objClosest on entry
     
     return value is 0 if points were found
     
     =======================================================================
     */
    
    int CNearTreeFindKNearest (const CNearTreeHandle treehandle,
                               const size_t k,
                               const double dRadius,
                               CVectorHandle coordClosest,
                               CVectorHandle objClosest,
                               const void * coord,
                               int resetcount);
   
    int CNearTreeFindKNearest_Annular (const CNearTreeHandle treehandle,
                                       const size_t k,
                                       const double dRadius,
                                       CVectorHandle distances,
                                       CVectorHandle indices,
                                       const void * coord);

    int CNearTreeFindKNearest_Sphere (const CNearTreeHandle treehandle,
                                      const size_t k,
                                      const double dRadius,
                                      CVectorHandle distances,
                                      CVectorHandle indices,
                                      const void * coord);

    /*
     =======================================================================
     int CNearTreeFindKNearInAnnulus ( const CNearTreeHandle treehandle,
     const size_t k,
     const int shell,
     const int closed,
     const double dRadiusInner,
     double dRadiusOuter,
     CVectorHandle distInRing,
     CVectorHandle indexInRing,
     const void * coord);
     
     Function to search a Neartree for the set of objects closer to some probe point, coord,
     than dRadius.
     
     k is the maximum number of closest neighbors to return.  Finds this many if passible.
     
     shell if non-zero, the search only returns hits in the nearest thin shell
     
     closed if non-zero, point at dRadiusInner will be included
     
     dRadiusInner is the minimum radius - any point closer than dRadiusInner from the probe point will be ignored, if closed is zero, any point at dRadiusInner will
     also be ignored
     
     dRadiusOuter is the maximum search radius - any point further than dRadius from the probe point will be ignored,
     
     distInRing is an existing vector of the distances to the k closest points
     to the probe point in the annulus that can be found in the Neartree.  The caller
     should have created this vector with CVectorCreate(&distanceInRing,sizeof(double),k)
     
     indexInRing is an existing vector of indices of the coordinates and objects of
     k closest points from the probe point in the annulus
     that can be found in the Neartree
     
     coord  is the probe point
     
     return value is 0 if points were found
     
     =======================================================================
     */
    
    int CNearTreeFindKNearInAnnulus (const CNearTreeHandle treehandle,
                                     const size_t k,
                                     const int shell,
                                     const int closed,
                                     const double dRadiusInner,
                                     double dRadiusOuter,
                                     CVectorHandle distInRing,
                                     CVectorHandle indexInRing,
                                     const void * coord);
    
    
    /*
     =======================================================================
     
     int CNearTreeNearest ( const CNearTreeHandle treehandle, 
     double CNEARTREE_FAR *dRadius,  
     void CNEARTREE_FAR * CNEARTREE_FAR * coordClosest,
     void CNEARTREE_FAR * CNEARTREE_FAR * objClosest,
     const void CNEARTREE_FAR * coord )
     
     Function to search a Neartree for the object closest to some probe point, coord.
     This function is called by CNearTreeNearestNeighbor.
     
     *dRadius is the smallest currently known distance of an object from the probe point
     and is modified as the search progresses.
     
     coordClosest is the returned closest point
     to the probe point that can be found in the Neartree
     
     objClosest is a pointer to a pointer to hold the corresponding object or is NULL
     
     coord  is the probe point
     
     the return value is 0 only if a point was found within dRadius
     
     this version search down the shortest branch first
     
     =======================================================================
     =======================================================================
     
     int CNearTreeLeftNearest ( const CNearTreeHandle treehandle, 
     double CNEARTREE_FAR *dRadius,  
     void CNEARTREE_FAR * CNEARTREE_FAR * coordClosest,
     void CNEARTREE_FAR * CNEARTREE_FAR * objClosest,
     const void CNEARTREE_FAR * coord )
     
     Function to search a Neartree for the object closest to some probe point, coord.
     This function is called by CNearTreeNearestNeighbor.
     
     *dRadius is the smallest currently known distance of an object from the probe point
     and is modified as the search progresses.
     
     coordClosest is the returned closest point
     to the probe point that can be found in the Neartree
     
     objClosest is a pointer to a pointer to hold the corresponding object or is NULL
     
     coord  is the probe point
     
     the return value is 0 only if a point was found within dRadius
     
     this version searches down the left branch first
     
     =======================================================================
     
     */
    int CNearTreeNearest ( const CNearTreeHandle treehandle, 
                          double CNEARTREE_FAR * dRadius,  
                          void CNEARTREE_FAR * CNEARTREE_FAR * coordClosest,
                          void CNEARTREE_FAR * CNEARTREE_FAR * objClosest,
                          const void CNEARTREE_FAR * coord );

    int CNearTreeLeftNearest ( const CNearTreeHandle treehandle, 
                          double CNEARTREE_FAR * dRadius,  
                          void CNEARTREE_FAR * CNEARTREE_FAR * coordClosest,
                          void CNEARTREE_FAR * CNEARTREE_FAR * objClosest,
                          const void CNEARTREE_FAR * coord );
    /*
     =======================================================================
     int CNearTreeFindFarthest ( const CNearTreeHandle treehandle,
     double CNEARTREE_FAR * dRadius,  
     void CNEARTREE_FAR * CNEARTREE_FAR * coordFarthest,
     void CNEARTREE_FAR * CNEARTREE_FAR * objFarthest,
     const void CNEARTREE_FAR * coord )
     
     Function to search a Neartree for the object farthest from some probe point, coord.
     This function is called by CNearTreeFarthestNeighbor.
     
     *dRadius is the largest currently known distance of an object from the probe point
     and is modified as the search progresses.
     
     coordFarthest is the returned farthest point
     from the probe point that can be found in the Neartree
     
     objFarthest is a pointer to a pointer to hold the corresponding object or is NULL
     coord  is the probe point
     
     the return value is 0 only if a point was found.
     
     =======================================================================
     */
    
    int CNearTreeFindFarthest ( const CNearTreeHandle treehandle,
                               double CNEARTREE_FAR * dRadius,  
                               void CNEARTREE_FAR * CNEARTREE_FAR * coordFarthest,
                               void CNEARTREE_FAR * CNEARTREE_FAR * objFarthest,
                               const void CNEARTREE_FAR * coord );
    
    
    /*
     =======================================================================
     int CNearTreeFindKNearest ( const CNearTreeHandle treehandle,
     const size_t k,
     const double dRadius,
     CVectorHandle coordClosest,
     CVectorHandle objClosest,
     const void * coord,
     int resetcount);
     
     Function to search a Neartree for the set of objects closer to some probe point, coord,
     than dRadius. 
     
     k is the maximum number of closest neighbors to return.  Finds this many if passible.
     
     dRadius is the maximum search radius - any point closer than dRadius to the probe
     point will be ignored
     
     coordClosest is a vector of pointers to coordinate tuples and is the 
     returned set of farthest points from the probe point that can be found in the Neartree
     
     objClosest is a vector of objects and is the returned set of farthest points
     from the probe point that can be found in the Neartree
     
     coord  is the probe point
     
     resetcount should be non-zero to clear objClosest on entry
     
     return value is 0 if points were found
     
     =======================================================================
     */
    
    int CNearTreeFindKNearest ( const CNearTreeHandle treehandle,
                               const size_t k,
                               const double dRadius,
                               CVectorHandle coordClosest,
                               CVectorHandle objClosest,
                               const void * coord,
                               int resetcount);
    
    /*
     =======================================================================
     int CNearTreeFindKTreeNearest ( const CNearTreeHandle treehandle,
     const size_t k,
     const double dRadius,
     CNearTreeHandle foundNearest,
     const void CNEARTREE_FAR * coord,
     int resetcount);
     
     Function to search a Neartree for the set of k objects closer to some probe point, coord,
     than dRadius.
     
     k is the maximum number of closest neighbors to return.  Finds this many if passible.
     dRadius is the minimum search radius - any point closer than dRadius to the probe
     point will be ignored
     foundClosest is an existing CNearTree to which the found points will be added
     coord  is the probe point
     resetcount should be non-zero to clear foundClosest on entry
     return value is 0 if points were found
     
     =======================================================================
     */
    int CNearTreeFindKTreeNearest ( const CNearTreeHandle treehandle,
                                   const size_t k,
                                   const double dRadius,
                                   CNearTreeHandle foundClosest,
                                   const void CNEARTREE_FAR * coord,
                                   int resetcount);
    
    
    /*
     =======================================================================
     int CNearTreeFindKFarthest ( const CNearTreeHandle treehandle,
     const size_t k,
     const double dRadius,
     CVectorHandle coordFarthest,
     CVectorHandle objFarthest,
     const void * coord,
     int resetcount);
     
     Function to search a Neartree for the set of objects farther from some probe point, coord,
     than dRadius. 
     
     k is the maximum number of farthest neighbors to return.  Finds this many if passible.
     
     dRadius is the maximum search radius - any point farther than dRadius from the probe
     point will be ignored
     
     coordFarthest is a vector of pointers to coordinate tuples and is the 
     returned set of farthest points from the probe point that can be found in the Neartree
     
     objFarthest is a vector of objects and is the returned set of farthest points
     from the probe point that can be found in the Neartree
     
     coord  is the probe point
     
     resetcount should be non-zero to clear objFarthest on entry
     
     return value is 0 if points were found
     
     =======================================================================
     */
    
    int CNearTreeFindKFarthest ( const CNearTreeHandle treehandle,
                                const size_t k,
                                const double dRadius,
                                CVectorHandle coordFarthest,
                                CVectorHandle objFarthest,
                                const void * coord,
                                int resetcount);
    
    /*
     =======================================================================
     int CNearTreeFindKTreeFarthest ( const CNearTreeHandle treehandle,
     const size_t k,
     const double dRadius,
     CNearTreeHandle foundFarthest,
     const void CNEARTREE_FAR * coord,
     int resetcount);
     
     Function to search a Neartree for the set of k objects farther from some probe point, coord,
     than dRadius.
     
     k is the maximum number of farthest neighbors to return.  Finds this many if passible.
     dRadius is the minimum search radius - any point farther than dRadius from the probe
     point will be ignored
     foundFarthest is an existing CNearTree to which the found points will be added
     coord  is the probe point
     resetcount should be non-zero to clear foundFarthest on entry
     return value is 0 if points were found
     
     =======================================================================
     */
    int CNearTreeFindKTreeFarthest ( const CNearTreeHandle treehandle,
                                    const size_t k,
                                    const double dRadius,
                                    CNearTreeHandle foundFarthest,
                                    const void CNEARTREE_FAR * coord,
                                    int resetcount);
    
    
    
    
    /*
     =======================================================================
     The following macro is provided here to ensure operation with older
     versions of CVector
     =======================================================================
     */
    
#ifndef CVectorElementAt
    /* CVectorElementAt -- return the element at the given index as a void pointer without checking
     and without protection against relocation */
    
#define CVectorElementAt(vectorhandle,index) ((void CNEARTREE_FAR *)(((char *)((vectorhandle)->array))+(index)*(vectorhandle)->elementsize))
#endif
    
    
    /*
     =======================================================================
     int CNearTreeObjects ( const CNearTreeHandle treehandle, CVectorHandle CNEARTREE_FAR * vectorhandle)
     
     Function to return the vector of objects in the tree.  This vector
     is not guaranteed to be in the same order as the order of insertion
     
     vectorhandle -- a pointer to a CVectorHandle
     
     =======================================================================
     */
    
    int CNearTreeObjects ( const CNearTreeHandle treehandle, CVectorHandle CNEARTREE_FAR * vectorhandle);    
    
    /*
     =======================================================================
     int CNearTreeCoords ( const CNearTreeHandle treehandle, CVectorHandle CNEARTREE_FAR * vectorhandle)
     
     Function to return the vector of coordinates in the tree.  This vector
     is not guaranteed to be in the same order as the order of insertion
     
     vectorhandle -- a pointer to a CVectorHandle
     
     =======================================================================
     */
    
    int CNearTreeCoords ( const CNearTreeHandle treehandle, CVectorHandle CNEARTREE_FAR * vectorhandle);    
    
    /*
     =======================================================================
     void CNEARTREE_FAR * CNearTreeObjectAt ( const CNearTreeHandle treehandle, size_t index)
     
     Function to return the an object pointer at the given index
     
     implemented as a macro
     
     =======================================================================
     */
    
#define CNearTreeObjectAt(treehandle,index) CVectorElementAt(treehandle->m_ObjectStore,index)
    
    /*
     =======================================================================
     void CNEARTREE_FAR * CNearTreeCoordAt ( const CNearTreeHandle treehandle, size_t index)
     
     Function to return the a coordinate pointer at the given index
     
     implemented as a macro
     
     =======================================================================
     */
    
#define CNearTreeCoordAt(treehandle,index) CVectorElementAt(treehandle->m_CoordStore,index)
    
    
    
#ifdef __cplusplus
    
}

#endif


#endif