Imported Upstream version 1.5
[routino] / src / segments.h
1 /***************************************
2  $Header: /home/amb/routino/src/RCS/segments.h,v 1.37 2010/07/31 14:36:15 amb Exp $
3
4  A header file for the segments.
5
6  Part of the Routino routing software.
7  ******************/ /******************
8  This file Copyright 2008-2010 Andrew M. Bishop
9
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Affero General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU Affero General Public License for more details.
19
20  You should have received a copy of the GNU Affero General Public License
21  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  ***************************************/
23
24
25 #ifndef SEGMENTS_H
26 #define SEGMENTS_H    /*+ To stop multiple inclusions. +*/
27
28 #include <stdint.h>
29
30 #include "types.h"
31
32 #include "files.h"
33 #include "profiles.h"
34
35
36 /* Data structures */
37
38
39 /*+ A structure containing a single segment. +*/
40 struct _Segment
41 {
42  index_t    node1;              /*+ The index of the starting node. +*/
43  index_t    node2;              /*+ The index of the finishing node. +*/
44
45  index_t    next2;              /*+ The index of the next segment sharing node2. +*/
46
47  index_t    way;                /*+ The index of the way associated with the segment. +*/
48
49  distance_t distance;           /*+ The distance between the nodes. +*/
50 };
51
52
53 /*+ A structure containing the header from the file. +*/
54 typedef struct _SegmentsFile
55 {
56  index_t   number;              /*+ How many segments in total? +*/
57  index_t   snumber;             /*+ How many super-segments? +*/
58  index_t   nnumber;             /*+ How many normal segments? +*/
59 }
60  SegmentsFile;
61
62
63 /*+ A structure containing a set of segments (and pointers to mmap file). +*/
64 struct _Segments
65 {
66  SegmentsFile file;             /*+ The header data from the file. +*/
67
68 #if !SLIM
69
70  void        *data;             /*+ The memory mapped data. +*/
71
72  Segment     *segments;         /*+ An array of segments. +*/
73
74 #else
75
76  int          fd;               /*+ The file descriptor for the file. +*/
77
78  Segment      cached[3];        /*+ The cached segments. +*/
79  index_t      incache[3];       /*+ The indexes of the cached segments. +*/
80
81 #endif
82 };
83
84
85 /* Functions */
86
87 Segments *LoadSegmentList(const char *filename);
88
89 Segment *NextSegment(Segments* segments,Segment *segment,index_t node);
90
91 distance_t Distance(double lat1,double lon1,double lat2,double lon2);
92
93 duration_t Duration(Segment *segment,Way *way,Profile *profile);
94
95
96 /* Macros and inline functions */
97
98 /*+ Return true if this is a normal segment. +*/
99 #define IsNormalSegment(xxx)   (((xxx)->distance)&SEGMENT_NORMAL)
100
101 /*+ Return true if this is a super-segment. +*/
102 #define IsSuperSegment(xxx)    (((xxx)->distance)&SEGMENT_SUPER)
103
104 /*+ Return true if the segment is oneway towards the specified node. +*/
105 #define IsOnewayTo(xxx,yyy)    ((xxx)->node1==(yyy)?((xxx)->distance&ONEWAY_2TO1):((xxx)->distance&ONEWAY_1TO2))
106
107 /*+ Return true if the segment is oneway from the specified node. +*/
108 #define IsOnewayFrom(xxx,yyy)  ((xxx)->node2==(yyy)?((xxx)->distance&ONEWAY_2TO1):((xxx)->distance&ONEWAY_1TO2))
109
110 /*+ Return the other node in the segment that is not the specified node. +*/
111 #define OtherNode(xxx,yyy)     ((xxx)->node1==(yyy)?(xxx)->node2:(xxx)->node1)
112
113
114 #if !SLIM
115
116 /*+ Return a segment pointer given a set of segments and an index. +*/
117 #define LookupSegment(xxx,yyy,zzz) (&(xxx)->segments[yyy])
118
119 /*+ Return a segment index given a set of segments and a pointer. +*/
120 #define IndexSegment(xxx,yyy)      ((yyy)-&(xxx)->segments[0])
121
122 #else
123
124 static Segment *LookupSegment(Segments *segments,index_t index,int position);
125
126 static index_t IndexSegment(Segments *segments,Segment *segment);
127
128
129 /*++++++++++++++++++++++++++++++++++++++
130   Find the Segment information for a particular segment.
131
132   Segment *LookupSegment Returns a pointer to the cached segment information.
133
134   Segments *segments The segments structure to use.
135
136   index_t index The index of the segment.
137
138   int position The position in the cache to store the value.
139   ++++++++++++++++++++++++++++++++++++++*/
140
141 static inline Segment *LookupSegment(Segments *segments,index_t index,int position)
142 {
143  SeekFile(segments->fd,sizeof(SegmentsFile)+(off_t)index*sizeof(Segment));
144
145  ReadFile(segments->fd,&segments->cached[position-1],sizeof(Segment));
146
147  segments->incache[position-1]=index;
148
149  return(&segments->cached[position-1]);
150 }
151
152
153 /*++++++++++++++++++++++++++++++++++++++
154   Find the segment index for a particular segment pointer.
155
156   index_t IndexSegment Returns the index of the segment in the list.
157
158   Segments *segments The segments structure to use.
159
160   Segment *segment The segment whose index is to be found.
161   ++++++++++++++++++++++++++++++++++++++*/
162
163 static inline index_t IndexSegment(Segments *segments,Segment *segment)
164 {
165  int i;
166
167  for(i=0;i<sizeof(segments->cached)/sizeof(segments->cached[0]);i++)
168     if(&segments->cached[i]==segment)
169        return(segments->incache[i]);
170
171  return(NO_SEGMENT);
172 }
173
174 #endif
175
176
177 #endif /* SEGMENTS_H */