// Short-string optimization
constexpr size_t buffer_size = 16; // Slightly larger than the size of a pointer
class Immutable_string {
public:
Immutable_string(const char* str) :
size(strlen(str))
{
if (size < buffer_size)
strcpy_s(string_buffer, buffer_size, str);
else {
string_ptr = new char[size + 1];
strcpy_s(string_ptr, size + 1, str);
}
}
~Immutable_string()
{
if (size >= buffer_size)
delete[] string_ptr;
}
const char* get_str() const
{
return (size < buffer_size) ? string_buffer : string_ptr;
}
private:
// If the string is short enough, we store the string itself
// instead of a pointer to the string.
union {
char* string_ptr;
char string_buffer[buffer_size];
};
const size_t size;
};
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
using namespace std;
void RGB_to_HSL (double r,double g,double b, double *h,double *s,double *l)
{
double v;
double m;
double vm;
double r2, g2, b2;
v = max(r,g);
v = max(v,b);
m = min(r,g);
m = min(m,b);
if ((*l = (m + v) / 2.0) <= 0.0) return;
if ((*s = vm = v - m) > 0.0)
{
*s /= (*l <= 0.5) ? (v + m ) : (2.0 - v - m) ;
} else
return;
r2 = (v - r) / vm;
g2 = (v - g) / vm;
b2 = (v - b) / vm;
if (r == v)
*h = (g == m ? 5.0 + b2 : 1.0 - g2); else
if (g == v)
*h = (b == m ? 1.0 + r2 : 3.0 - b2);
else
*h = (r == m ? 3.0 + g2 : 5.0 - r2);
*h /= 6;
}
void HSL_to_RGB (double h,double sl,double l,double *r,double *g,double *b)
{
double v;
v = (l <= 0.5) ? (l * (1.0 + sl)) : (l + sl - l * sl);
if (v <= 0)
{
*r = *g = *b = 0.0;
}
else
{
double m;
double sv;
int sextant;
double fract, vsf, mid1, mid2;
m = l + l - v;
sv = (v - m ) / v;
h *= 6.0;
sextant = int (h);
fract = h - sextant;
vsf = v * sv * fract;
mid1 = m + vsf;
mid2 = v - vsf;
switch (sextant)
{
case 0: *r = v; *g = mid1; *b = m; break;
case 1: *r = mid2; *g = v; *b = m; break;
case 2: *r = m; *g = v; *b = mid1; break;
case 3: *r = m; *g = mid2; *b = v; break;
case 4: *r = mid1; *g = m; *b = v; break;
case 5: *r = v; *g = m; *b = mid2; break;
}
}
}
int main()
{
double r,g,b,h,s,l;
r=145;
g=129;
b=81;
RGB_to_HSL(r,g,b,&h,&s,&l);
cout<<h<<endl;
cout<<s<<endl;
cout<<l<<endl;
HSL_to_RGB (h,s,l,&r,&g,&b);
cout<<r<<endl;
cout<<g<<endl;
cout<<b<<endl;
return 0;
}
И теперь сверху жалуются, что мой код "слишком сишный", то есть написан на Си без плюсов, хоть я и использую классы. Проблема в том, что никто из коллег на работе не может нормально объяснить, как, собственно должен выглядеть "плюсовый" код.
int j = 0;
char s[] = "hello\n";
char ch;
while (s[j])
{
ch = s[j];
putchar(toupper(ch));
j++;
}
std::string s("hello");
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c) -> unsigned char { return std::toupper(c); });
const Polygon operator+( const Polygon& lhs,const Polygon& rhs )
{
Polygon ret = lhs;
const int last = rhs.GetNumPoints();
for( int i = 0; i < last; ++i ) // Конкатенация
{
ret.AddPoint( rhs.GetPoint(i) );
}
return ret;
}
и далее
внезапно да!