1 /***************************************
2  A header file for the extended nodes.
3 
4  Part of the Routino routing software.
5  ******************/ /******************
6  This file Copyright 2008-2015, 2019 Andrew M. Bishop
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Affero General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU Affero General Public License for more details.
17 
18  You should have received a copy of the GNU Affero General Public License
19  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  ***************************************/
21 
22 
23 #ifndef NODESX_H
24 #define NODESX_H    /*+ To stop multiple inclusions. +*/
25 
26 #include <stdint.h>
27 
28 #include "types.h"
29 #include "nodes.h"
30 
31 #include "typesx.h"
32 
33 #include "cache.h"
34 #include "files.h"
35 
36 
37 /* Data structures */
38 
39 
40 /*+ An extended structure used for processing. +*/
41 struct _NodeX
42 {
43  node_t       id;               /*+ The node identifier; initially the OSM value, later the Node index, finally the first segment. +*/
44 
45  latlong_t    latitude;         /*+ The node latitude. +*/
46  latlong_t    longitude;        /*+ The node longitude. +*/
47 
48  transports_t allow;            /*+ The types of transport that are allowed through the node. +*/
49 
50  nodeflags_t  flags;            /*+ Flags containing extra information (e.g. super-node, turn restriction). +*/
51 };
52 
53 /*+ A structure containing a set of nodes (memory format). +*/
54 struct _NodesX
55 {
56  char     *filename;            /*+ The name of the intermediate file (for the NodesX). +*/
57  char     *filename_tmp;        /*+ The name of the temporary file (for the NodesX). +*/
58 
59  int       fd;                  /*+ The file descriptor of the open file (for the NodesX). +*/
60 
61  index_t   number;              /*+ The number of extended nodes still being considered. +*/
62  index_t   knumber;             /*+ The number of extended nodes kept for next time. +*/
63 
64 #if !SLIM
65 
66  NodeX    *data;                /*+ The extended node data (when mapped into memory). +*/
67 
68 #else
69 
70  NodeX     cached[3];           /*+ Three cached extended nodes read from the file in slim mode. +*/
71  index_t   incache[3];          /*+ The indexes of the cached extended nodes. +*/
72 
73  NodeXCache *cache;             /*+ A RAM cache of extended nodes read from the file. +*/
74 
75 #endif
76 
77  char     *ifilename_tmp;       /*+ The name of the temporary file (for the NodesX ID index). +*/
78 
79  int       ifd;                 /*+ The file descriptor of the temporary file (for the NodesX ID index). +*/
80 
81  node_t   *idata;               /*+ The extended node IDs (sorted by ID). +*/
82 
83  index_t  *pdata;               /*+ The node indexes after pruning. +*/
84 
85  index_t  *gdata;               /*+ The final node indexes (sorted geographically). +*/
86 
87  BitMask  *super;               /*+ A bit-mask marker for super nodes (same order as sorted nodes). +*/
88 
89  index_t   latbins;             /*+ The number of bins containing latitude. +*/
90  index_t   lonbins;             /*+ The number of bins containing longitude. +*/
91 
92  ll_bin_t  latzero;             /*+ The bin number of the furthest south bin. +*/
93  ll_bin_t  lonzero;             /*+ The bin number of the furthest west bin. +*/
94 };
95 
96 
97 /* Functions in nodesx.c */
98 
99 NodesX *NewNodeList(int append,int readonly);
100 void FreeNodeList(NodesX *nodesx,int keep);
101 
102 void AppendNodeList(NodesX *nodesx,node_t id,double latitude,double longitude,transports_t allow,nodeflags_t flags);
103 void FinishNodeList(NodesX *nodesx);
104 
105 index_t IndexNodeX(NodesX *nodesx,node_t id);
106 
107 void SortNodeList(NodesX *nodesx);
108 
109 void RemoveNonHighwayNodes(NodesX *nodesx,WaysX *waysx,int keep);
110 
111 void RemovePrunedNodes(NodesX *nodesx,SegmentsX *segmentsx);
112 
113 void SortNodeListGeographically(NodesX *nodesx);
114 
115 void SaveNodeList(NodesX *nodesx,const char *filename,SegmentsX *segmentsx);
116 
117 
118 /* Macros and inline functions */
119 
120 #if !SLIM
121 
122 #define LookupNodeX(nodesx,index,position)      &(nodesx)->data[index]
123 
124 #define PutBackNodeX(nodesx,nodex)              while(0) { /* nop */ }
125 
126 #else
127 
128 /* Prototypes */
129 
130 static inline NodeX *LookupNodeX(NodesX *nodesx,index_t index,int position);
131 
132 static inline void PutBackNodeX(NodesX *nodesx,NodeX *nodex);
133 
134 CACHE_NEWCACHE_PROTO(NodeX)
CACHE_DELETECACHE_PROTO(NodeX)135 CACHE_DELETECACHE_PROTO(NodeX)
136 CACHE_FETCHCACHE_PROTO(NodeX)
137 CACHE_REPLACECACHE_PROTO(NodeX)
138 CACHE_INVALIDATECACHE_PROTO(NodeX)
139 
140 /* Data type */
141 
142 CACHE_STRUCTURE(NodeX)
143 
144 /* Inline functions */
145 
146 CACHE_NEWCACHE(NodeX)
147 CACHE_DELETECACHE(NodeX)
148 CACHE_FETCHCACHE(NodeX)
149 CACHE_REPLACECACHE(NodeX)
150 CACHE_INVALIDATECACHE(NodeX)
151 
152 
153 /*++++++++++++++++++++++++++++++++++++++
154   Lookup a particular extended node with the specified id from the file on disk.
155 
156   NodeX *LookupNodeX Returns a pointer to a cached copy of the extended node.
157 
158   NodesX *nodesx The set of nodes to use.
159 
160   index_t index The node index to look for.
161 
162   int position The position in the cache to use.
163   ++++++++++++++++++++++++++++++++++++++*/
164 
165 static inline NodeX *LookupNodeX(NodesX *nodesx,index_t index,int position)
166 {
167  nodesx->cached[position-1]=*FetchCachedNodeX(nodesx->cache,index,nodesx->fd,0);
168 
169  nodesx->incache[position-1]=index;
170 
171  return(&nodesx->cached[position-1]);
172 }
173 
174 
175 /*++++++++++++++++++++++++++++++++++++++
176   Put back an extended node's data into the file on disk.
177 
178   NodesX *nodesx The set of nodes to modify.
179 
180   NodeX *nodex The extended node to be put back.
181   ++++++++++++++++++++++++++++++++++++++*/
182 
PutBackNodeX(NodesX * nodesx,NodeX * nodex)183 static inline void PutBackNodeX(NodesX *nodesx,NodeX *nodex)
184 {
185  int position1=nodex-&nodesx->cached[0];
186 
187  ReplaceCachedNodeX(nodesx->cache,nodex,nodesx->incache[position1],nodesx->fd,0);
188 }
189 
190 #endif /* SLIM */
191 
192 
193 #endif /* NODESX_H */
194