# Source Code Examples

For complete examples, please refer to the source code of sample commands contained in the directories ``c++-samples`' and ``c-samples`'.

- Running nearest neighbor search.
- Running range search.

## Running nearest neighbor search.

The following is an example of using the function ``getNeighbors()`'.

>     (C++)
>             HnSRTreeFile file;
>             HnPoint queryPoint;
>             int i, numNeighbors;
>             HnPointVector points;
>             HnDataItemVector dataItems;

>             . . .

>             /* open an index file and prepare `queryPoint' and `numNeighbors' */

>             . . .

>             file.getNeighbors(queryPoint, numNeighbors, &points, &dataItems);

>             for ( i=0; i<points.size(); i++ ) {
>                 HnPoint point = points.elementAt(i);
>                 HnDataItem dataItem = dataItems.elementAt(i);

>                 . . .

>                 /* work with `point' and `dataItem' */

>                 . . .

>             }

>             /* close an index file */

>     (C  )
>             HnSRTreeFileSt *file;
>             HnPointSt *queryPoint;
>             int i, numNeighbors;
>             HnPointVectorSt *points;
>             HnDataItemVectorSt *dataItems;

>             . . .

>             /* open an index file and prepare `queryPoint' and `numNeighbors' */

>             . . .

>             HnSRTreeFileSt_getNeighbors(file, queryPoint, numNeighbors,
>                                         &points, &dataItems);

>             for ( i=0; i<points->size; i++ ) {
>                 HnPointSt *point = points->elements[i];
>                 HnDataItemSt *dataItem = dataItems->elements[i];

>                 . . .

>                 /* work with `point' and `dataItem' */

>                 . . .

>             }

>             HnPointVectorSt_freeElements(points);
>             HnPointVectorSt_free(points);

>             HnDataItemVectorSt_freeElements(dataItems);
>             HnDataItemVectorSt_free(dataItems);

>             /* close an index file and delete `queryPoint' */

> See also HnPoint, HnDataItem, HnPointVector, HnDataItemVector.

## Running range search.

The following is an example of using the function ``getFirst()`' and ``getNext()`'.

>     (C++)
>             HnSRTreeFile file;
>             HnRect queryRect;
>             HnPoint point;
>             HnDataItem dataItem;

>             . . .

>             /* open an index file and prepare `queryRect' */

>             . . .

>             file.getFirst(queryRect, &point, &dataItem);

>             while ( point != HnPoint::null ) {

>                 . . .

>                 /* work with `point' and `dataItem' */

>                 . . .

>                 file.getNext(&point, &dataItem);
>             }

>             /* close an index file */

>     (C  )
>             HnSRTreeFileSt *file;
>             HnRectSt *queryRect;
>             HnPointSt *point;
>             HnDataItemSt *dataItem;

>             . . .

>             /* open an index file and prepare `queryRect' */

>             . . .

>             HnSRTreeFileSt_getFirstInRect(file, queryRect, &point, &dataItem);

>             while ( point != NULL ) {

>                 . . .

>                 /* work with `point' and `dataItem' */

>                 . . .

>                 HnPointSt_free(point);
>                 HnDataItemSt_free(dataItem);

>                 HnSRTreeFileSt_getNext(file, &point, &dataItem);
>             }

>             /* close an index file and delete `queryRect' */

> See also HnRect, HnPoint, HnDataItem.

[TOC] [Library] [Classes] [Commands] [Examples] [References]

Any feedback is appreciated (corrections, suggestions, etc.).