473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

matching callback parameters in generic algorithm

I'm useless at templates, but, inspired by this post:

<hi***************************@syrcnyrdrs-01-ge0.nyroc.rr.com>,

I'm trying to write an algorithm

template <class BidirectionalIterator, class Function, class Size>
Function for_each_combination(BidirectionalIterator first,
BidirectionalIterator last,
Size k,
Function fn);

which does the obvious thing.

The post references some code (from Feb 05 CUJ) which implements
for_each_permutation. That implementation uses a nice algorithm from
Knuth to generate the permutations which

(i) cycles the elements of [first, last[ in situ, so that the callback can
be called simply as fn(first, last).

(ii) leaves the elements of [first, last[ in their original order.

I can't find such a nice algorithm for combinations. I have a simple
enough algorithm which generates all combinations of the integers 1, ...
(k-1). When it comes to calling the callback fn for each combination, I
copy the corresponding k elements from [first, last[ into a

vector<typename iterator_traits<BidirectionalIterator>::value_type >

but I can't make the call to fn. The caller supplies a callback taking
two parameters of the type BidirectionalIterator, which isn't matched.

My questions:

Is there a way that I can convert iterators from my vector into the type
of the template parameter BidirectionalIterator?

Alternatively, and off-topic, can anyone point me to an algorithm for
enumerating combinations which has the properties (i) and (ii) above?

Thanks.


Sep 10 '05 #1
3 1678
On Sat, 10 Sep 2005 12:24:48 +0000, spakka wrote:
Alternatively, and off-topic, can anyone point me to an algorithm for
enumerating combinations which has the properties (i) and (ii) above?


Ok - sorted now.

Sep 11 '05 #2
spakka wrote:
On Sat, 10 Sep 2005 12:24:48 +0000, spakka wrote:

Alternatively, and off-topic, can anyone point me to an algorithm for
enumerating combinations which has the properties (i) and (ii) above?

Ok - sorted now.


How? It was an interesting problem.

john
Sep 11 '05 #3
On Sun, 11 Sep 2005 13:29:49 +0000, John Harrison wrote:
spakka wrote:
On Sat, 10 Sep 2005 12:24:48 +0000, spakka wrote:

Alternatively, and off-topic, can anyone point me to an algorithm for
enumerating combinations which has the properties (i) and (ii) above?

Ok - sorted now.


How? It was an interesting problem.

john


I cheated. I just copy into the storage that was passed to me, then
restore the original contents at the end:
// not for users
template<class ForwardIterator, class RandomAccessIterator,
class Size, class Function>
Function _combine(ForwardIterator first, ForwardIterator lastk,
ForwardIterator current, Size n, Size k, Size base, Size level,
Function f, RandomAccessIterator vals)
{
for (Size i = base; i < n - k + 1 + level; ++i) {
*current = vals[i];
if (level == k - 1) {
f(first, lastk);
}
else {
f = _combine(first, lastk, current + 1, n, k, i + 1, level + 1,
f, vals);
}
}
return f;
}

template<class ForwardIterator, class Function, class Size>
Function for_each_combination(ForwardIterator first,
ForwardIterator last, Size k, Function fn)
{
Size n = distance(first, last);
ForwardIterator lastk = first;
advance(lastk, k);

// save the original sequence
vector<typename iterator_traits<ForwardIterator>::value_type> sav;
sav.reserve(n);
copy(first, last, back_inserter(sav));

fn = _combine(first, lastk, first, n, k, 0, 0, fn, sav.begin());

// restore the original sequence
copy(sav.begin(), sav.end(), first);

return fn;
}

Sep 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: ajay.sonawane | last post by:
Hello ther I read somewhere that callback function should be global or static member function. 1: I could use static member functions but how could I access members of class which ar not static....
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
8
by: kurtcobain1978 | last post by:
-------------------------------------------------------------------------------- I need to do the exactly same thing in VB.NET. Load a unmanaged C DLL dynamically and then call a function in...
10
by: javuchi | last post by:
I'm searching for a library which makes aproximative string matching, for example, searching in a dictionary the word "motorcycle", but returns similar strings like "motorcicle". Is there such a...
12
by: Larry Bates | last post by:
I'm trying to get the results of binascii.crc32 to match the results of another utility that produces 32 bit unsigned CRCs. binascii.crc32 returns results in the range of -2**31-1 and 2**21-1....
2
by: Marcus Kwok | last post by:
I have processing code (I'll call it the "model") written in native unmanaged pure C++, and I have put a GUI on top of it written using Windows Forms (.NET 1.1). The GUI is used to set the...
0
by: RoninZA | last post by:
Hi all, My problem is this... I need to somehow communicate the completion of a workflow back to the calling application. The scenario is as follows: I have a "generic" workflow engine,...
6
by: Daniel Fetchinson | last post by:
Hi all, There are a number of free tools for image matching but it's not very easy to decipher the actual algorithm from the code that includes db management, GUI, etc, etc. I have my own image...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.