免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6228 | 回复: 1

好可怕的template [复制链接]

论坛徽章:
0
发表于 2012-02-19 11:47 |显示全部楼层
高人们解析一下,呵呵。这个chess engine源码g下就有,非常有名:
  1. /*
  2.   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
  3.   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
  4.   Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad

  5.   Stockfish is free software: you can redistribute it and/or modify
  6.   it under the terms of the GNU General Public License as published by
  7.   the Free Software Foundation, either version 3 of the License, or
  8.   (at your option) any later version.

  9.   Stockfish is distributed in the hope that it will be useful,
  10.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.   GNU General Public License for more details.

  13.   You should have received a copy of the GNU General Public License
  14.   along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15. */

  16. #if !defined(ENDGAME_H_INCLUDED)
  17. #define ENDGAME_H_INCLUDED

  18. #include <map>
  19. #include <string>

  20. #include "position.h"
  21. #include "types.h"


  22. /// EndgameType lists all supported endgames

  23. enum EndgameType {

  24.   // Evaluation functions

  25.   KXK,   // Generic "mate lone king" eval
  26.   KBNK,  // KBN vs K
  27.   KPK,   // KP vs K
  28.   KRKP,  // KR vs KP
  29.   KRKB,  // KR vs KB
  30.   KRKN,  // KR vs KN
  31.   KQKR,  // KQ vs KR
  32.   KBBKN, // KBB vs KN
  33.   KNNK,  // KNN vs K
  34.   KmmKm, // K and two minors vs K and one or two minors


  35.   // Scaling functions
  36.   SCALE_FUNS,

  37.   KBPsK,   // KB+pawns vs K
  38.   KQKRPs,  // KQ vs KR+pawns
  39.   KRPKR,   // KRP vs KR
  40.   KRPPKRP, // KRPP vs KRP
  41.   KPsK,    // King and pawns vs king
  42.   KBPKB,   // KBP vs KB
  43.   KBPPKB,  // KBPP vs KB
  44.   KBPKN,   // KBP vs KN
  45.   KNPK,    // KNP vs K
  46.   KPKP     // KP vs KP
  47. };


  48. /// Some magic to detect family type of endgame from its enum value

  49. template<bool> struct bool_to_type { typedef Value type; };
  50. template<> struct bool_to_type<true> { typedef ScaleFactor type; };
  51. template<EndgameType E> struct eg_family : public bool_to_type<(E > SCALE_FUNS)> {};


  52. /// Base and derived templates for endgame evaluation and scaling functions

  53. template<typename T>
  54. struct EndgameBase {

  55.   virtual ~EndgameBase() {}
  56.   virtual Color color() const = 0;
  57.   virtual T operator()(const Position&) const = 0;
  58. };


  59. template<EndgameType E, typename T = typename eg_family<E>::type>
  60. struct Endgame : public EndgameBase<T> {

  61.   explicit Endgame(Color c) : strongerSide(c), weakerSide(~c) {}
  62.   Color color() const { return strongerSide; }
  63.   T operator()(const Position&) const;

  64. private:
  65.   Color strongerSide, weakerSide;
  66. };


  67. /// Endgames class stores in two std::map the pointers to endgame evaluation
  68. /// and scaling base objects. Then we use polymorphism to invoke the actual
  69. /// endgame function calling its operator() method that is virtual.

  70. class Endgames {

  71.   typedef std::map<Key, EndgameBase<Value>*> M1;
  72.   typedef std::map<Key, EndgameBase<ScaleFactor>*> M2;

  73.   M1 m1;
  74.   M2 m2;

  75.   M1& map(Value*) { return m1; }
  76.   M2& map(ScaleFactor*) { return m2; }

  77.   template<EndgameType E> void add(const std::string& code);

  78. public:
  79.   Endgames();
  80.   ~Endgames();

  81.   template<typename T> EndgameBase<T>* get(Key key) {
  82.     return map((T*)0).count(key) ? map((T*)0)[key] : NULL;
  83.   }
  84. };

  85. #endif // !defined(ENDGAME_H_INCLUDED)
复制代码

论坛徽章:
0
发表于 2012-02-20 09:54 |显示全部楼层
boost::auto的扩展再应用.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

SACC2019中国系统架构师大会

【数字转型 架构演进】SACC2019中国系统架构师大会,7折限时优惠重磅来袭!
2019年10月31日~11月2日第11届中国系统架构师大会(SACC2019)将在北京隆重召开。四大主线并行的演讲模式,1个主会场、20个技术专场、超千人参与的会议规模,100+来自互联网、金融、制造业、电商等领域的嘉宾阵容,将为广大参会者提供一场最具价值的技术交流盛会。

限时七折期:2019年8月31日前


----------------------------------------

大会官网>>
  

北京盛拓优讯信息技术有限公司. 版权所有 16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122
中国互联网协会会员  联系我们:huangweiwei@it168.com
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP