/***************************************
- $Header: /home/amb/routino/src/RCS/nodesx.c,v 1.56 2010/04/28 17:27:02 amb Exp $
+ $Header: /home/amb/routino/src/RCS/nodesx.c,v 1.75 2010/10/09 14:14:42 amb Exp $
Extented Node data type functions.
#include <sys/stat.h>
#include "types.h"
-#include "functions.h"
+#include "nodes.h"
+#include "segments.h"
+
#include "nodesx.h"
#include "segmentsx.h"
#include "waysx.h"
-#include "segments.h"
-#include "nodes.h"
+#include "types.h"
+
+#include "files.h"
+#include "functions.h"
-/* Variables */
-/*+ The command line '--slim' option. +*/
-extern int option_slim;
+/* Variables */
/*+ The command line '--tmpdir' option or its default value. +*/
extern char *option_tmpdirname;
nodesx->filename=(char*)malloc(strlen(option_tmpdirname)+32);
if(append)
- sprintf(nodesx->filename,"%s/nodes.input.tmp",option_tmpdirname);
+ sprintf(nodesx->filename,"%s/nodesx.input.tmp",option_tmpdirname);
else
- sprintf(nodesx->filename,"%s/nodes.%p.tmp",option_tmpdirname,nodesx);
+ sprintf(nodesx->filename,"%s/nodesx.%p.tmp",option_tmpdirname,nodesx);
+
+#if SLIM
+ nodesx->nfilename=(char*)malloc(strlen(option_tmpdirname)+32);
+
+ sprintf(nodesx->nfilename,"%s/nodes.%p.tmp",option_tmpdirname,nodesx);
+#endif
if(append)
{
off_t size;
- nodesx->fd=AppendFile(nodesx->filename);
+ nodesx->fd=OpenFileAppend(nodesx->filename);
size=SizeFile(nodesx->filename);
nodesx->xnumber=size/sizeof(NodeX);
}
else
- nodesx->fd=OpenFile(nodesx->filename);
+ nodesx->fd=OpenFileNew(nodesx->filename);
return(nodesx);
}
if(nodesx->idata)
free(nodesx->idata);
+#if !SLIM
if(nodesx->ndata)
free(nodesx->ndata);
+#endif
+
+#if SLIM
+ DeleteFile(nodesx->nfilename);
+
+ free(nodesx->nfilename);
+#endif
if(nodesx->super)
free(nodesx->super);
/*++++++++++++++++++++++++++++++++++++++
- Append a node to a newly created node list (unsorted).
+ Append a single node to an unsorted node list.
NodesX* nodesx The set of nodes to process.
double latitude The latitude of the node.
double longitude The longitude of the node.
+
+ allow_t allow The allowed traffic types through the node.
++++++++++++++++++++++++++++++++++++++*/
-void AppendNode(NodesX* nodesx,node_t id,double latitude,double longitude)
+void AppendNode(NodesX* nodesx,node_t id,double latitude,double longitude,allow_t allow)
{
NodeX nodex;
- assert(!nodesx->idata); /* Must not have idata filled in => unsorted */
-
nodex.id=id;
nodex.latitude =radians_to_latlong(latitude);
nodex.longitude=radians_to_latlong(longitude);
+ nodex.allow=allow;
WriteFile(nodesx->fd,&nodex,sizeof(NodeX));
nodesx->xnumber++;
+
+ assert(nodesx->xnumber<NODE_FAKE); /* NODE_FAKE marks the high-water mark for real nodes. */
}
{
int fd;
- /* Check the start conditions */
-
- assert(!nodesx->idata); /* Must not have idata filled in => unsorted */
-
/* Print the start message */
printf("Sorting Nodes");
DeleteFile(nodesx->filename);
- fd=OpenFile(nodesx->filename);
+ fd=OpenFileNew(nodesx->filename);
/* Allocate the array of indexes */
DeleteFile(nodesx->filename);
- fd=OpenFile(nodesx->filename);
+ fd=OpenFileNew(nodesx->filename);
/* Sort geographically */
else
{
#ifdef REGRESSION_TESTING
- // Need this for regression testing because heapsort() is not order
+ // Need this for regression testing because filesort_heapsort() is not order
// preserving like qsort() is (or was when tested).
index_t a_id=a->id;
int end=nodesx->number-1;
int mid;
- assert(nodesx->idata); /* Must have idata filled in => sorted by id */
-
/* Binary search - search key exact match only is required.
*
* # <- start | Check mid and move start or end if it doesn't match
/*++++++++++++++++++++++++++++++++++++++
- Lookup a particular node.
-
- NodeX *LookupNodeX Returns a pointer to the extended node with the specified id.
-
- NodesX* nodesx The set of nodes to process.
-
- index_t index The node index to look for.
-
- int position The position in the cache to use.
- ++++++++++++++++++++++++++++++++++++++*/
-
-NodeX *LookupNodeX(NodesX* nodesx,index_t index,int position)
-{
- assert(index!=NO_NODE); /* Must be a valid node */
-
- if(option_slim)
- {
- SeekFile(nodesx->fd,index*sizeof(NodeX));
-
- ReadFile(nodesx->fd,&nodesx->cached[position-1],sizeof(NodeX));
-
- return(&nodesx->cached[position-1]);
- }
- else
- {
- return(&nodesx->xdata[index]);
- }
-}
-
-
-/*++++++++++++++++++++++++++++++++++++++
Remove any nodes that are not part of a highway.
NodesX *nodesx The complete node list.
latlong_t lat_min,lat_max,lon_min,lon_max;
int fd;
- /* Check the start conditions */
-
- assert(nodesx->idata); /* Must have idata filled in => data sorted */
-
/* Print the start message */
printf("Checking: Nodes=0");
DeleteFile(nodesx->filename);
- fd=OpenFile(nodesx->filename);
+ fd=OpenFileNew(nodesx->filename);
SeekFile(nodesx->fd,0);
while(!ReadFile(nodesx->fd,&nodex,sizeof(NodeX)))
{
index_t i;
- /* Check the start conditions */
-
- assert(!nodesx->ndata); /* Must not have ndata filled in => no real nodes */
-
/* Print the start message */
printf("Creating Real Nodes: Nodes=0");
/* Map into memory */
- if(!option_slim)
- nodesx->xdata=MapFile(nodesx->filename);
+#if !SLIM
+ nodesx->xdata=MapFile(nodesx->filename);
+#endif
- /* Allocate the memory */
+ /* Allocate the memory (or open the file) */
+#if !SLIM
nodesx->ndata=(Node*)malloc(nodesx->number*sizeof(Node));
assert(nodesx->ndata); /* Check malloc() worked */
+#else
+ nodesx->nfd=OpenFileNew(nodesx->nfilename);
+#endif
/* Loop through and allocate. */
for(i=0;i<nodesx->number;i++)
{
NodeX *nodex=LookupNodeX(nodesx,i,1);
+ Node *node =LookupNodeXNode(nodesx,nodex->id,1);
- nodesx->ndata[nodex->id].latoffset=latlong_to_off(nodex->latitude);
- nodesx->ndata[nodex->id].lonoffset=latlong_to_off(nodex->longitude);
- nodesx->ndata[nodex->id].firstseg=SEGMENT(NO_SEGMENT);
+ node->latoffset=latlong_to_off(nodex->latitude);
+ node->lonoffset=latlong_to_off(nodex->longitude);
+ node->firstseg=NO_SEGMENT;
+ node->allow=nodex->allow;
+ node->flags=0;
if(nodesx->super[nodex->id]==iteration)
- nodesx->ndata[nodex->id].firstseg|=NODE_SUPER;
+ node->flags|=NODE_SUPER;
+
+#if SLIM
+ PutBackNodeXNode(nodesx,nodex->id,1);
+#endif
if(!((i+1)%10000))
{
/* Unmap from memory */
- if(!option_slim)
- nodesx->xdata=UnmapFile(nodesx->filename);
+#if !SLIM
+ nodesx->xdata=UnmapFile(nodesx->filename);
+#endif
/* Print the final message */
{
index_t i;
- /* Check the start conditions */
-
- assert(nodesx->ndata); /* Must have ndata filled in => real nodes exist */
- assert(segmentsx->sdata); /* Must have sdata filled in => real segments exist */
+ if(nodesx->number==0 || segmentsx->number==0)
+ return;
/* Print the start message */
/* Map into memory */
- if(!option_slim)
- {
- nodesx->xdata=MapFile(nodesx->filename);
- segmentsx->xdata=MapFile(segmentsx->filename);
- }
+#if !SLIM
+ nodesx->xdata=MapFile(nodesx->filename);
+ segmentsx->xdata=MapFile(segmentsx->filename);
+#endif
/* Index the nodes */
SegmentX *segmentx=LookupSegmentX(segmentsx,i,1);
node_t id1=segmentx->node1;
node_t id2=segmentx->node2;
- Node *node1=&nodesx->ndata[id1];
- Node *node2=&nodesx->ndata[id2];
+ Node *node1=LookupNodeXNode(nodesx,id1,1);
+ Node *node2=LookupNodeXNode(nodesx,id2,2);
/* Check node1 */
- if(SEGMENT(node1->firstseg)==SEGMENT(NO_SEGMENT))
+ if(node1->firstseg==NO_SEGMENT)
{
- node1->firstseg^=SEGMENT(NO_SEGMENT);
- node1->firstseg|=i;
+ node1->firstseg=i;
+
+#if SLIM
+ PutBackNodeXNode(nodesx,id1,1);
+#endif
}
else
{
- index_t index=SEGMENT(node1->firstseg);
+ index_t index=node1->firstseg;
do
{
}
else
{
- if(segmentsx->sdata[index].next2==NO_NODE)
+ Segment *segment=LookupSegmentXSegment(segmentsx,index,1);
+
+ if(segment->next2==NO_NODE)
{
- segmentsx->sdata[index].next2=i;
+ segment->next2=i;
+
+#if SLIM
+ PutBackSegmentXSegment(segmentsx,index,1);
+#endif
+
break;
}
else
- index=segmentsx->sdata[index].next2;
+ index=segment->next2;
}
}
while(1);
/* Check node2 */
- if(SEGMENT(node2->firstseg)==SEGMENT(NO_SEGMENT))
+ if(node2->firstseg==NO_SEGMENT)
{
- node2->firstseg^=SEGMENT(NO_SEGMENT);
- node2->firstseg|=i;
+ node2->firstseg=i;
+
+#if SLIM
+ PutBackNodeXNode(nodesx,id2,2);
+#endif
}
else
{
- index_t index=SEGMENT(node2->firstseg);
+ index_t index=node2->firstseg;
do
{
}
else
{
- if(segmentsx->sdata[index].next2==NO_NODE)
+ Segment *segment=LookupSegmentXSegment(segmentsx,index,1);
+
+ if(segment->next2==NO_NODE)
{
- segmentsx->sdata[index].next2=i;
+ segment->next2=i;
+
+#if SLIM
+ PutBackSegmentXSegment(segmentsx,index,1);
+#endif
+
break;
}
else
- index=segmentsx->sdata[index].next2;
+ index=segment->next2;
}
}
while(1);
/* Unmap from memory */
- if(!option_slim)
- {
- nodesx->xdata=UnmapFile(nodesx->filename);
- segmentsx->xdata=UnmapFile(segmentsx->filename);
- }
+#if !SLIM
+ nodesx->xdata=UnmapFile(nodesx->filename);
+ segmentsx->xdata=UnmapFile(segmentsx->filename);
+#endif
/* Print the final message */
{
index_t i;
int fd;
- Nodes *nodes;
+ NodesFile nodesfile={0};
int super_number=0;
- /* Check the start conditions */
-
- assert(nodesx->ndata); /* Must have ndata filled in => real nodes exist */
-
/* Print the start message */
printf("Writing Nodes: Nodes=0");
/* Map into memory */
- if(!option_slim)
- nodesx->xdata=MapFile(nodesx->filename);
-
- /* Count the number of super-nodes */
-
- for(i=0;i<nodesx->number;i++)
- if(nodesx->ndata[i].firstseg&NODE_SUPER)
- super_number++;
-
- /* Fill in a Nodes structure with the offset of the real data in the file after
- the Node structure itself. */
-
- nodes=calloc(1,sizeof(Nodes));
-
- assert(nodes); /* Check calloc() worked */
-
- nodes->number=nodesx->number;
- nodes->snumber=super_number;
-
- nodes->latbins=nodesx->latbins;
- nodes->lonbins=nodesx->lonbins;
-
- nodes->latzero=nodesx->latzero;
- nodes->lonzero=nodesx->lonzero;
-
- nodes->data=NULL;
- nodes->offsets=NULL;
- nodes->nodes=NULL;
-
- /* Write out the Nodes structure and then the real data. */
+#if !SLIM
+ nodesx->xdata=MapFile(nodesx->filename);
+#endif
- fd=OpenFile(filename);
+ /* Write out the nodes data */
- WriteFile(fd,nodes,sizeof(Nodes));
+ fd=OpenFileNew(filename);
+ SeekFile(fd,sizeof(NodesFile));
WriteFile(fd,nodesx->offsets,(nodesx->latbins*nodesx->lonbins+1)*sizeof(index_t));
- for(i=0;i<nodes->number;i++)
+ for(i=0;i<nodesx->number;i++)
{
NodeX *nodex=LookupNodeX(nodesx,i,1);
- Node *node=&nodesx->ndata[nodex->id];
+ Node *node=LookupNodeXNode(nodesx,nodex->id,1);
+
+ if(node->flags&NODE_SUPER)
+ super_number++;
WriteFile(fd,node,sizeof(Node));
}
}
+ /* Write out the header structure */
+
+ nodesfile.number=nodesx->number;
+ nodesfile.snumber=super_number;
+
+ nodesfile.latbins=nodesx->latbins;
+ nodesfile.lonbins=nodesx->lonbins;
+
+ nodesfile.latzero=nodesx->latzero;
+ nodesfile.lonzero=nodesx->lonzero;
+
+ SeekFile(fd,0);
+ WriteFile(fd,&nodesfile,sizeof(NodesFile));
+
CloseFile(fd);
/* Unmap from memory */
- if(!option_slim)
- nodesx->xdata=UnmapFile(nodesx->filename);
+#if !SLIM
+ nodesx->xdata=UnmapFile(nodesx->filename);
+#endif
/* Print the final message */
- printf("\rWrote Nodes: Nodes=%d \n",nodes->number);
+ printf("\rWrote Nodes: Nodes=%d \n",nodesx->number);
fflush(stdout);
-
- /* Free the fake Nodes */
-
- free(nodes);
}