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

    +20

    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
    int spectrumColor(float magn)
    {
    	if (magn != magn) return 0xFF000000;
    	if ((magn <= -FLT_MAX && magn >= FLT_MAX)) return 0xFF000000;
    	const float minRange = -95.0f;
    	if (magn > minRange && magn <= (minRange + 10.0f))
    		return blendColor(0, 0x00004f, (magn - minRange) / 10.0f);
    	if (magn > (minRange + 10.0f) && magn <= (minRange + 20.0f))
    		return blendColor(0x00004f, 0x50007b, (magn - (minRange + 10.0f)) / 10.0f);
    	if (magn > (minRange + 20.0f) && magn <= (minRange + 30.0f))
    		return blendColor(0x50007b, 0x990076, (magn - (minRange + 20.0f)) / 10.0f);
    	if (magn > (minRange + 30.0f) && magn <= (minRange + 40.0f))
    		return blendColor(0x990076, 0xd20040, (magn - (minRange + 30.0f)) / 10.0f);
    	if (magn > (minRange + 40.0f) && magn <= (minRange + 50.0f))
    		return blendColor(0xd20040, 0xf51f00, (magn - (minRange + 40.0f)) / 10.0f);
    	if (magn > (minRange + 50.0f) && magn <= (minRange + 60.0f))
    		return blendColor(0xf51f00, 0xffaa00, (magn - (minRange + 50.0f)) / 10.0f);
    	if (magn > (minRange + 60.0f) && magn <= (minRange + 70.0f))
    		return blendColor(0xffaa00, 0xfff966, (magn - (minRange + 60.0f)) / 10.0f);
    	if (magn > (minRange + 70.0f) && magn <= (minRange + 80.0f))
    		return blendColor(0xfff966, 0xffffff, (magn - (minRange + 70.0f)) / 10.0f);
    	if (magn < minRange) return 0xFF000000;
    	if (magn >= (minRange + 80.0f)) return 0xFFFFFFFF;
    	return 0xFF000000;
    }

    Вот так в одной говноподелке считается цвет столбцов спектра для визуализации аудио.

    just_nameless, 25 Марта 2013

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

    +24

    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
    tmpPtr = SCPGetFreeTextMedicalHistory(ecg);
    if (!tmpPtr) tmpPtr = unsp;
    m_patientPage->SetDlgItemTextW(IDC_EDIT15, tmpPtr);
    if (tmpPtr != unsp) SCPFreeMem(tmpPtr);
    
    tmpPtr = SCPGetTechnicianDescription(ecg);
    if (!tmpPtr) tmpPtr = unsp;
    m_miscPage->SetDlgItemTextW(IDC_EDIT19, tmpPtr);
    if (tmpPtr != unsp) SCPFreeMem(tmpPtr);
    
    . . .
    
    tmpPtr = SCPGetLatestConfirmingPhysician(ecg);
    if (!tmpPtr) tmpPtr = unsp;
    m_miscPage->SetDlgItemTextW(IDC_EDIT18, tmpPtr);
    if (tmpPtr != unsp) SCPFreeMem(tmpPtr);
    
    tmpPtr = SCPGetReferringPhysician(ecg);
    if (!tmpPtr) tmpPtr = unsp;
    m_miscPage->SetDlgItemTextW(IDC_EDIT17, tmpPtr);
    if (tmpPtr != unsp) SCPFreeMem(tmpPtr);
    
    . . .

    just_nameless, 25 Марта 2013

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

    +9

    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
    void drawAAWave(DWORD *image, WAVE *w, int top, int left, 
    		int width, int height, int offsetPx)
    	{
    		int thd2 = top + height/2;
    		float hm2 = this->height - 2;
    		int x = 0, xc = 0, yc = 0;
    		float mul = amplify;
    		if (invert) mul = -mul;
    		if (x*scale + offsetPx*scale > w->length) return;
    		float yFrom = (thd2 - w->data[offsetPx*scale]*mul);
    		if (yFrom < 2.0f) yFrom = 2.0f;
    		if (yFrom > hm2) yFrom = hm2;
    		float yTo, dist, delta, sdelta, alpha, lc; 
    		int yMin, yMax, xcl, n;
    		for ( ; x < width-1 && (x+offsetPx)*scale < w->length; x++)
    		{
    			yTo = thd2 - w->data[(x + offsetPx)*scale]*mul;
    			if (yTo < 2) yTo = 2; if (yTo > hm2) yTo = hm2;
    			delta = yTo - yFrom;
    			sdelta = 1.0f / sqrtf(1.0f + delta*delta);
    			lc = ((x + 0.5f)*yTo - (x + 1.5f)*yFrom) + 0.5f;
    			yMin = (int)(min(yFrom, yTo)) - 1;
    			yMax = (int)(max(yFrom, yTo)) + 1;
    			xcl = x;
    			if (delta > 1.0f || delta < -1.0f) xcl++;
    			for (xc = x; xc <= xcl; xc++)
    				for (yc = yMin; yc <= yMax; yc++)
    				{
    					dist = ((-delta)*(xc + 0.5f) + yc + lc)*sdelta;
    					alpha = 1.0f - fabs(dist*0.8f);
    					if (alpha < 0.0f) alpha = 0.0f;
    					n = maxWidth*yc + xc;
    					image[n] = clrBlend(image[n], lineColor, alpha);
    				}
    			yFrom = yTo;
    		}
    	}

    Велосипедный метод рисования кривой с антиализингом

    just_nameless, 25 Марта 2013

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

    +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
    CompoundExpression*
    CompoundExpression::newBinaryExpression(
    	BasicBinaryOperation::Type operation,
    	const Expression *x,
    	const Expression *y
    ) {
    	vector<Expression::const_pointer> params(2);
    	params[0] = x;
    	params[1] = y;
    	
    	// integer power optimization
    	if (operation == BasicBinaryOperation::POWER) {
    		if (y->isNumber()) {
    			Number::const_pointer number_y = dynamic_cast<typeof number_y>(y);
    			if (number_y != NULL && number_y->isIntegerNumber()) {
    				IntegerNumber::const_pointer integer_y = dynamic_cast<typeof integer_y>(number_y);
    				if (integer_y != NULL) {
    					operation = BasicBinaryOperation::INT_POWER;
    					return new CompoundExpression(BinaryOperation::getOperation(operation), params);
    				}
    			}
    		}
    	}
    	
    	// x^(y/n), where 'n' is odd integer
    	// transform to '(x^y)^(1/n)'
    	if (operation == BasicBinaryOperation::POWER) {
    		if (y->isCompoundExpression()) {
    			auto compoundExpressionY = dynamic_cast<CompoundExpression::const_pointer>(y);
    			if (compoundExpressionY != NULL && compoundExpressionY->operation->isBinary()) {
    				auto innerOperation = compoundExpressionY->operation;
    				auto binaryOperation = dynamic_cast<BinaryOperation const *>(innerOperation);
    				if (binaryOperation != NULL && binaryOperation->getType() == BasicBinaryOperation::DIVIDE) {
    					Expression::const_pointer   numerator = compoundExpressionY->params[0];
    					Expression::const_pointer denominator = compoundExpressionY->params[1];
    					if (denominator->isNumber()) {
    						auto numberDenominator = dynamic_cast<Number::const_pointer>(denominator);
    						if (numberDenominator != NULL && numberDenominator->isIntegerNumber()) {
    							auto integerDenominator = dynamic_cast<IntegerNumber::const_pointer>(numberDenominator);
    							if (integerDenominator != NULL && (integerDenominator->intValue() % 2) != 0) {
    								auto base = CompoundExpression::newBinaryExpression(BasicBinaryOperation::POWER, x, numerator);
    								return CompoundExpression::newBinaryExpression(BasicBinaryOperation::NTH_ROOT, integerDenominator, base);
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    
    	return new CompoundExpression(BinaryOperation::getOperation(operation), params);
    }

    Моё. Потребовалось воткнуть оптимизацию арифметического выражения некоторого вида. В результате родился вот такой костыль.

    UncleAli, 24 Марта 2013

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

    +19

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    struct BufInfo 
    {
    	const tbal::Bitmap &src, &dst;
    	int y1, y2;
    	BufInfo (const tbal::Bitmap &scr, const tbal::Bitmap &dst, int y1, int y2) : src(src), dst(dst), y1(y1), y2(y2) {}
    };

    Как можно проебать час жизни...

    TarasB, 20 Марта 2013

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

    +14

    1. 1
    2. 2
    3. 3
    4. 4
    template<typename T>
    constexpr size_t printed_sizeof() {
        return log10(sizeof(T)) + 1;
    }

    Осваиваем новые стандарты.

    roman-kashitsyn, 19 Марта 2013

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

    +20

    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
    #define STR(a) #a
    
    #define EXCEPTION_CLASS_CREATE(a)                   \
    class a : public std::exception                     \
    {                                                   \
    public:                                             \
        a()                                             \
        {                                               \
            d(STR(a)"\n");                              \
        }                                               \
                                                        \
        a(const char * format, ...)                     \
        {                                               \
            char buffer[ 1024 ];                        \
            va_list vl;                                 \
            va_start( vl, format );                     \
            vsnprintf( buffer, sizeof(buffer), format, vl ); \
            va_end( vl );                               \
            _str.append( buffer );                      \
            d(STR(a)" %s\n", buffer);                   \
        }                                               \
                                                        \
        ~a() throw()                                    \
        {                                               \
        }                                               \
                                                        \
        const char* what() const throw()                \
        {                                               \
            return _str.c_str();                        \
        }                                               \
    private:                                            \
        std::string _str;                               \
    };
    // ...
    EXCEPTION_CLASS_CREATE( InternalException )

    Мы очень любим varargs

    roman-kashitsyn, 19 Марта 2013

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

    +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
    class session {
    public:
        session(int id, boost::asio::io_service &io_service) :
            id(id),
            timer(io_service)
        {
            timer.expires_from_now(session_timeout);
            timer.async_wait(boost::bind(&session::on_timeout, this, _1));
        }
    
        void on_timeout(const boost::system::error_code &error) {
            if (error)
                return;
            std::cout << "Session timed out " << id << std::endl;
        }
    
    private:
        int id;
        boost::asio::deadline_timer timer;
    };
    
    std::map<boost::asio::ip::udp::endpoint, boost::shared_pointer<session> > sessions;

    sessions.erase(endpoint) приводит к небольшому насилию над трупом сессии... Ничего конечно не вылетает, и никогда не сломается, но совесть мучает, неприятно пользоваться UB'ом.

    bormand, 18 Марта 2013

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

    +15

    1. 1
    2. 2
    3. 3
    String testName;
    //...
    std::swap(testName,  _testName);

    String из thirdparty-библиотеки, а swap везде в нашем коде. По очевидным причинам получаем подение производительности.

    LispGovno, 16 Марта 2013

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

    +23

    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
    struct mystream: public std::ostream 
    {
        mystream(std::ostream & o): std::ostream(o.rdbuf()) {}
    
        template <class T>
        mystream & operator << (T const & arg)
        {
            if (enabled_) as_std_ostream() << arg;
            return *this;
        }
    
        // дерьмо STX
        mystream & operator << (std::ostream & (*f)(std::ostream &))
        {
            if (enabled_) as_std_ostream() << f;
            return *this;
        }
    
        mystream & operator << (std::ios & (*f)(std::ios &))
        {
            if (enabled_) as_std_ostream() << f;
            return *this;    
        }
    
        mystream & operator << (std::ios_base & (*f)(std::ios_base &))
        {
            if (enabled_) as_std_ostream() << f;
            return *this;
        }
        // дерьмо ETX
    
        void enable() { enabled_ = true; }
        void disable() { enabled_ = false; }
    
    protected:
        bool enabled_;
        std::ostream & as_std_ostream() { return *this; }
    };

    а так хотелось хоть сегодня рыбки поесть захерачить вместо трёх перегрузок
    template <class O>
    mystream & operator << (O & (*f)(O &)) { ...

    defecate-plusplus, 13 Марта 2013

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