diff options
author | Neil Moore <neil@s-z.org> | 2014-07-13 04:55:04 -0400 |
---|---|---|
committer | Neil Moore <neil@s-z.org> | 2014-07-13 05:04:20 -0400 |
commit | 366234d7866321869deb3bc75800a91091f3adeb (patch) | |
tree | 64206f0d7e3aa6970090c1a96c6161181ef79cd8 | |
parent | 8caa665a5bfd477586b1619f879ee4dcd979859a (diff) | |
download | crawl-ref-366234d7866321869deb3bc75800a91091f3adeb.tar.gz crawl-ref-366234d7866321869deb3bc75800a91091f3adeb.zip |
Remove some duplicate code.
We had a template to compare pairs by their second member, and two
separate classes to do the same thing for specific pair types. Remove
the non-generic versions, and move the template to libutil.h.
-rw-r--r-- | crawl-ref/source/effects.cc | 16 | ||||
-rw-r--r-- | crawl-ref/source/libutil.h | 11 | ||||
-rw-r--r-- | crawl-ref/source/mon-abil.cc | 16 | ||||
-rw-r--r-- | crawl-ref/source/spl-other.cc | 16 |
4 files changed, 22 insertions, 37 deletions
diff --git a/crawl-ref/source/effects.cc b/crawl-ref/source/effects.cc index d57421d1ae..6e98d0e4cf 100644 --- a/crawl-ref/source/effects.cc +++ b/crawl-ref/source/effects.cc @@ -2373,19 +2373,6 @@ void update_level(int elapsedTime) delete_cloud(i); } -// A comparison struct for use in an stl priority queue. -template<typename T> -struct greater_second -{ - // The stl priority queue is a max queue and uses < as the default - // comparison. We want a min queue so we have to use a > operation - // here. - bool operator()(const T & left, const T & right) - { - return left.second > right.second; - } -}; - // Basically we want to break a circle into n_arcs equal sized arcs and find // out which arc the input point pos falls on. static int _arc_decomposition(const coord_def & pos, int n_arcs) @@ -2459,7 +2446,8 @@ void collect_radius_points(vector<vector<coord_def> > &radius_points, // Using a priority queue because squares don't make very good circles at // larger radii. We will visit points in order of increasing euclidean - // distance from the origin (not path distance). + // distance from the origin (not path distance). We want a min queue + // based on the distance, so we use greater_second as the comparator. priority_queue<coord_dist, vector<coord_dist>, greater_second<coord_dist> > fringe; diff --git a/crawl-ref/source/libutil.h b/crawl-ref/source/libutil.h index 22c9daa888..01a2c20ab6 100644 --- a/crawl-ref/source/libutil.h +++ b/crawl-ref/source/libutil.h @@ -250,6 +250,17 @@ static inline void swapv(Z &a, Z &b) b = tmp; } +// A comparator for pairs. +template<typename T> +struct greater_second +{ + bool operator()(const T & left, const T & right) + { + return left.second > right.second; + } +}; + + static inline int sqr(int x) { return x * x; diff --git a/crawl-ref/source/mon-abil.cc b/crawl-ref/source/mon-abil.cc index 8415223829..fe4ff0b71d 100644 --- a/crawl-ref/source/mon-abil.cc +++ b/crawl-ref/source/mon-abil.cc @@ -1838,31 +1838,23 @@ static bool _lost_soul_affectable(const monster* mons) && !mons_class_flag(mons->type, M_NO_EXP_GAIN); } -struct hd_sorter -{ - bool operator()(const pair<monster* ,int> &a, const pair<monster* ,int> &b) - { - return a.second > b.second; - } -}; - static bool _lost_soul_teleport(monster* mons) { bool seen = you.can_see(mons); - vector<pair<monster*, int> > candidates; + typedef pair<monster*, int> mon_quality; + vector<mon_quality> candidates; // Assemble candidate list and randomize for (monster_iterator mi; mi; ++mi) { if (_lost_soul_affectable(*mi) && mons_aligned(mons, *mi)) { - pair<monster* , int> m = make_pair(*mi, min(mi->hit_dice, 18) - + random2(8)); + mon_quality m(*mi, min(mi->hit_dice, 18) + random2(8)); candidates.push_back(m); } } - sort(candidates.begin(), candidates.end(), hd_sorter()); + sort(candidates.begin(), candidates.end(), greater_second<mon_quality>()); for (unsigned int i = 0; i < candidates.size(); ++i) { diff --git a/crawl-ref/source/spl-other.cc b/crawl-ref/source/spl-other.cc index 3b2ca4fa7a..f259ae41ca 100644 --- a/crawl-ref/source/spl-other.cc +++ b/crawl-ref/source/spl-other.cc @@ -19,6 +19,7 @@ #include "itemname.h" #include "itemprop.h" #include "items.h" +#include "libutil.h" #include "makeitem.h" #include "message.h" #include "misc.h" @@ -181,14 +182,6 @@ spret_type cast_recall(bool fail) return SPRET_SUCCESS; } -struct recall_sorter -{ - bool operator()(const pair<mid_t,int> &a, const pair<mid_t,int> &b) - { - return a.second > b.second; - } -}; - // Type recalled: // 0 = anything // 1 = undead only (Yred religion ability) @@ -196,7 +189,8 @@ struct recall_sorter void start_recall(int type) { // Assemble the recall list. - vector<pair<mid_t, int> > rlist; + typedef pair<mid_t, int> mid_hd; + vector<mid_hd> rlist; you.recall_list.clear(); for (monster_iterator mi; mi; ++mi) @@ -215,7 +209,7 @@ void start_recall(int type) continue; } - pair<mid_t, int> m = make_pair(mi->mid, mi->hit_dice); + mid_hd m(mi->mid, mi->hit_dice); rlist.push_back(m); } @@ -227,7 +221,7 @@ void start_recall(int type) // Sort the recall list roughly by HD, randomizing a little for (unsigned int i = 0; i < rlist.size(); ++i) rlist[i].second += random2(10); - sort(rlist.begin(), rlist.end(), recall_sorter()); + sort(rlist.begin(), rlist.end(), greater_second<mid_hd>()); you.recall_list.clear(); for (unsigned int i = 0; i < rlist.size(); ++i) |