Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SgUtil.h

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file SgUtil.h
00003     Small utility functions
00004 */
00005 //----------------------------------------------------------------------------
00006 
00007 #ifndef SG_UTIL_H
00008 #define SG_UTIL_H
00009 
00010 #include <iostream>
00011 #include "SgBlackWhite.h"
00012 #include "SgBWArray.h"
00013 //----------------------------------------------------------------------------
00014 
00015 namespace SgUtil {
00016 
00017 template<class T>
00018 inline void ForceInRange(const T& min, T* p, const T& max)
00019 {
00020     if (*p < min)
00021         *p = min;
00022     if (*p > max)
00023         *p = max;
00024 }
00025 
00026 template <class T>
00027 inline bool InRange(const T& i, const T& from, const T& to) 
00028 {
00029     return (i >= from) && (i <= to);
00030 }
00031 
00032 template<class T>
00033 inline void LowerLimit(T& x, const T& limit)
00034 {
00035     if (x < limit)
00036         x = limit;
00037 }
00038 
00039 template<class T>
00040 inline void UpperLimit(T& x, const T& limit)
00041 {
00042     if (x > limit)
00043         x = limit;
00044 }
00045 
00046 } // namespace SgUtil
00047 
00048 /** Utility class to assure balance between black and white plays.
00049     The difference between the number of plays by both colors
00050     is forced to be within the margin.
00051 */
00052 class SgBalancer
00053 {
00054 public:
00055     SgBalancer(int margin) : 
00056         m_balance(0),
00057         m_nuCalls(0),
00058         m_margin(margin),
00059         m_played(0,0),
00060         m_rejected(0,0)
00061     { }
00062     
00063     bool Play(SgBlackWhite color)
00064     {
00065         SG_ASSERT(SgIsBlackWhite(color));
00066         ++m_nuCalls;
00067         if (color == SG_BLACK)
00068         {
00069             if (m_balance < m_margin)
00070             {
00071                 ++m_balance;
00072                 ++m_played[SG_BLACK];
00073                 return true;
00074             }
00075         }
00076         else if (m_balance > -m_margin)
00077         {
00078             --m_balance;
00079             ++m_played[SG_WHITE];
00080             return true;
00081         }
00082         ++m_rejected[color];
00083         return false;
00084     }
00085     
00086     int Balance() const
00087     {
00088         return m_balance;
00089     }
00090     
00091     int Margin() const
00092     {
00093         return m_margin;
00094     }
00095     
00096     int NuCalls() const
00097     {
00098         return m_nuCalls;
00099     }
00100     
00101     int NuPlayed(SgBlackWhite color) const
00102     {
00103         return m_played[color];
00104     }
00105     
00106     int NuRejected(SgBlackWhite color) const
00107     {
00108         return m_rejected[color];
00109     }
00110     
00111 private:
00112     int m_balance;
00113     int m_nuCalls;
00114     const int m_margin;
00115     SgBWArray<int> m_played;
00116     SgBWArray<int> m_rejected;
00117 };
00118 
00119 //----------------------------------------------------------------------------
00120 
00121 std::ostream& operator<<(std::ostream& stream, const SgBalancer& balancer);
00122 
00123 //----------------------------------------------------------------------------
00124 
00125 #endif // SG_UTIL_H
00126 


17 Jun 2010 Doxygen 1.4.7