keep splitting the atom question

A

Angus

Hello

I am writing a sampling type program which takes a buffer, and picks
the middle of the buffer. But I need to take a bit out the middle and
then take what is left (on both sides) and take the middle out of
these fragments.

I have a function which takes a range, eg start, end and a samplesize
and calculates the range of the buffer to take for sampling. But of
course each time I call this function it creates two new start-end
ranges to again get the middle bit again. I have a size of sample so
when the middle bits reach the sample size I just stop.

How would I write something to achieve this? Should it be a recursive
function?
 
J

Jim Langston

Angus said:
Hello

I am writing a sampling type program which takes a buffer, and picks
the middle of the buffer. But I need to take a bit out the middle and
then take what is left (on both sides) and take the middle out of
these fragments.

I have a function which takes a range, eg start, end and a samplesize
and calculates the range of the buffer to take for sampling. But of
course each time I call this function it creates two new start-end
ranges to again get the middle bit again. I have a size of sample so
when the middle bits reach the sample size I just stop.

How would I write something to achieve this? Should it be a recursive
function?

I'd probably do it recursively. This is more a programming question than a
C++ quesiton though.
 
G

Guest

Hello

I am writing a sampling type program which takes a buffer, and picks
the middle of the buffer. But I need to take a bit out the middle and
then take what is left (on both sides) and take the middle out of
these fragments.

I have a function which takes a range, eg start, end and a samplesize
and calculates the range of the buffer to take for sampling. But of
course each time I call this function it creates two new start-end
ranges to again get the middle bit again. I have a size of sample so
when the middle bits reach the sample size I just stop.

How would I write something to achieve this? Should it be a recursive
function?


Recursive is probably a good idea. However for these kinds of questions
please consult comp.programming instead.
 
V

Victor Bazarov

Angus said:
I am writing a sampling type program which takes a buffer, and picks
the middle of the buffer. But I need to take a bit out the middle

What does that mean "to take a bit out the middle"?
and
then take what is left (on both sides) and take the middle out of
these fragments.

And then what? When does it end, and what do you expect as the result
of that processing?
I have a function which takes a range, eg start, end and a samplesize
and calculates the range of the buffer to take for sampling. But of
course each time I call this function it creates two new start-end
ranges to again get the middle bit again. I have a size of sample so
when the middle bits reach the sample size I just stop.

How would I write something to achieve this? Should it be a recursive
function?

Yes, the recursion seems to fit right in.

template<class Iterator, class F>
void doSomethingWithTheMiddle(Iterator start, Iterator end, F f)
{
if (start != end) { // not an empty range
Iterator middle = start + (start - end)/2;
f(*middle);
doSomethingWithTheMiddle(start, middle);
doSomethingWithTheMiddle(middle, end);
}
}

The function above has a flaw -- it will recurse infinitely for a range
of length 1. You need to correct it. Hint: you need to add 1 addition
somewhere.

V
 
H

Howard

template<class Iterator, class F>
void doSomethingWithTheMiddle(Iterator start, Iterator end, F f)
{
if (start != end) { // not an empty range
Iterator middle = start + (start - end)/2;

Did you mean:

= start + (end-start)/2; // which equals (start+end)/2

?
f(*middle);
doSomethingWithTheMiddle(start, middle);
doSomethingWithTheMiddle(middle, end);
}
}

-Howard
 
V

Victor Bazarov

Howard said:
Did you mean:

= start + (end-start)/2; // which equals (start+end)/2

?

Yes, of course. However, the expression in your comment cannot
be substituted. For iterators a difference is often defined,
whereas a sum isn't. Think pointers, for example. You can
subtract one from the other and get a ptrdiff_t value, but you
can't add one pointer to another, it just doesn't make sense.

V
 
H

Howard

Victor Bazarov said:
Yes, of course. However, the expression in your comment cannot
be substituted. For iterators a difference is often defined,
whereas a sum isn't. Think pointers, for example. You can
subtract one from the other and get a ptrdiff_t value, but you
can't add one pointer to another, it just doesn't make sense.

I thought that was the case. That's why I put it in a comment, not in code.
Thanks for clarifying.
-Howard
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,124
Latest member
JuniorPell
Top