algorith implementation

B

Bo Persson

Kai-Uwe Bux said:
It should not. Such comparison is handled gracefully by promotion
rules. I
think, the code is required to compile.

It does with a warning, but the result is incorrect. :-(

If Size is ptrdiff_t for example, and n is negative, comparing to a
size_t doesn't work.

We are on the right track though, and I think this one could work:

template < typename OutIter, typename Size, typename T >
void fill_n ( OutIter first, Size n, const T & value ) {
if (0 < n) // n must be positive
{
for ( std::size_t count = n; 0 < count; --count ) {
*first = value;
++first;
}
}
}

If we sort out the possibly negative n first, the remaining values
must be convertible to size_t (assuming size_t is the largest unsigned
type :).


Bo Persson
 
F

Frederick Gotham

Bo Persson posted:
My compiler would complain about a signed/unsigned comparison when
Size really is signed.


Simply cast one of them -- I regularly use casts to suppress compiler
warnings, e.g.:

for(size_t i = MAX - 1; i != (size_t)-1; --i)
 
B

Bo Persson

Frederick Gotham said:
Bo Persson posted:



Simply cast one of them -- I regularly use casts to suppress
compiler
warnings, e.g.:

for(size_t i = MAX - 1; i != (size_t)-1; --i)

That just works sometimes.

If the signed value really is negative, it doesn't help.

int x = -5;
unsigned y = UINT_MAX;

if (x < y)
...


Bo Persson
 
V

Victor Bazarov

Bo said:
That just works sometimes.

If the signed value really is negative, it doesn't help.

int x = -5;
unsigned y = UINT_MAX;

if (x < y)
...

What is the expected result? Since -5 converted to unsigned is the
same as unsigned((UINT_MAX + 1) - 5), it's less than UINT_MAX. If
both of them are promoted to 'long', -5 is still less than UINT_MAX
which is positive. If they are promoted to unsigned long, see the
unsigned thing above.

V
 
V

Victor Bazarov

Victor said:
[..] Since -5 converted to unsigned is the
same as unsigned((UINT_MAX + 1) - 5)[..]

Now that I'm thinking about it again, it's probably implementation-
defined and not that simple... Sounded plausible, didn't it? :)

V
 
K

Kai-Uwe Bux

Bo said:
It does with a warning, but the result is incorrect. :-(

If Size is ptrdiff_t for example, and n is negative, comparing to a
size_t doesn't work.

You'r right. My bad.

We are on the right track though, and I think this one could work:

template < typename OutIter, typename Size, typename T >
void fill_n ( OutIter first, Size n, const T & value ) {
if (0 < n) // n must be positive
{
for ( std::size_t count = n; 0 < count; --count ) {
*first = value;
++first;
}
}
}

If we sort out the possibly negative n first, the remaining values
must be convertible to size_t (assuming size_t is the largest unsigned
type :).

Looks good. Maybe, I would do

template < typename OutIter, typename Size, typename T >
void fill_n ( OutIter first, Size n, const T & value ) {
if (0 <= n) // n must not be negative
{
for ( std::size_t count = n; 0 < count; --count ) {
*first = value;
++first;
}
}
}

to make it easier for the compiler to optimize the if-statement away if Size
is an unsigned type.


Best

Kai-Uwe Bux
 
F

Fraser Ross

template <class OutputIterator, class Size, class T>
void fill_n (OutputIterator first, Size n, const T& value) {
if (1 <= n) {
size_t m= n;
do {
--m;
*first= value;
++first;
}
while (1 <= m);
}
};
I had roughly the same code. I would have written almost the same as
you after noticing that a for statement can be used. The optimisation
though I wouldn't have known about.

Fraser.



template < typename OutIter, typename Size, typename T >
void fill_n ( OutIter first, Size n, const T & value ) {
if (0 <= n) // n must not be negative
{
for ( std::size_t count = n; 0 < count; --count ) {
*first = value;
++first;
}
}
}

to make it easier for the compiler to optimize the if-statement away if Size
is an unsigned type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top