1. Список говнокодов пользователя ngry

    Всего: 2

  2. C++ / Говнокод #6644

    +164

    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
    81. 81
    template < int Order, typename T >
    struct PrefixSum {
      static inline void update ( T* a ) throw () { *a += *(a-1); PrefixSum < Order-1, T > :: update( a+1 ); }
    };
    
    template < typename T >
    struct PrefixSum < 1,T > {
      static inline void update ( T* a ) throw () { *a += *(a-1); }
    };
    
    template < int P, int N, int Condition = 0 > 
    struct Bpn {
      enum { value = N * ( Bpn < P-1, N-1, (N > P) > :: value - Bpn < P-1, N, (N > P-1) > :: value ) };
    };
    
    template < int P, int N > struct Bpn < P, N, !0 > { enum { value = 0 }; };
    template < int P >        struct Bpn < P, 0,  0 > { enum { value = P ? 0 : 1 }; };
    template < int P >        struct Bpn < P, 1,  0 > { enum { value = P & 1 ? 1 : -1 }; };
    
    template < typename Ta, typename Tb, bool C > struct IfThenElse;
    template < typename Ta, typename Tb > struct IfThenElse < Ta, Tb, true  > { typedef Ta TRes; };
    template < typename Ta, typename Tb > struct IfThenElse < Ta, Tb, false > { typedef Tb TRes; };
    
    template < int K, int I = 1 >
    struct MomentSeries {
      typedef typename IfThenElse < MomentSeries<K,I+1>, MomentSeries<K,K>, (I<K) >::TRes SubT;
      static inline double accumulate ( double const* psum ) throw () {
        return Bpn < K, I > :: value * psum [ I + 1 ] + SubT :: accumulate ( psum );
      }
    };
    
    template < int K >
    struct MomentSeries < K, K > {
      static inline double accumulate ( double const* psum ) throw () {
        return Bpn < K, K > :: value * psum [ K + 1 ];
      }
    };
    
    template < int Order >
    struct MomentLoop {
      static inline void assign ( double *moments, size_t momentStride, double const* psum ) throw() {
        *(moments - momentStride) = MomentSeries < Order-1 > :: accumulate ( psum );
        MomentLoopAssign < Order-1 > :: assign ( moments - momentStride, momentStride, psum ); 
      }
    };
    
    template <>
    struct MomentLoop < 1 > {
      static inline void assign ( double *moments, size_t momentStride, double const* psum ) throw() {
        moments [ 0 ] = MomentSeries < 1, 1 > :: accumulate ( psum );
        *(moments - momentStride) = psum [ 1 ];
      }
    };
    
    /**
     * Function computes a series of geometric moments by prefix summation method:
     * Zhou F., Kornerup P. Computing Moments by Prefix Sums. // VLSI Signal Proc. - 2000. - 25. - P. 5 - 17.
     * @param data is first data elemet address.
     * @param ndataItems is number of data items.
     * @param dataStride is the number of elements between two neighbor data items, 
     *        in case of simple array dataStride is equal to 1.
     * @param moments is address of 0-order moment.
     * @param momentStride is number of elements between two neigbor moment items,
     *        in case of consequtive moments placement, momentStride is equal to 1.
     */
    
    template < int Order, class OutPolicy >
    inline void psmoment ( double const* data, 
                           size_t const  ndataItems,
                           size_t const  dataStride,
                           double*       moments,
                           size_t const  momentStride ) throw() { 
      double psum [ Order+1 ] = { 0 };
      // Initialize prefix sum
      for ( size_t i = ndataItems, j = ndataItems * dataStride; i; ) {
        --i; j -= dataStride; psum [ 0 ] = data [ j ];
        PrefixSum < Order, double > :: update ( psum+1 );
      }
      // convert psum to moment values 
      OutPolicy :: assign ( moments + Order * momentStride, momentStride, psum );
    }

    Вы - тестовая площадка, перед написанием статьи в пфп ;)

    ngry, 12 Мая 2011

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

    +144.8

    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
    /**
     * Helper classes for computing window query on rectangles
     */
    
    class VerticalSegmentIntersect : public std::unary_function < PRGlyph, ptrdiff_t > {
      ptrdiff_t m_Lhs;
      ptrdiff_t m_Rhs;
    public:
      VerticalSegmentIntersect ( ptrdiff_t top, ptrdiff_t bottom ) throw() : m_Lhs(top+top), m_Rhs(bottom+bottom) {}
      ptrdiff_t operator() ( PRGlyph inpGlyph ) const throw() { 
        QRect const* area = inpGlyph->GetGlyphArea();
        ptrdiff_t x = area->bottom() + area->top(), y = area->bottom() - area->top();
        
        if (y < x - m_Rhs ) return 0;
        if (y < m_Lhs - x ) return 0;
        
        return 1;
      }
    };
    
    class HorisontalSegmentIntersect : public std::unary_function < PRGlyph, ptrdiff_t > {
      ptrdiff_t m_Lhs;
      ptrdiff_t m_Rhs;
    public:
      HorisontalSegmentIntersect ( ptrdiff_t left, ptrdiff_t right ) throw() : m_Lhs(left+left), m_Rhs(right+right) {}
      ptrdiff_t operator() ( PRGlyph inpGlyph ) const throw() { 
        QRect const* area = inpGlyph->GetGlyphArea();
        ptrdiff_t x = area->right() + area->left(), y = area->right() - area->left();
        
        if (y < x - m_Rhs ) return 0;
        if (y < m_Lhs - x ) return 0;
        return 1;
      }
    };
    
    /**
     * Helper classes for computing containment query on rectangles
     */ 
    
    class VerticalSegmentContains : public std::unary_function < PRGlyph, ptrdiff_t > {
      ptrdiff_t m_Lhs;
      ptrdiff_t m_Rhs;
    public:
      VerticalSegmentContains ( ptrdiff_t top, ptrdiff_t bottom ) throw() : m_Lhs(top+top), m_Rhs(bottom+bottom) {}
      ptrdiff_t operator() ( PRGlyph inpGlyph ) const throw() {
        QRect const* area = inpGlyph->GetGlyphArea();    
        ptrdiff_t x = area->bottom() + area->top(), y = area->bottom() - area->top();
        
        if ( y > x - m_Lhs ) return 0;
        if ( y > m_Rhs - x ) return 0;
        return 1;
      }
    };
    
    class HorisontalSegmentContains : public std::unary_function < PRGlyph, ptrdiff_t > {
      ptrdiff_t m_Lhs;
      ptrdiff_t m_Rhs;
    public:
      HorisontalSegmentContains ( ptrdiff_t left, ptrdiff_t right ) throw() : m_Lhs(left+left), m_Rhs(right+right) {}
      ptrdiff_t operator() ( PRGlyph inpGlyph ) const throw() {
        QRect const* area = inpGlyph->GetGlyphArea();    
        ptrdiff_t x = area->right() + area->left(), y = area->right() - area->left();
        
        if ( y > x - m_Lhs ) return 0;
        if ( y > m_Rhs - x ) return 0;
        return 1;
      }
    };
    
    // compute the window query on m_GlyphData rectangles
      QVector<PRGlyph> :: iterator windowq = m_Selection.isValid() ?
                                            std::partition ( m_GlyphData.begin(),
                                                             std::partition ( m_GlyphData.begin(), m_GlyphData.end(), VerticalSegmentIntersect ( m_Selection.top(), m_Selection.bottom() ) ),
                                                             HorisontalSegmentIntersect ( m_Selection.left(), m_Selection.right() )
                                                           ) : m_GlyphData.begin();
      // compute the containment query on window query rectangles (the containment query resuls is always subset of window query )
      QVector<PRGlyph> :: iterator containq = std::partition ( m_GlyphData.begin(),
                                                               std::partition ( m_GlyphData.begin(), windowq, VerticalSegmentContains ( m_Selection.top(), m_Selection.bottom() ) ),
                                                               HorisontalSegmentContains ( m_Selection.left(), m_Selection.right() )
                                                             );

    Способ быстренько находить прямоугольники, пересекающиеся с входным и содержимые им же. Применимо для прямоугольных параллелепипедов любой размерности.

    ngry, 01 Апреля 2010

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