robot_name: iteration 4; throw an exception
This commit is contained in:
parent
e1085191f9
commit
e1d89f3467
1 changed files with 5 additions and 1 deletions
|
|
@ -1,6 +1,7 @@
|
|||
#include "robot_name.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
#include <set>
|
||||
#include <random>
|
||||
|
||||
|
|
@ -49,6 +50,9 @@ void robot::reset()
|
|||
static distribution_t alpha_distribution('A', 'Z');
|
||||
static distribution_t digit_distribution('0', '9');
|
||||
|
||||
if (used_names.size() >= name_capacity)
|
||||
throw length_error("no more names available");
|
||||
|
||||
string new_name;
|
||||
|
||||
do {
|
||||
|
|
@ -56,7 +60,7 @@ void robot::reset()
|
|||
new_name = random_string(alpha_part_size, alpha_distribution)
|
||||
+ random_string(digit_part_size, digit_distribution);
|
||||
|
||||
} while (used_names.size() < name_capacity && used_names.count(new_name));
|
||||
} while (used_names.count(new_name));
|
||||
|
||||
used_names.erase(name_);
|
||||
name_ = new_name;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue