Skip to content

Commit

Permalink
Return the list (instrad of constructing it)
Browse files Browse the repository at this point in the history
Things probably became a wee bit faster.
  • Loading branch information
codemime committed Apr 24, 2016
1 parent 3557f56 commit be7deb4
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2727,8 +2727,8 @@ void cata_tiles::do_tile_loading_report() {
//TODO: exclude fake items from Item_factory::init_old()
tile_loading_report(item_controller->get_all_itypes(), "Items", "");
auto mtypes = MonsterGenerator::generator().get_all_mtypes();
lr_generic( mtypes.begin(), mtypes.end(), []( std::vector<const mtype *>::iterator m ) {
return ( *m )->id.str();
lr_generic( mtypes.begin(), mtypes.end(), []( const std::vector<mtype>::iterator &m ) {
return ( *m ).id.str();
}, "Monsters", "" );
tile_loading_report<vpart_info>(vpart_info::get_all().size(), "Vehicle Parts", "vp_");
tile_loading_report<trap>(trap::count(), "Traps", "");
Expand Down
2 changes: 1 addition & 1 deletion src/defense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ void defense_game::game_over()
void defense_game::init_mtypes()
{
for( auto &type : MonsterGenerator::generator().get_all_mtypes() ) {
mtype *const t = const_cast<mtype *>( type );
mtype *const t = const_cast<mtype *>( &type );
t->difficulty *= 1.5;
t->difficulty += int( t->difficulty / 5 );
t->flags.insert( MF_BASHES );
Expand Down
4 changes: 2 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,8 +1111,8 @@ bool game::cleanup_at_end()
int iTotalKills = 0;

for( const auto &type : MonsterGenerator::generator().get_all_mtypes() ) {
if( kill_count( type->id ) > 0 ) {
iTotalKills += kill_count( type->id );
if( kill_count( type.id ) > 0 ) {
iTotalKills += kill_count( type.id );
}
}

Expand Down
16 changes: 4 additions & 12 deletions src/generic_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,22 +198,14 @@ class generic_factory
/**
* Returns all the loaded objects. It can be used to iterate over them.
* This returns a reference and is therefor quite fast, but you can also
* use @ref get_all, which returns a copy of this data with raw pointers.
* You should prefer `get_all` as it exposes a more stable interface.
* use @ref get_all. You should prefer @ref get_all as it exposes a more
* stable interface.
*/
const std::unordered_map<string_id<T>, T> &all_ref() const {
return data;
}
std::vector<const T *> get_all() const {
std::vector<const T *> result;
result.reserve( data.size() );

using pair = typename std::unordered_map<string_id<T>, T>::value_type;
std::transform( data.begin(), data.end(), back_inserter( result ), []( const pair & p ) {
return &p.second;
} );

return result;
const std::vector<T> &get_all() const {
return list;
}
/**
* @name `string_id` interface functions
Expand Down
2 changes: 1 addition & 1 deletion src/monstergenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ void species_type::load( JsonObject &jo )
optional( jo, was_loaded, "fear_triggers", fear_trig, trigger_reader );
}

std::vector<const mtype *> MonsterGenerator::get_all_mtypes() const
const std::vector<mtype> &MonsterGenerator::get_all_mtypes() const
{
return mon_templates->get_all();
}
Expand Down
2 changes: 1 addition & 1 deletion src/monstergenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class MonsterGenerator

void check_monster_definitions() const;

std::vector<const mtype *> get_all_mtypes() const;
const std::vector<mtype> &get_all_mtypes() const;
mtype_id get_valid_hallucination() const;
friend struct mtype;
friend struct species_type;
Expand Down
42 changes: 21 additions & 21 deletions src/newcharacter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ void player::randomize( const bool random_scenario, points_left &points )
}
if( random_scenario ) {
std::vector<const scenario *> scenarios;
for( const scenario *const scenptr : scenario::get_all() ) {
if (!scenptr->has_flag("CHALLENGE")) {
scenarios.emplace_back( scenptr );
for( const auto &scen : scenario::get_all() ) {
if (!scen.has_flag("CHALLENGE")) {
scenarios.emplace_back( &scen );
}
}
g->scen = random_entry( scenarios );
Expand Down Expand Up @@ -342,7 +342,7 @@ void player::randomize( const bool random_scenario, points_left &points )
case 2:
case 3:
case 4:
if( allow_traits ) {
if( allow_traits ) {
rn = random_good_trait();
auto &mdata = mutation_branch::get( rn );
if( !has_trait(rn) && points.trait_points_left() >= mdata.points &&
Expand Down Expand Up @@ -1307,13 +1307,13 @@ tab_direction set_profession(WINDOW *w, player *u, points_left &points)
do {
if (recalc_profs) {
sorted_profs.clear();
for( const profession *const profptr : profession::get_all() ) {
if ((g->scen->profsize() == 0 && profptr->has_flag("SCEN_ONLY") == false) ||
g->scen->profquery( profptr->ident() ) ) {
if (!lcmatch(profptr->gender_appropriate_name(u->male), filterstring)) {
for( const auto &prof : profession::get_all() ) {
if ((g->scen->profsize() == 0 && prof.has_flag("SCEN_ONLY") == false) ||
g->scen->profquery( prof.ident() ) ) {
if (!lcmatch(prof.gender_appropriate_name(u->male), filterstring)) {
continue;
}
sorted_profs.push_back(profptr);
sorted_profs.push_back(&prof);
}
}
profs_length = sorted_profs.size();
Expand Down Expand Up @@ -1595,8 +1595,8 @@ tab_direction set_skills(WINDOW *w, player *u, points_left &points)

std::map<skill_id, int> prof_skills;
const auto &pskills = u->prof->skills();
std::copy( pskills.begin(), pskills.end(),

std::copy( pskills.begin(), pskills.end(),
std::inserter( prof_skills, prof_skills.begin() ) );

do {
Expand Down Expand Up @@ -1843,11 +1843,11 @@ tab_direction set_scenario(WINDOW *w, player *u, points_left &points)
do {
if (recalc_scens) {
sorted_scens.clear();
for( const scenario *const scenptr : scenario::get_all() ) {
if (!lcmatch(scenptr->gender_appropriate_name(u->male), filterstring)) {
for( const auto &scen : scenario::get_all() ) {
if (!lcmatch(scen.gender_appropriate_name(u->male), filterstring)) {
continue;
}
sorted_scens.push_back( scenptr );
sorted_scens.push_back( &scen );
}
scens_length = sorted_scens.size();
if (scens_length == 0) {
Expand Down Expand Up @@ -2128,10 +2128,10 @@ tab_direction set_description(WINDOW *w, player *u, const bool allow_reroll, poi
uimenu select_location;
select_location.text = _("Select a starting location.");
int offset = 0;
for( const start_location *const loc : start_location::get_all() ) {
if (g->scen->allowed_start(loc->ident()) || g->scen->has_flag("ALL_STARTS")) {
select_location.entries.push_back( uimenu_entry( loc->name() ) );
if( loc->ident() == u->start_location ) {
for( const auto &loc : start_location::get_all() ) {
if (g->scen->allowed_start(loc.ident()) || g->scen->has_flag("ALL_STARTS")) {
select_location.entries.push_back( uimenu_entry( loc.name() ) );
if( loc.ident() == u->start_location ) {
select_location.selected = offset;
}
offset++;
Expand Down Expand Up @@ -2344,9 +2344,9 @@ tab_direction set_description(WINDOW *w, player *u, const bool allow_reroll, poi
} else if ( action == "CHOOSE_LOCATION" ) {
select_location.redraw();
select_location.query();
for( const start_location *const loc : start_location::get_all() ) {
if( loc->name() == select_location.entries[ select_location.selected ].txt ) {
u->start_location = loc->ident();
for( const auto &loc : start_location::get_all() ) {
if( loc.name() == select_location.entries[ select_location.selected ].txt ) {
u->start_location = loc.ident();
}
}
werase(select_location.window);
Expand Down
12 changes: 6 additions & 6 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2038,12 +2038,12 @@ void player::memorial( std::ofstream &memorial_file, std::string epitaph )

// map <name, sym> to kill count
for( const auto &type : MonsterGenerator::generator().get_all_mtypes() ) {
if( g->kill_count( type->id ) > 0 ) {
if( g->kill_count( type.id ) > 0 ) {
kill_counts[std::tuple<std::string,std::string>(
type->nname(),
type->sym
)] += g->kill_count( type->id );
total_kills += g->kill_count( type->id );
type.nname(),
type.sym
)] += g->kill_count( type.id );
total_kills += g->kill_count( type.id );
}
}

Expand Down Expand Up @@ -9472,7 +9472,7 @@ bool player::can_wear( const item& it, bool alert ) const

if( ( ( it.covers( bp_foot_l ) && is_wearing_shoes( "left" ) ) ||
( it.covers( bp_foot_r ) && is_wearing_shoes( "right") ) ) &&
( !it.has_flag( "OVERSIZE" ) || !it.has_flag( "OUTER" ) ) &&
( !it.has_flag( "OVERSIZE" ) || !it.has_flag( "OUTER" ) ) &&
!it.has_flag( "SKINTIGHT" ) && !it.has_flag( "BELTED" ) ) {
// Checks to see if the player is wearing shoes
if( alert ) {
Expand Down
2 changes: 1 addition & 1 deletion src/profession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const profession *profession::weighted_random()
}
}

std::vector<const profession *> profession::get_all()
const std::vector<profession> &profession::get_all()
{
return all_profs.get_all();
}
Expand Down
2 changes: 1 addition & 1 deletion src/profession.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ enum add_type : int;
static const profession *generic(); // points to the generic, default profession
// return a random profession, weighted for use w/ random character creation or npcs
static const profession *weighted_random();
static std::vector<const profession *> get_all();
static const std::vector<profession> &get_all();

static bool has_initialized();
// clear profession map, every profession pointer becames invalid!
Expand Down
2 changes: 1 addition & 1 deletion src/scenario.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const scenario *scenario::weighted_random()
}
}

std::vector<const scenario *> scenario::get_all()
const std::vector<scenario> &scenario::get_all()
{
return all_scenarios.get_all();
}
Expand Down
2 changes: 1 addition & 1 deletion src/scenario.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class scenario
static const scenario* generic(); // points to the generic, default profession
// return a random scenario, weighted for use w/ random character creation
static const scenario* weighted_random();
static std::vector<const scenario*> get_all();
static const std::vector<scenario> &get_all();

// clear scenario map, every scenario pointer becames invalid!
static void reset();
Expand Down
2 changes: 1 addition & 1 deletion src/start_location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ std::string start_location::target() const
return _target;
}

std::vector<const start_location *> start_location::get_all()
const std::vector<start_location> &start_location::get_all()
{
return all_starting_locations.get_all();
}
Expand Down
2 changes: 1 addition & 1 deletion src/start_location.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class start_location
static void load_location( JsonObject &jsonobj );
static void reset();

static std::vector<const start_location *> get_all();
static const std::vector<start_location> &get_all();

/**
* Find a suitable start location on the overmap.
Expand Down
8 changes: 4 additions & 4 deletions src/wish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,12 @@ void game::wishmonster( const tripoint &p )

int i = 0;
for( const auto &montype : MonsterGenerator::generator().get_all_mtypes() ) {
wmenu.addentry( i, true, 0, "%s", montype->nname().c_str() );
wmenu.entries[i].extratxt.txt = montype->sym;
wmenu.entries[i].extratxt.color = montype->color;
wmenu.addentry( i, true, 0, "%s", montype.nname().c_str() );
wmenu.entries[i].extratxt.txt = montype.sym;
wmenu.entries[i].extratxt.color = montype.color;
wmenu.entries[i].extratxt.left = 1;
++i;
mtypes.push_back( montype );
mtypes.push_back( &montype );
}

do {
Expand Down

0 comments on commit be7deb4

Please sign in to comment.