void navigate_graph_clear (void) { while (SquareCacheSize > 0) { SquareCacheSize--; free_cache_slot (SquareCacheSize); } }
static void navigate_graph_clear_all (void) { int slot; for (slot = SquareCacheSize - 1; slot >= 0; slot--) { free_cache_slot (slot); } SquareCacheSize = 0; }
void navigate_graph_clear (int square) { int slot; if (square == -1) { navigate_graph_clear_all (); return; } for (slot = SquareCacheSize - 1; slot >= 0 && SquareGraphCache[slot]->square_id != square; slot--) ; if (slot >= 0) { SquareCacheSize--; free_cache_slot (slot); while (slot < SquareCacheSize) { SquareGraphCache[slot] = SquareGraphCache[slot + 1]; slot++; } } }
//TODO arrange as LRU list static struct SquareGraphItem *get_square_graph (int square_id) { int i; int slot; int line; int lines1_count; int lines2_count; struct SquareGraphItem *cache; int cur_line = 0; int found = 0; for (slot = 0; slot < SquareCacheSize; slot++) { if (SquareGraphCache[slot]->square_id == square_id) { found = 1; break; } } if (!found) { if (SquareCacheSize == MAX_GRAPH_CACHE) { slot--; free_cache_slot (slot); cache = SquareGraphCache[slot]; } else { cache = (struct SquareGraphItem *)malloc(sizeof(struct SquareGraphItem)); SquareCacheSize++; } } else { cache = SquareGraphCache[slot]; } for (i = slot; i > 0; i--) { SquareGraphCache[i] = SquareGraphCache[i - 1]; } SquareGraphCache[0] = cache; if (found) return cache; cache->square_id = square_id; lines1_count = 0; lines2_count = 0; /* Count total lines */ for (i = ROADMAP_ROAD_FIRST; i <= ROADMAP_ROAD_LAST; ++i) { int first_line; int last_line; if (roadmap_line_in_square (square_id, i, &first_line, &last_line) > 0) { lines1_count += (last_line - first_line + 1); } } cache->lines_count = lines1_count * 2 + lines2_count; /* assume that the number of nodes equals the number of lines */ cache->nodes_count = roadmap_square_points_count (square_id); cache->mem_size = cache->lines_count * sizeof(int) + cache->lines_count * sizeof(unsigned short) + cache->nodes_count * sizeof(unsigned short); while (cache_total_mem && ((cache_total_mem + cache->mem_size) > MAX_MEM_CACHE) && SquareCacheSize > 1) { SquareCacheSize--; free_cache_slot(SquareCacheSize); free (SquareGraphCache[SquareCacheSize]); SquareGraphCache[SquareCacheSize] = NULL; } cache->lines = malloc(cache->lines_count * sizeof(int)); cache->lines_index = calloc(cache->lines_count, sizeof(unsigned short)); cache->nodes_index = calloc(cache->nodes_count, sizeof(unsigned short)); cache_total_mem += cache->mem_size; for (i = ROADMAP_ROAD_LAST; i >= ROADMAP_ROAD_FIRST; --i) { int first_line; int last_line; if (roadmap_line_in_square (square_id, i, &first_line, &last_line) > 0) { for (line = last_line; line >= first_line; line--) { int from_point_id; int to_point_id; roadmap_line_points (line, &from_point_id, &to_point_id); from_point_id &= 0xffff; add_graph_node(cache, line, from_point_id, cur_line, 0); cur_line++; //if (roadmap_point_square(to_point_id) == square_id) { to_point_id &= 0xffff; add_graph_node(cache, line, to_point_id, cur_line, REVERSED); cur_line++; //} } } } assert(cur_line <= cache->lines_count); return cache; }