TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
filter.h
1 
2 /**
3  * Copyright (c) 2017-present, Facebook, Inc.
4  *
5  * This source code is licensed under the MIT license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 
11 namespace cherrypi {
12 namespace utils {
13 
14 template <typename Units, typename UnaryPredicate>
15 inline auto filterUnits(Units&& units, UnaryPredicate pred) {
16  std::vector<typename std::decay<Units>::type::value_type> result;
17  std::copy_if(units.begin(), units.end(), std::back_inserter(result), pred);
18  return result;
19 }
20 
21 template <typename Units, typename UnaryPredicate>
22 inline auto countUnits(Units&& units, UnaryPredicate pred) {
23  return std::count_if(units.begin(), units.end(), pred);
24 }
25 
26 inline std::vector<tc::Unit> filterUnitsByType(
27  std::vector<tc::Unit> const& units,
28  tc::BW::UnitType type) {
29  return filterUnits(
30  units, [type](tc::Unit const& u) { return u.type == type; });
31 }
32 
33 inline std::vector<tc::Unit> filterUnitsByTypes(
34  std::vector<tc::Unit> const& units,
35  std::vector<tc::BW::UnitType> const& types) {
36  return filterUnits(units, [types](tc::Unit const& u) {
37  for (auto type : types) {
38  if (u.type == type) {
39  return true;
40  }
41  }
42  return false;
43  });
44 }
45 
46 inline std::vector<tc::Unit> filterUnitsByType(
47  std::vector<tc::Unit> const& units,
48  std::function<bool(tc::BW::UnitType)> const& pred) {
49  return filterUnits(units, [pred](tc::Unit const& u) {
50  auto ut = tc::BW::UnitType::_from_integral_nothrow(u.type);
51  if (ut) {
52  return pred(*ut);
53  }
54  return false;
55  });
56 }
57 
58 } // namespace utils
59 } // namespace cherrypi
auto countUnits(Units &&units, UnaryPredicate pred)
Definition: filter.h:22
int32_t type
Definition: frame.h:87
auto filterUnits(Units &&units, UnaryPredicate pred)
Definition: filter.h:15
Definition: frame.h:81
std::vector< tc::Unit > filterUnitsByType(std::vector< tc::Unit > const &units, tc::BW::UnitType type)
Definition: filter.h:26
Main namespace for bot-related code.
Definition: areainfo.cpp:17
std::vector< tc::Unit > filterUnitsByTypes(std::vector< tc::Unit > const &units, std::vector< tc::BW::UnitType > const &types)
Definition: filter.h:33