Minimizing lines
#15

Quote:
Originally Posted by NealPeteros
Посмотреть сообщение
So I have a crapload of information in one line. How do you minimize it in order to avoid the
Код:
error 075: input line too long (after substitutions)
Код:
if(i != 19 || i != 20 || i != 21 || i != 23 || i != 24 || i != 25 || i != 27 || i != 28 || i != 29 || i != 30 || i != 31 || i != 33 \
                    || i != 34 || i != 35 || i != 36 || i != 37 || i != 38 || i != 39 || i != 40 || i != 41 || i != 42 ||  i != 44 || i != 45 || i != 46 \
                    || i != 47 || i != 48 || i != 49 || i != 50 || i != 51 || i != 52 || i != 53 || i != 55 || i != 56 || i != 57 || i != 58 || i != 59 \
                    || i != 60 || i != 61 || i != 62 || i != 63 || i != 64 || i != 65 || i != 66 || i != 68 || i != 69 || i != 70 \
                    || i != 72 || i != 73 || i != 74 || i != 76 || i != 77 || i != 78)
OK, firstly I don’t think that code does what you think it does. Taking just a small part:

Код:
if(i != 19 || i != 20)
So if the value is 19, the first check fails. But if the value is 19 the second check PASSES! So since you check that a number is not two numbers, every number passes - because no number is two numbers! Of course in your code it is worse since you check many many numbers pointlessly. To pass this check i would have to be 19 AND 20 AND 21 AND … ALL AT ONCE - it clearly can’t be that…

You probably want:

Код:
if (i == 19 || i == 20 || i == 21 ...)
You can then invert the whole thing with ! or else:

Код:
if (!(i == 19 || i == 20 || i == 21 ...))
Secondly, the two given options so far can be combined. They were:

Код:
if (i < 19 || i > 78)
That misses 22, 26, and others.

Код:
switch (i)
{
    case 19, 20, 21, 23, 24, 25, 27, 28, 29, 30, 31, 33, 34,
         35, 36, 37, 38, 39, 40, 41, 42, 44, 45, 46, 47, 48,
         49, 50, 51, 52, 53, 55, 56, 57, 58, 59, 60, 61, 62,
         63, 64, 65, 66, 68, 69, 70, 72, 73, 74, 76, 77, 78:
    {
    }
}
That is more accurate and slightly faster than all the ifs, but is fundamentally the same thing.

Код:
switch (i)
{
    case 19..21, 23..25, 27..31, 33..42, 44..53, 55..66, 68..70, 72..74, 76..78:
    {
    }
}
This generates identical code to the other switch, but is a bit shorter to write, and lends itself to a new form:

Код:
if (19 <= i <= 21 || 23 <= i <= 25 || 27 <= i <= 31 || 33 <= i <= 42 || 44 <= i <= 53 || 55 <= i <= 66 || 68 <= i <= 70 || 72 <= i <= 74 || 76 <= i <= 78)
{
}
That’s the fastest method so far given, but there is one even better method no-one has suggested, one that requires just one check (maybe two):

Код:
static const isIn_s[] = {
    false, false, false, false, false, false, false, false, false, false,
    false, false, false, false, false, false, false, false, false, true,
    true,  true,  false, true,  true,  true,  false, true,  true,  true,
    true,  true,  false, true,  true,  true,  true,  true,  true,  true,
    true,  true,  true,  false, true,  true,  true,  true,  true,  true,
    true,  true,  true,  true,  false, true,  true,  true,  true,  true,
    true,  true,  true,  true,  true,  true,  true,  false, true,  true,
    true,  false, true,  true,  true,  false, true,  true,  true
};
if (0 <= i < sizeof (isIn_s) && isIn_s[i])
{
}
The 0 <= i < sizeof (isIn_s) check can be skipped if you know the range of i.
Reply


Messages In This Thread
Minimizing lines - by NealPeteros - 15.09.2018, 09:39
Re: Minimizing lines - by Banditul18 - 15.09.2018, 09:41
Re: Minimizing lines - by Lucky13 - 15.09.2018, 09:42
Re: Minimizing lines - by Spmn - 15.09.2018, 09:52
Re: Minimizing lines - by NealPeteros - 15.09.2018, 09:54
Re: Minimizing lines - by Calisthenics - 15.09.2018, 10:23
Re: Minimizing lines - by UFF - 15.09.2018, 13:26
Re: Minimizing lines - by v1k1nG - 15.09.2018, 13:27
Re: Minimizing lines - by Calisthenics - 15.09.2018, 13:39
Re: Minimizing lines - by v1k1nG - 15.09.2018, 13:46
Re: Minimizing lines - by UFF - 15.09.2018, 13:49
Re: Minimizing lines - by v1k1nG - 15.09.2018, 13:53
Re: Minimizing lines - by ShihabSoft - 15.09.2018, 16:16
Re: Minimizing lines - by Threshold - 16.09.2018, 00:38
Re: Minimizing lines - by Y_Less - 16.09.2018, 10:36

Forum Jump:


Users browsing this thread: 1 Guest(s)