In risposta ai (fantastici) golf di orlp
:
La correttezza deve venire prima di tutto
- La maggior parte di questi si scompone per alcuni tipi interi. Ciò include la versione dall'OP
- È interessante notare che essi fanno il lavoro per
int16_t
- per cui v'è il presupposto. Probabilmente i bit shift avrebbero bisogno di +16 per 32 bit ints (è praticamente ovunque in questi giorni). Questo li rende un personaggio più grande ...
L'unico modo "corretto" per scriverlo, l'IMO è (x>3) && (x > y+1)
, che può essere ridotto a x>3&x>y+1
(9 caratteri).
(Devi davvero prendere in considerazione la possibilità di tipi (più grandi) senza segno, specialmente perché la non-firma è "contagiosa" nelle espressioni C ++. Suppongo che "aggiustare" che con gli static_cast<>
s appropriati possa un po 'vanificare lo scopo ...)
AGGIORNARE
Con i seguenti test sono stato in grado di capire quali espressioni funzionano effettivamente in modo affidabile:
Live On Coliru
#define REPORT(caption, expr) do {\
do_report(caption, [](T x, T y) -> bool { return (expr); }, #expr); } while (false)
template <typename T> struct driver {
static void run() {
std::cout << "\n" << __PRETTY_FUNCTION__ << "\n";
// the only two correct implementations:
REPORT("MASTER" , (x>3) && (x>y+1));
REPORT("GOLF" , x>3&x>y+1);
REPORT("lookup" , "000000000000000000000000111000111100"[x*6+y]-'0');
// failing sometimes:
REPORT("question", (x>3)&(x-y>1));
REPORT("orlp0" , x>3&x-y>1);
REPORT("orlp2" , ~y+x>2>>y);
REPORT("orlp3" , x*x-y*y>9);
REPORT("orlp4" , ~y>x/~3*x);
REPORT("orlp5" , -3>>y>y-x);
REPORT("orlp6" , ~y+x<<y>2);
// failing always
REPORT("orlp1" , -x<~y>4>x);
}
private:
static void do_report(std::string const& caption, bool (*f)(T,T), char const* expression) {
std::string r;
for (T x = 0; x < 6; ++x) for (T y = 0; y < 6; ++y) r += f(x, y)?'1':'0';
bool const correct = "000000000000000000000000111000111100" == r;
std::cout << (correct?"OK\t":"ERR\t") << r << "\t" << caption << "\t" << expression << "\n";
}
};
int main() {
driver<int8_t>::run();
driver<int16_t>::run();
driver<int32_t>::run();
driver<int64_t>::run();
driver<uint8_t>::run();
driver<uint16_t>::run();
driver<uint32_t>::run();
driver<uint64_t>::run();
}
Uscita su coliru, qui per riferimento:
static void driver<T>::run() [with T = signed char]
OK 000000000000000000000000111000111100 MASTER (x>3) && (x>y+1)
OK 000000000000000000000000111000111100 GOLF x>3&x>y+1
OK 000000000000000000000000111000111100 lookup "000000000000000000000000111000111100"[x*6+y]-'0'
OK 000000000000000000000000111000111100 question (x>3)&(x-y>1)
OK 000000000000000000000000111000111100 orlp0 x>3&x-y>1
OK 000000000000000000000000111000111100 orlp2 ~y+x>2>>y
OK 000000000000000000000000111000111100 orlp3 x*x-y*y>9
OK 000000000000000000000000111000111100 orlp4 ~y>x/~3*x
OK 000000000000000000000000111000111100 orlp5 -3>>y>y-x
OK 000000000000000000000000111000111100 orlp6 ~y+x<<y>2
ERR 000000000000000000000000000000000000 orlp1 -x<~y>4>x
static void driver<T>::run() [with T = short int]
OK 000000000000000000000000111000111100 MASTER (x>3) && (x>y+1)
OK 000000000000000000000000111000111100 GOLF x>3&x>y+1
OK 000000000000000000000000111000111100 lookup "000000000000000000000000111000111100"[x*6+y]-'0'
OK 000000000000000000000000111000111100 question (x>3)&(x-y>1)
OK 000000000000000000000000111000111100 orlp0 x>3&x-y>1
OK 000000000000000000000000111000111100 orlp2 ~y+x>2>>y
OK 000000000000000000000000111000111100 orlp3 x*x-y*y>9
OK 000000000000000000000000111000111100 orlp4 ~y>x/~3*x
OK 000000000000000000000000111000111100 orlp5 -3>>y>y-x
OK 000000000000000000000000111000111100 orlp6 ~y+x<<y>2
ERR 000000000000000000000000000000000000 orlp1 -x<~y>4>x
static void driver<T>::run() [with T = int]
OK 000000000000000000000000111000111100 MASTER (x>3) && (x>y+1)
OK 000000000000000000000000111000111100 GOLF x>3&x>y+1
OK 000000000000000000000000111000111100 lookup "000000000000000000000000111000111100"[x*6+y]-'0'
OK 000000000000000000000000111000111100 question (x>3)&(x-y>1)
OK 000000000000000000000000111000111100 orlp0 x>3&x-y>1
OK 000000000000000000000000111000111100 orlp2 ~y+x>2>>y
OK 000000000000000000000000111000111100 orlp3 x*x-y*y>9
OK 000000000000000000000000111000111100 orlp4 ~y>x/~3*x
OK 000000000000000000000000111000111100 orlp5 -3>>y>y-x
OK 000000000000000000000000111000111100 orlp6 ~y+x<<y>2
ERR 000000000000000000000000000000000000 orlp1 -x<~y>4>x
static void driver<T>::run() [with T = long int]
OK 000000000000000000000000111000111100 MASTER (x>3) && (x>y+1)
OK 000000000000000000000000111000111100 GOLF x>3&x>y+1
OK 000000000000000000000000111000111100 lookup "000000000000000000000000111000111100"[x*6+y]-'0'
OK 000000000000000000000000111000111100 question (x>3)&(x-y>1)
OK 000000000000000000000000111000111100 orlp0 x>3&x-y>1
OK 000000000000000000000000111000111100 orlp2 ~y+x>2>>y
OK 000000000000000000000000111000111100 orlp3 x*x-y*y>9
OK 000000000000000000000000111000111100 orlp4 ~y>x/~3*x
OK 000000000000000000000000111000111100 orlp5 -3>>y>y-x
OK 000000000000000000000000111000111100 orlp6 ~y+x<<y>2
ERR 000000000000000000000000000000000000 orlp1 -x<~y>4>x
static void driver<T>::run() [with T = unsigned char]
OK 000000000000000000000000111000111100 MASTER (x>3) && (x>y+1)
OK 000000000000000000000000111000111100 GOLF x>3&x>y+1
OK 000000000000000000000000111000111100 lookup "000000000000000000000000111000111100"[x*6+y]-'0'
OK 000000000000000000000000111000111100 question (x>3)&(x-y>1)
OK 000000000000000000000000111000111100 orlp0 x>3&x-y>1
OK 000000000000000000000000111000111100 orlp2 ~y+x>2>>y
OK 000000000000000000000000111000111100 orlp3 x*x-y*y>9
OK 000000000000000000000000111000111100 orlp4 ~y>x/~3*x
OK 000000000000000000000000111000111100 orlp5 -3>>y>y-x
OK 000000000000000000000000111000111100 orlp6 ~y+x<<y>2
ERR 000000000000000000000000000000000000 orlp1 -x<~y>4>x
static void driver<T>::run() [with T = short unsigned int]
OK 000000000000000000000000111000111100 MASTER (x>3) && (x>y+1)
OK 000000000000000000000000111000111100 GOLF x>3&x>y+1
OK 000000000000000000000000111000111100 lookup "000000000000000000000000111000111100"[x*6+y]-'0'
OK 000000000000000000000000111000111100 question (x>3)&(x-y>1)
OK 000000000000000000000000111000111100 orlp0 x>3&x-y>1
OK 000000000000000000000000111000111100 orlp2 ~y+x>2>>y
OK 000000000000000000000000111000111100 orlp3 x*x-y*y>9
OK 000000000000000000000000111000111100 orlp4 ~y>x/~3*x
OK 000000000000000000000000111000111100 orlp5 -3>>y>y-x
OK 000000000000000000000000111000111100 orlp6 ~y+x<<y>2
ERR 000000000000000000000000000000000000 orlp1 -x<~y>4>x
static void driver<T>::run() [with T = unsigned int]
OK 000000000000000000000000111000111100 MASTER (x>3) && (x>y+1)
OK 000000000000000000000000111000111100 GOLF x>3&x>y+1
OK 000000000000000000000000111000111100 lookup "000000000000000000000000111000111100"[x*6+y]-'0'
ERR 000000000000000000000000111001111100 question (x>3)&(x-y>1)
ERR 000000000000000000000000111001111100 orlp0 x>3&x-y>1
ERR 111111011111001111000111111011111101 orlp2 ~y+x>2>>y
ERR 011111001111000111000011111001111100 orlp3 x*x-y*y>9
ERR 111111111111111111111111111111111111 orlp4 ~y>x/~3*x
ERR 111111011111001111000111111011111101 orlp5 -3>>y>y-x
ERR 111111011111001111000111111011111101 orlp6 ~y+x<<y>2
ERR 000000000000000000000000000000000000 orlp1 -x<~y>4>x
static void driver<T>::run() [with T = long unsigned int]
OK 000000000000000000000000111000111100 MASTER (x>3) && (x>y+1)
OK 000000000000000000000000111000111100 GOLF x>3&x>y+1
OK 000000000000000000000000111000111100 lookup "000000000000000000000000111000111100"[x*6+y]-'0'
ERR 000000000000000000000000111001111100 question (x>3)&(x-y>1)
ERR 000000000000000000000000111001111100 orlp0 x>3&x-y>1
ERR 111111011111001111000111111011111101 orlp2 ~y+x>2>>y
ERR 011111001111000111000011111001111100 orlp3 x*x-y*y>9
ERR 111111111111111111111111111111111111 orlp4 ~y>x/~3*x
ERR 111111011111001111000111111011111101 orlp5 -3>>y>y-x
ERR 111111011111001111000111111011111101 orlp6 ~y+x<<y>2
ERR 000000000000000000000000000000000000 orlp1 -x<~y>4>x
Sommario
Dato che ciò riguarda il "costo" della ripetizione degli elementi del codice sorgente, è possibile utilizzare una tabella di ricerca. Puoi "nascondere" la tabella di ricerca, quindi lo è anche
LUT[x][y]
o
LUT[x*6+y]
Naturalmente puoi essere pedante e ottuso e rinominare la LUT
L[x][y]
Quindi la mia "versione" è ... 7 caratteri . (Oppure crea una funzione ed L(x,y)
è ancora più breve).
O, ancora più importante: corretto, testabile e mantenibile.