The International Simutrans Forum

 

Author Topic: [Code v3.7] fixed_list_tpl.h  (Read 11180 times)

0 Members and 1 Guest are viewing this topic.

knightly

  • Guest
[Code v3.7] fixed_list_tpl.h
« on: May 10, 2009, 08:16:12 PM »
Just some suggestions regarding the code of fixed_list_tpl.h

Maybe the following code :

Code: [Select]

//These methods are used for automating looping arithmetic
uint8 add_index(uint8 base, uint8 addition, int index)
{
uint8 tmp;

if((base + addition) < index)
{
return base + addition;
}
else
{
tmp = (base + addition) - index;
}

if(tmp < index)
{
return tmp;
}
else
{
//There could be a sophisticated system of trimming here,
//but it would take extra work/memory/processing power, and
//is only used internally, so not worth it. This code should
//never be reached.
return index - 1;
}
}

uint8 subtract_index(uint8 base, uint8 subtraction, int index)
{
uint8 tmp;

if((base - subtraction) < index && (base - subtraction) > 0)
{
return base - subtraction;
}
else
{
tmp = (base - subtraction) + index; //Should re-set the overflow
}

if(tmp < index)
{
return tmp;
}
else
{
//There could be a sophisticated system of trimming here,
//but it would take extra work/memory/processing power, and
//is only used internally, so not worth it. This code should
//never be reached.
return index - 1;
}
}


can be simplied like this :

Code: [Select]

//These methods are used for automating looping arithmetic
uint8 add_index(uint8 base, uint8 addition, int index)
{
return (addition < index ? (base + addition) % index : -1);
}

uint8 subtract_index(uint8 base, uint8 subtraction, int index)
{
return (substraction < index ? (base + index - subtraction) % index : -1);
}


And very minor suggestions :

Code: [Select]

void add_to_head(T datum)
{
uint8 i;
if(size > 0)
{
i = subtract_index(head, 1, N);
}
else
{
i = 0;
}
data[i] = datum;
head = i;
if(tail == head && size > 0)
{
tail = subtract_index(head, 1, N);
}
else
{
size ++;
}
if(placeholder_set && placeholder == i)
{
//Placeholder is overwritten
placeholder_set = false;
placeholder = 0;
}
}

void add_to_tail(T datum)
{
uint8 i;

if(size == 0)
{
i = 0;
}
else
{
i = add_index(tail, 1, N);
}
data[i] = datum;
tail = i;
if(head == tail && size > 0)
{
head = add_index(tail, 1, N);
if(placeholder_set && placeholder == i)
{
//Placeholder is overwritten
placeholder_set = false;
placeholder = 0;
}
}
else
{
size ++;
}
}


The temporary variable i in add_to_head() can be dropped and replaced by head, and the temporary variable i in add_to_tail() can be dropped and replaced by tail.