Skip to content

Commit da75eae

Browse files
committed
small improvement & fixes.
- fix the max number of landmark selected. - rework the selection of landmark in ALT preprocessing to start by the smallest node ID. - fix getOneOrTwo issue who take all integrer number in entry. - fix API only to take poitive integrer and return error if not the exact type. (for example : 1ABC will prune to 1, but now, it will just return an error "INVALID PARAMETER"). - return an error when the graph is empty at preprocessing time.
1 parent 7f1baf6 commit da75eae

File tree

5 files changed

+31
-7
lines changed

5 files changed

+31
-7
lines changed

src/alt.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Steps of the Algorithm:
1919
* 1. If `save_alt` is enabled, attempt to load precomputed landmark data from storage.
2020
* 2. Initialize storage structures for distance calculations.
21-
* 3. Select the first landmark as an arbitrary node (first node in the dataset).
21+
* 3. Select the first landmark as the node with the smallest ID (e.g., ID 1 if available).
2222
* 4. Compute shortest-path distances using Dijkstra’s algorithm:
2323
* - `dist_landmark[i][j]` stores the shortest path distance from landmark `j` to node `i`.
2424
* 5. Iteratively select `nb_alt` landmarks using the farthest-node heuristic:
@@ -72,7 +72,13 @@ void preprocessAlt(graph& gdata, config& conf)
7272

7373
gdata.dist_landmark.resize(n, std::vector<int>(conf.nb_alt, -1));
7474

75-
int fl = gdata.node_to_index.begin()->first;
75+
int fl = std::numeric_limits<int>::max();
76+
for (const auto& kv : gdata.node_to_index)
77+
{
78+
if (kv.first < fl)
79+
fl = kv.first;
80+
}
81+
7682
landmarks.push_back(fl);
7783
console("info", "processing landmark " + std::to_string(fl) + " (1/" + std::to_string(conf.nb_alt) + ")");
7884
logger("processing landmark " + std::to_string(fl) + " (1/" + std::to_string(conf.nb_alt) + ")");
@@ -87,6 +93,8 @@ void preprocessAlt(graph& gdata, config& conf)
8793
md[idx] = d[idx];
8894
}
8995
}
96+
97+
md[gdata.node_to_index[fl]] = -1;
9098

9199
for (int i = 1; i < conf.nb_alt; ++i) {
92100
int nl = -1;

src/api.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,12 @@ int launchApiGateway(const graph& gdata, search_buffers& buffers, const config&
224224
try {
225225
size_t amp_pos = query_part.find("&", st_pos);
226226
std::string val = query_part.substr(st_pos + 6, amp_pos - (st_pos + 6));
227-
start_val = std::stoi(val);
227+
size_t consumed = 0;
228+
int parsed = std::stoi(val, &consumed);
229+
if (consumed != val.size()) {
230+
throw std::invalid_argument("invalid integer parameter");
231+
}
232+
start_val = parsed;
228233
start_ok = true;
229234
}
230235
catch (...) {
@@ -238,7 +243,12 @@ int launchApiGateway(const graph& gdata, search_buffers& buffers, const config&
238243
try {
239244
size_t amp_pos = query_part.find("&", en_pos);
240245
std::string val = query_part.substr(en_pos + 4, amp_pos - (en_pos + 4));
241-
end_val = std::stoi(val);
246+
size_t consumed = 0;
247+
int parsed = std::stoi(val, &consumed);
248+
if (consumed != val.size()) {
249+
throw std::invalid_argument("invalid integer parameter");
250+
}
251+
end_val = parsed;
242252
end_ok = true;
243253
}
244254
catch (...) {

src/csv.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ bool readCSV(const std::string& map_path, std::vector<edge>& edges)
176176
logger("total lines read: " + std::to_string(line_num));
177177
logger("total valid edges loaded: " + std::to_string(valid_lines));
178178

179+
if (edges.size() == 0) {
180+
logger("error: no valid edges loaded. graph is empty.");
181+
console("error", "no valid edges loaded. graph is empty.");
182+
return false;
183+
}
184+
179185
return true;
180186
}
181187

src/decl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ void clear();
177177
int getInteger(const std::string& prompt);
178178
double getPercentage(const std::string& prompt);
179179
bool getYesNo(const std::string& prompt);
180-
double getOneOrTwo(const std::string& prompt);
180+
int getOneOrTwo(const std::string& prompt);
181181

182182
void initializeSearchBuffers(const graph& gdata, search_buffers& buffers);
183183

src/helper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ double getPercentage(const std::string& prompt)
163163
* @param prompt The message displayed to the user.
164164
* @return '1' or '2'.
165165
*/
166-
double getOneOrTwo(const std::string& prompt)
166+
int getOneOrTwo(const std::string& prompt)
167167
{
168168
int one_or_two;
169169
while (true) {
170170
one_or_two = getInteger(prompt);
171-
if (one_or_two != 1 || one_or_two != 2) {
171+
if (one_or_two == 1 || one_or_two == 2) {
172172
break;
173173
}
174174
console("error", "invalid input. please enter 1 or 2.");

0 commit comments

Comments
 (0)