1. C++ / Говнокод #12956

    +10

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    static int
          _S_compare(size_type __n1, size_type __n2)
          {
      const difference_type __d = difference_type(__n1 - __n2);
    
      if (__d > __gnu_cxx::__numeric_traits<int>::__max)
        return __gnu_cxx::__numeric_traits<int>::__max;
      else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
        return __gnu_cxx::__numeric_traits<int>::__min;
      else
        return int(__d);
          }

    LispGovno, 01 Мая 2013

    Комментарии (20)
  2. C++ / Говнокод #12947

    +13

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    struct read_access_tag {};
    struct write_access_tag {};
    struct read_write_access_tag : read_access_tag, write_access_tag {};
    
    template <
        typename ByteOrder,
        typename ValueType
    >
    std::size_t get_from(const uint8_t *src, ValueType &dst, const read_access_tag&) {
        ByteOrder::decode(src, dst);
        return sizeof(dst);
    }
    
    template <
        typename ByteOrder,
        typename ValueType
    >
    std::size_t put_into(ValueType src, uint8_t *dst, const write_access_tag&) {
        ByteOrder::encode(src, dst);
        return sizeof(src);
    }
    
    // ...
    
    template <
        typename ByteOrder = default_byte_order,
        typename AccessTag = read_write_access_tag
        >
    class basic_buffer {
    public:
        typedef ByteOrder byte_order;
        typedef AccessTag access_tag;
        typedef typename access_traits<access_tag>::value_type value_type;
        typedef typename access_traits<access_tag>::strict_type strict_type;
        typedef typename access_traits<access_tag>::iterator iterator;
        typedef typename access_traits<access_tag>::const_iterator const_iterator;
    
        basic_buffer(iterator begin, iterator end)
            : begin_(begin)
            , end_(end)
            , pos_(begin)
        {}
    
        // ...
    
        template <typename T>
        basic_buffer & put(T value) {
            if (bytes_left() < sizeof(value)) throw Overflow;
            pos_ += put_into<byte_order>(value, pos_, access_tag());
            return *this;
        }
    
        template <typename T>
        basic_buffer & get(T &value) {
            if (bytes_left() < sizeof(value)) throw Overflow;
            pos_ += get_from<byte_order>(pos_, value, access_tag());
            return *this;
        }
    }

    Развитие идей из

    http://govnokod.ru/12465
    Изобретаем права доступа в compile time, чтобы можно было запретить писать в readonly-буфер и читать из writeonly-буфера без дупликации кода. put_into по сути не нужен (запись в readonly_buffer у меня и без этого не скомпилится), существует из соображений симметрии. Полный код здесь
    https://github.com/roman-kashitsyn/encoding-binary

    roman-kashitsyn, 28 Апреля 2013

    Комментарии (11)
  3. C++ / Говнокод #12938

    +17

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    #include<iostream>
    using namespace std;
    int main(){
    	int n,a[100100],d[100100],ans=d[0]=1,i,j;
    	cin>>n>>a[0];
    	for(i = 1;i<n;++i)
    		for(j =i-1,cin>>a[i],d[i]=1;j>=0;--j) 
    			if(a[i]>a[j]) ans = max(ans, d[i]=max(d[i],d[j]+1));
    	cout<<ans;
    }

    Решение задачи нахождения НВП (наибольшей возр. подпосл-ти)

    AvadaKedavra, 26 Апреля 2013

    Комментарии (7)
  4. C++ / Говнокод #12937

    +12

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    namespace predicate {
    
    using ...;
    
    typedef boost::function<bool (const object &obj)> bool_func;
    typedef boost::function<int (const object &obj)> int_func;
    
    // ... скучные реализации операторов ...
    
    template <class I, class S> struct predicate_grammar :
        qi::grammar<I, bool_func(), S>
    {
        predicate_grammar() : predicate_grammar::base_type(bool_expr)
        {
            identifier = char_("a-z") >> *(char_("a-z_0-9"));
            bool_prop = identifier [ _val = bind(&make_bool_prop_reader, _1, _pass) ];
            bool_expr = (bool_expr2 >> "||" >> bool_expr) [ _val = bind(&make_logic_op, &op_or, _1, _2) ]
                      | bool_expr2 [ _val = _1 ];
            bool_expr2 = (bool_expr3 >> "&&" >> bool_expr2) [ _val = bind(&make_logic_op, &op_and, _1, _2) ]
                       | bool_expr3 [ _val = _1 ];
            bool_expr3 = ('(' >> bool_expr >> ')') [ _val = _1 ]
                       | ('!' >> bool_expr3) [ _val = bind(&make_not, _1) ]
                       | int_comp [ _val = _1 ]
                       | bool_prop [ _val = _1];
            int_comp = (int_expr >> "<" >> int_expr) [ _val = bind(&make_cmp_op, &op_less, _1, _2) ]
                     | (int_expr >> "<=" >> int_expr) [ _val = bind(&make_cmp_op, &op_less_eq, _1, _2) ]
                     | (int_expr >> ">" >> int_expr) [ _val = bind(&make_cmp_op, &op_greater, _1, _2) ]
                     | (int_expr >> ">=" >> int_expr) [ _val = bind(&make_cmp_op, &op_greater_eq, _1, _2) ]
                     | (int_expr >> "==" >> int_expr) [ _val = bind(&make_cmp_op, &op_eq, _1, _2) ]
                     | (int_expr >> "!=" >> int_expr) [ _val = bind(&make_cmp_op, &op_not_eq, _1, _2) ];
            int_expr = int_prop [ _val = _1 ]
                     | int_const [ _val = bind(&make_int_const, _1) ];
            int_const = int_ [ _val = _1 ];
            int_prop = identifier [ _val = bind(&make_int_prop_reader, _1, _pass) ];
        }
    
        qi::rule<I, std::string(), S> identifier;
        qi::rule<I, int(), S> int_const;
        qi::rule<I, int_func(), S> int_expr, int_prop;
        qi::rule<I, bool_func(), S> bool_expr, bool_expr2, bool_expr3, int_comp, bool_prop;
    };
    
    boost::function<bool (const object &)> parse(const std::string &src) {
        if (src.empty())
            return make_bool_const(true);
        bool_func p;
        std::string::const_iterator b = src.begin(), e = src.end();
        predicate_grammar<std::string::const_iterator, boost::spirit::ascii::space_type> grammar;
        if (!phrase_parse(b, e, grammar, boost::spirit::ascii::space, p) || b != e) {
            std::stringstream s;
            s << "Predicate parsing failed at " << (b - src.begin()) << " in \"" << src << "\"";
            throw std::runtime_error(s.str());
        }
        return p;
    }

    Обещанный в http://govnokod.ru/12936#comment175980 говнокодец с использованием бусто-духа.

    bormand, 26 Апреля 2013

    Комментарии (28)
  5. C++ / Говнокод #12936

    +18

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    class Mutex
    {
      HANDLE m_Mutex;
    public:
      Mutex()
      {
        m_Mutex = CreateMutex(0, false, 0);
      }
    
      ~Mutex()
      {
        ReleaseMutex(m_Mutex);
        CloseHandle(m_Mutex);
      }
    
      void Lock()
      {
        auto lv_WaitResult = WaitForSingleObject(m_Mutex, 10000);
        assert(lv_WaitResult != WAIT_TIMEOUT);
      }
    
      void Unlock()
      {
        ReleaseMutex(m_Mutex);
      }
    };
    
    
    template<int C>
    struct GLocker
    {
      static Mutex m_Mutex;
    };
    template<int C>  Mutex GLocker<C>::m_Mutex;
    
    class Locker
    {
      Mutex *m_Mutex;
    
    public:
      Locker(Mutex &_Mutex)
      {
        m_Mutex = &_Mutex;
        m_Mutex->Lock();
      }
    
      virtual ~Locker()
      {
        m_Mutex->Unlock();
      }
    };
    
    
    class ThreadCreator
    {
    protected:
      static void NewThread(void *_Func)
      {
        (*(std::function<void()>*)_Func)();
        delete (std::function<void()>*)_Func;
      }
    
    public:
      static ThreadCreator g_ThreadCreator;
    
      HANDLE operator=(const std::function<void()> &_Func)
      {
        std::function<void()> *lv_Func = new std::function<void()>(_Func);
        return (HANDLE)_beginthread(ThreadCreator::NewThread,0,(void*)lv_Func);
      }
    };
    ThreadCreator ThreadCreator::g_ThreadCreator;
    
    
    #define LOCK const Locker  _Lock##__LINE__##__COUNTER__ = GLocker<9000 + __COUNTER__>::m_Mutex // 9000 является запасом для пользовательских номеров.
    #define LOCK_EX(Port) const Locker  _Lock##__LINE__##__COUNTER__ = GLocker<Port>::m_Mutex // блокирует определённый мьютекс, полезно когда нужно делать синхронизацию из разных мест.
    
    #define GO_FUNC ThreadCreator::g_ThreadCreator = 
    #define GO_EX(...) GO_FUNC  [__VA_ARGS__]() mutable -> void
    #define GO GO_FUNC  []() -> void

    Подсказка сайта:
    http://www.gamedev.ru/code/tip/tread_creation

    LispGovno, 25 Апреля 2013

    Комментарии (26)
  6. C++ / Говнокод #12924

    +16

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    #define TYPE_INIT_(_Number, _Line, _Type,...) struct ln_##_Line##_number_##_Number : public _Type\
    {\
      ln_##_Line##_number_##_Number()\
        :_Type(__VA_ARGS__){}\
    \
      ln_##_Line##_number_##_Number(const _Type& _val)\
        :_Type(_val){}\
    \
      const _Type& operator=(const _Type& _val)\
      {\
        ((_Type)(*this)) = _val;\
        return (*this);\
      }\
    }
    #define TYPE_INIT(_Number, _Line, _Type,...)  TYPE_INIT_(_Number, _Line, _Type, __VA_ARGS__)
    #define INIT(_Type, ...) TYPE_INIT(__COUNTER__, __LINE__, _Type, __VA_ARGS__)
    
    // использование
    
    struct ABC
    {
      int A,B,C;
      
      ABC(int _A,int _B,int _C)
        :A(_A),
        B(_B),
        C(_C)
      {}
    };
    
    
    struct TestClass
    {
      INIT( string, "Ololo" ) lv_String;
      INIT( ABC, 1, 2, 3 ) lv_ABC;
    };

    Оттуда.

    LispGovno, 20 Апреля 2013

    Комментарии (18)
  7. C++ / Говнокод #12911

    +13

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    void CMainWindow::OnTimer(UINT_PTR id){
    	switch(id){
    	case IDT_TIMER1:
    		CClientDC dc(this);
    		dc.SetMapMode(MM_ISOTROPIC);
    		GetClientRect(&rcClient);
    		dc.SetWindowExt(CONSTANT,CONSTANT);
    		dc.SetViewportExt(rcClient.right,rcClient.bottom);
    		dc.SelectObject(GetStockObject(NULL_BRUSH));
    		dc.SetROP2(R2_NOTXORPEN);
    		wsprintf(buff,L"Score: %d       ", score);
    		dc.TextOut(5,5,buff,15);
    		if(!won || resized)dc.Ellipse(ptsBegin0.x-RAD,ptsBegin0.y-RAD,ptsBegin0.x+RAD,ptsBegin0.y+RAD);
    		ptsBegin0.x = rand() % (CONSTANT-RAD) + RAD;
    		ptsBegin0.y = rand() % (CONSTANT -RAD)+ RAD;
    		dc.DPtoLP(&ptsBegin0,1);
    		dc.Ellipse(ptsBegin0.x-RAD,ptsBegin0.y-RAD,ptsBegin0.x+RAD,ptsBegin0.y+RAD);
    		if(!won || resized)dc.Ellipse(ptsEnd0.x-RAD,ptsEnd0.y-RAD,ptsEnd0.x+RAD,ptsEnd0.y+RAD);
    		ptsEnd0.x = rand() % (CONSTANT - RAD) + RAD;
    		ptsEnd0.y = rand() % (CONSTANT - RAD) + RAD;
    		dc.DPtoLP(&ptsEnd0,1);
    		dc.Ellipse(ptsEnd0.x-RAD,ptsEnd0.y-RAD,ptsEnd0.x+RAD,ptsEnd0.y+RAD);
    		won = resized = false;
    	}
    }

    Abbath, 18 Апреля 2013

    Комментарии (0)
  8. C++ / Говнокод #12902

    +7

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    MashTextGen::MashTextGen()
    {
    	files.push_back("BarButton.cpp");
    	files.push_back("CreateBar.cpp");
    	files.push_back("Eventfulness.cpp");
    	files.push_back("Glob.cpp");
    	files.push_back("main.cpp");
    	files.push_back("mashtextgen.cpp");
    	files.push_back("MashWindow.cpp");
    	files.push_back("Note.cpp");
    	files.push_back("NotificationBar.cpp");
    	files.push_back("Render.cpp");
    	files.push_back("RogueWindow.cpp");
    	files.push_back("WorldWindow.cpp");
    	files.push_back("BarButton.h");
    	files.push_back("CreateBar.h");
    	files.push_back("Eventfulness.h");
    	files.push_back("Glob.h");
    	files.push_back("main.h");
    	files.push_back("mashtextgen.h");
    	files.push_back("MashWindow.h");
    	files.push_back("Note.h");
    	files.push_back("NotificationBar.h");
    	files.push_back("Render.h");
    	files.push_back("RogueWindow.h");
    	files.push_back("WorldWindow.h");
    	
    	LoadMoreData();
    	picker = 0;
    }

    Я так и не понял зачем этот код.
    Найдено в опенсорсной игре.

    x0m9k, 17 Апреля 2013

    Комментарии (30)
  9. C++ / Говнокод #12898

    +15

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    for(std::list<Eff_t*>::iterator i = m_effects.begin(); i != m_effects.end(); ++i)
    	{
    		Rot3DEff_t* pRot3DEff = dynamic_cast<Rot3DEff_t*>(*i); 
    		//иначе вместо деструктра Rot3DEff_t вызывается деструктор Eff_t
    		//если этого не делать не освободится текстура m_pText класса Rot3DEff_t
    		if (pRot3DEff)
    			delete pRot3DEff;
    		else
    			delete *i;
    	}

    lifemaker, 16 Апреля 2013

    Комментарии (5)
  10. C++ / Говнокод #12874

    +13

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    std::string StringUtilities::replace(const std::string& strValue,
                                         uint8_t piWhat,
                                         uint8_t piWith)
    {  
      size_t len = strValue.length();
      uint8_t* lTemp = new uint8_t[len + 1];
      memset(lTemp, '\0', len + 1); //+ 1 for \0
      memcpy(lTemp, strValue.c_str(), len);
      for (size_t i = 0; i < len; i++)
      {
        if (lTemp[i] == piWhat)
          lTemp[i] = piWith;
      }
      return string( (int8_t*) lTemp );  
    }

    Любители велосипедов...

    blackhearted, 10 Апреля 2013

    Комментарии (12)