- 1
- 2
- 3
- 4
- 5
- 6
void sleep_in_qt_ms(unsigned millisec) {
QMutex foo;
foo.lock();
foo.try_lock(millisec);
foo.unlock();
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+24
void sleep_in_qt_ms(unsigned millisec) {
QMutex foo;
foo.lock();
foo.try_lock(millisec);
foo.unlock();
}
sleep в Qt - что, серьезно, чтоли?
особенно порадовало: Warning: Destroying a locked mutex may result in undefined behavior.
действительно, накой нам деструкторы?
+11
void Bitmap::flipRB()
{
int nBytes = w*h;
void *d = data;
__asm
{
mov edx, 0;
pixloop: mov eax, d;
mov eax, [eax];
mov ebx, edx;
shl ebx, 0x2;
add eax, ebx;
mov ebx, [eax];
and ebx, 0xFFFFFF;
mov ecx, ebx;
shr ebx, 0x10;
shl ecx, 0x10;
or ebx, ecx;
shr ecx, 0x10;
and ecx, 0xFF00;
or ebx, ecx;
and ebx, 0xFFFFFF;
or ebx, 0xFF000000;
mov [eax], ebx;
inc edx;
cmp edx, nBytes;
jne pixloop;
}
}
Откопал очередной свой велосипедный класс, в нем попался такой вот метод для конвертирования цветов ARGB в ABGR.
+3
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;
const int n=20;
int mass[20];
void massiv(int x[])
{
srand(time(NULL));
for(int i=0;i<20;i++) mass[i]=rand()%100;
return 0;
}
void vivod(int x[])
{
for(int i=0;i<20;i++) cout<<mas[i]<<"\n";
return 0;
}
void sort (int x[])
{
int i,j,b;
for(j=1;j<=n-1;j++)
for(i=0;i<=n-1-j;i++)
if (mass[i]>mass[i+1])
{
b=mass[i];
mass[i]=mass[i+1];
mass[i+1]=b;
}
}
return 0;
}
int main(int argc, char *argv[])
{
zapolneniemassiva(mass[20]);
pokaz(mass[20]);
sort(mass[20]);
variant3(mass[20]);
system("PAUSE");
return EXIT_SUCCESS;
}
Одногруппник сделал лабу на C++, программа сортирует массив по возрастанию и выводит, ну, по крайней мере, должна.
+1
int main()
{
__asm()
{
NOP
}
return 0;
}
программа делает ничего, но делает
+11
typedef ::boost::array<T, item_amount+1> not_raw_array;
union data
{
not_raw_array m_raw_array;//Most members of boost::array not suitable for call with raw array. Be careful!
};
+20
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;
}
Вот так в одной говноподелке считается цвет столбцов спектра для визуализации аудио.
+24
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);
. . .
+9
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;
}
}
Велосипедный метод рисования кривой с антиализингом
+13
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);
}
Моё. Потребовалось воткнуть оптимизацию арифметического выражения некоторого вида. В результате родился вот такой костыль.
+19
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) {}
};
Как можно проебать час жизни...