Skip to content

Commit

Permalink
remove pointer arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
abyrd committed Jun 17, 2013
1 parent c04e233 commit 40f2d7d
Show file tree
Hide file tree
Showing 16 changed files with 333 additions and 233 deletions.
6 changes: 3 additions & 3 deletions Makefile
Expand Up @@ -15,13 +15,13 @@ all: $(BINS)
brrrroker: broker.o
$(CC) $(CFLAGS) $^ $(LIBS) -o $@

workerrrr: bitset.o qstring.o router.o transitdata.o util.o worker.o bitset.o
workerrrr: bitset.o qstring.o router.o tdata.o util.o worker.o bitset.o
$(CC) $(CFLAGS) $^ $(LIBS) -o $@

client: bitset.o qstring.o router.o transitdata.o util.o client.o
client: bitset.o qstring.o router.o tdata.o util.o client.o
$(CC) $(CFLAGS) $^ $(LIBS) -o $@

lookup-console: transitdata.o util.o lookup-console.o trie.o
lookup-console: tdata.o util.o lookup-console.o trie.o
$(CC) $(CFLAGS) $^ $(LIBS) -o $@

clean:
Expand Down
12 changes: 10 additions & 2 deletions client.c
Expand Up @@ -66,8 +66,16 @@ int main (int argc, char **argv) {
// read and range-check parameters
int n_requests = 1;
int concurrency = RRRR_TEST_CONCURRENCY;
if (argc > 1)
randomize = !(strcmp(argv[1], "rand"));
if (argc != 4)
usage();

if (strcmp(argv[1], "rand") == 0) {
randomize = true;
} else if (strcmp(argv[1], "id") == 0) {
randomize = false;
} else {
usage();
}

if (randomize && argc > 2) {
n_requests = atoi(argv[2]);
Expand Down
7 changes: 7 additions & 0 deletions config.h
Expand Up @@ -20,10 +20,17 @@
// #define CLIENT_ENDPOINT "ipc://client_pipe"
// #define WORKER_ENDPOINT "ipc://worker_pipe"

// #define INFO
// #define DEBUG
// #define TRACE

/* http://stackoverflow.com/questions/1644868/c-define-macro-for-debug-printing */
#ifdef INFO
#define I
#else
#define I for(;0;)
#endif

#ifdef DEBUG
#define D
#else
Expand Down
4 changes: 2 additions & 2 deletions demo.sh
@@ -1,9 +1,9 @@
#!/bin/bash

./haltes ns_stops.txt
./haltes stops
read -r FROM < /tmp/stopid
echo $FROM
./haltes ns_stops.txt
./haltes stops
read -r TO < /tmp/stopid
echo $TO
./client id $FROM $TO | python ./resolver.py ./nl.gtfsdb
Expand Down
6 changes: 3 additions & 3 deletions lookup-console.c
Expand Up @@ -7,7 +7,7 @@
#include <stdlib.h>

#include "trie.h"
#include "transitdata.h"
#include "tdata.h"
#include "config.h"

static struct termios g_old_kbd_mode;
Expand Down Expand Up @@ -53,10 +53,10 @@ int main(int argc, char *argv[]){
int len = 0;
unsigned index = 0;
char text[255];
transit_data_t tdata;
tdata_t tdata;
trie_t *t = trie_init();

transit_data_load(RRRR_INPUT_FILE, &tdata);
tdata_load(RRRR_INPUT_FILE, &tdata);
trie_load(t, &tdata);
char suffix[512];

Expand Down
2 changes: 1 addition & 1 deletion resolver.py
Expand Up @@ -35,7 +35,7 @@ def trip_info(trip_id) :
legs = []
for line in sys.stdin.readlines() :
try :
tripid, fromid, fromtime, toid, totime = line.split()
tripid, fromid, fromtime, toid, totime = line.split(';')
line = '%5s - %5s %s to %s [%s]' % (fromtime[:5], totime[:5], stop_name(fromid), stop_name(toid), trip_info(tripid))
except :
pass
Expand Down
304 changes: 172 additions & 132 deletions router.c

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions router.h
Expand Up @@ -6,7 +6,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include "transitdata.h"
#include "tdata.h"
#include "bitset.h"
#include "util.h"

Expand All @@ -21,7 +21,7 @@ struct router_state {

typedef struct router router_t;
struct router {
transit_data_t tdata;
tdata_t tdata;
// scratch space for solving
// making this opaque requires more dynamic allocation
int table_size;
Expand All @@ -41,7 +41,7 @@ struct router_request {
bool arrive_by;
};

void router_setup(router_t*, transit_data_t*);
void router_setup(router_t*, tdata_t*);

bool router_request_from_qstring(router_request_t*);

Expand Down
71 changes: 33 additions & 38 deletions transitdata.c → tdata.c
@@ -1,5 +1,5 @@
/* transitdata.c : handles memory mapped data file with timetable etc. */
#include "transitdata.h" // make sure it works alone
#include "tdata.h" // make sure it works alone
#include "util.h"
#include <fcntl.h>
#include <sys/mman.h>
Expand All @@ -9,8 +9,8 @@
#include <stdio.h>

// file-visible struct
typedef struct transit_data_header transit_data_header_t;
struct transit_data_header {
typedef struct tdata_header tdata_header_t;
struct tdata_header {
char version_string[8]; // should read "TTABLEV1"
int nstops;
int nroutes;
Expand All @@ -27,28 +27,27 @@ struct transit_data_header {
int loc_route_active;
};

inline char *transit_data_stop_id_for_index(transit_data_t *td, int stop_index) {
inline char *tdata_stop_id_for_index(tdata_t *td, int stop_index) {
return td->stop_ids + (td->stop_id_width * stop_index);
}

inline char *transit_data_route_id_for_index(transit_data_t *td, int route_index) {
inline char *tdata_route_id_for_index(tdata_t *td, int route_index) {
return td->route_ids + (td->route_id_width * route_index);
}

// this should probably return the pointer and store the number of items in a parameter, to avoid awkward & and match other functions.
inline char *transit_data_trip_ids_for_route_index(transit_data_t *td, int route_index) {
inline char *tdata_trip_ids_for_route(tdata_t *td, int route_index) {
route_t route = (td->routes)[route_index];
int char_offset = route.trip_ids_offset * td->trip_id_width;
return td->trip_ids + char_offset;
}

inline uint32_t *transit_data_trip_masks_for_route_index(transit_data_t *td, int route_index) {
inline uint32_t *tdata_trip_masks_for_route(tdata_t *td, int route_index) {
route_t route = (td->routes)[route_index];
return td->trip_active + route.trip_ids_offset;
}

/* Map an input file into memory and reconstruct pointers to its contents. */
void transit_data_load(char *filename, transit_data_t *td) {
void tdata_load(char *filename, tdata_t *td) {

int fd = open(filename, O_RDONLY);
if (fd == -1)
Expand All @@ -64,7 +63,7 @@ void transit_data_load(char *filename, transit_data_t *td) {
die("could not map input file");

void *b = td->base;
transit_data_header_t *header = b;
tdata_header_t *header = b;
if( strncmp("TTABLEV1", header->version_string, 8) )
die("the input file does not appear to be a timetable");
td->nstops = header->nstops;
Expand All @@ -87,50 +86,46 @@ void transit_data_load(char *filename, transit_data_t *td) {
td->route_active = (uint32_t*) (b + header->loc_route_active);
}

void transit_data_close(transit_data_t *td) {
void tdata_close(tdata_t *td) {
munmap(td->base, td->size);
}

// change to return number of stops, store pointer to array.
inline int *transit_data_stops_for_route(transit_data_t td, int route, int **next_route) {
inline int *tdata_stops_for_route(tdata_t td, int route) {
route_t route0 = td.routes[route];
route_t route1 = td.routes[route + 1];
*next_route = td.route_stops + route1.route_stops_offset;
return td.route_stops + route0.route_stops_offset;
}

inline int transit_data_routes_for_stop(transit_data_t *td, int stop, int **routes_ret) {
inline int tdata_routes_for_stop(tdata_t *td, int stop, int **routes_ret) {
stop_t stop0 = td->stops[stop];
stop_t stop1 = td->stops[stop + 1];
*routes_ret = td->stop_routes + stop0.stop_routes_offset;
return stop1.stop_routes_offset - stop0.stop_routes_offset;
}

inline stoptime_t *transit_data_stoptimes_for_route(transit_data_t td, int route, stoptime_t **next_route) {
route_t route0 = td.routes[route];
route_t route1 = td.routes[route + 1];
*next_route = td.stop_times + route1.stop_times_offset;
return td.stop_times + route0.stop_times_offset;
inline stoptime_t *tdata_stoptimes_for_route(tdata_t *td, int route_index) {
route_t *route = &( td->routes[route_index] );
return td->stop_times + route->stop_times_offset;
}

void transit_data_dump_route(transit_data_t *td, int route) {
int *s_end;
int *s = transit_data_stops_for_route(*td, route, &s_end);
rtime_t *t_end;
rtime_t *t = transit_data_stoptimes_for_route(*td, route, &t_end);
int nstops = s_end - s;
printf("ROUTE %d NSTOPS %d\n", route, nstops);
printf("SSEQ SINDEX \n");
for (int i = 0; i < nstops; ++i) {
printf("%4d %6d : ", i, s[i]);
for (rtime_t *t2 = &(t[i]); t2 < t_end; t2 += nstops) {
printf("%s ", timetext(*t2));
void tdata_dump_route(tdata_t *td, int route_idx) {
int *stops = tdata_stops_for_route(*td, route_idx);
route_t route = td->routes[route_idx];
stoptime_t (*times)[route.n_stops] = (void*) tdata_stoptimes_for_route(td, route_idx);
printf("\nRoute details for '%s' [%d] (n_stops %d, n_trips %d)\n",
tdata_route_id_for_index(td, route_idx), route_idx, route.n_stops, route.n_trips);
printf("stop sequence, stop name (index), departures \n");
for (int si = 0; si < route.n_stops; ++si) {
char *stop_id = tdata_stop_id_for_index (td, stops[si]);
printf("%4d %35s [%06d] : ", si, stop_id, stops[si]);
for (int ti = 0; ti < route.n_trips; ++ti) {
printf("%s ", timetext(times[ti][si].departure));
}
printf("\n");
}
printf("\n");
}

void transit_data_dump(transit_data_t *td) {
void tdata_dump(tdata_t *td) {
printf("\nCONTEXT\n"
"nstops: %d\n"
"nroutes: %d\n", td->nstops, td->nroutes);
Expand Down Expand Up @@ -164,17 +159,17 @@ void transit_data_dump(transit_data_t *td) {
}
printf("\nSTOPIDS\n");
for (int i = 0; i < td->nstops; i++) {
printf("stop %03d has id %s \n", i, transit_data_stop_id_for_index(td, i));
printf("stop %03d has id %s \n", i, tdata_stop_id_for_index(td, i));
}
printf("\nROUTEIDS, TRIPIDS\n");
for (int i = 0; i < td->nroutes; i++) {
printf("route %03d has id %s and first trip id %s \n", i,
transit_data_route_id_for_index(td, i),
transit_data_trip_ids_for_route_index(td, i));
tdata_route_id_for_index(td, i),
tdata_trip_ids_for_route(td, i));
}
}


// transit_data_get_route_stops
// tdata_get_route_stops

/* optional stop ids, names, coordinates... */
39 changes: 20 additions & 19 deletions transitdata.h → tdata.h
@@ -1,6 +1,6 @@
/* transitdata.h */
#ifndef _TRANSIT_DATA_H
#define _TRANSIT_DATA_H
/* tdata.h */
#ifndef _TDATA_H
#define _TDATA_H

#include "geometry.h"
#include "util.h"
Expand All @@ -14,10 +14,12 @@ struct stop {
};

typedef struct route route_t;
struct route {
struct route { // switch to unsigned
int route_stops_offset;
int stop_times_offset;
int trip_ids_offset;
int n_stops;
int n_trips;
};

typedef struct transfer transfer_t;
Expand All @@ -33,8 +35,8 @@ struct stoptime {
};

// treat entirely as read-only?
typedef struct transit_data transit_data_t;
struct transit_data {
typedef struct tdata tdata_t;
struct tdata {
void *base;
size_t size;
// required data
Expand All @@ -58,29 +60,28 @@ struct transit_data {
uint32_t *route_active;
};

void transit_data_load(char* /*filename*/, transit_data_t*);
void tdata_load(char* /*filename*/, tdata_t*);

void transit_data_close(transit_data_t*);
void tdata_close(tdata_t*);

void transit_data_dump(transit_data_t*);
void tdata_dump(tdata_t*);


int *transit_data_stops_for_route(transit_data_t, int route, int **next_route);
int *tdata_stops_for_route(tdata_t, int route);

/* TODO: return number of items and store pointer to beginning, to allow restricted pointers */
int transit_data_routes_for_stop(transit_data_t*, int stop, int **routes_ret);
int tdata_routes_for_stop(tdata_t*, int stop, int **routes_ret);

stoptime_t *transit_data_stoptimes_for_route(transit_data_t, int route, stoptime_t **next_route);
stoptime_t *tdata_stoptimes_for_route(tdata_t*, int route_index);

void transit_data_dump_route(transit_data_t *td, int route);
void tdata_dump_route(tdata_t*, int route_index);

char *transit_data_stop_id_for_index(transit_data_t*, int stop_index);
char *tdata_stop_id_for_index(tdata_t*, int stop_index);

char *transit_data_route_id_for_index(transit_data_t*, int route_index);
char *tdata_route_id_for_index(tdata_t*, int route_index);

char *transit_data_trip_ids_for_route_index(transit_data_t*, int route_index);
char *tdata_trip_ids_for_route(tdata_t*, int route_index);

uint32_t *transit_data_trip_masks_for_route_index(transit_data_t *td, int route_index);
uint32_t *tdata_trip_masks_for_route(tdata_t*, int route_index);

#endif // _TRANSIT_DATA_H
#endif // _TDATA_H

0 comments on commit 40f2d7d

Please sign in to comment.