Click here to Skip to main content
13,195,379 members (61,929 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 3 Nov 2003

Input and output iterators for sampling a data stream

, 3 Nov 2003
Rate this:
Please Sign up or sign in to vote.
These iterators provide a simple way to sample or stretch a fixed size data set to fit in a larger or smaller container


I was working on an application to show 3D paths using Direct3D and ran into a video card limit in the number of elements in the path. To solve the problem, I wrote some simple code to sample the data I was generating without just clipping the data off at a fixed point. While I needed it for this special case, the problem is a general one that arises in stretching or shrinking data such as images or changing the sample rate of audio files.

I searched for some code that would accomplish that and the closest I could come up with was writing a custom predicate for the boost filter iterator. While writing the predicate, I realized there was a more general solution that would support not just sampling the data set but stretching it as well.

In order to provide the most flexible and easy to use interface I provided 3 ways that the sample algorithm can be used. Through an input iterator, an output iterator or a predicate that can be used by the boost filter iterator or the STL remove_if algorithm.

Using the code

Using the iterator is relatively simple. Per the STL convention, I've provided a pair of template functions that allow for easy creation of both input and output iterator forms. Here is a simple sample that shows the use of both forms input and output.

std::vector<INT> list;
for(int i=0; i<7; ++i)

std::cout << "Original sequence:" << std::endl;
std::copy(list.begin(), list.end(), std::ostream_iterator<INT>(std::cout, " "));
std::cout << std::endl;

std::cout << "Input sampled/stretched at various intervals:" << std::endl;
for(size_t dest_size = 1; dest_size<=list.size()*2; ++dest_size)
  std::copy(sample::sample(list.begin(), dest_size, list.size()),
       sample::sample(list.end(), dest_size, list.size()),
       std::ostream_iterator<INT>(std::cout, " "));
  std::cout << std::endl;

std::cout << "Output sampled/stretched at various intervals:" << std::endl;
for(size_t dest_size = 1; dest_size<=list.size()*2; ++dest_size)
  std::copy(list.begin(), list.end(), sample::sample_output(
      std::ostream_iterator<INT>(std::cout, " "), dest_size, list.size()));
  std::cout << std::endl;

The biggest limitation of these objects is that you must know the size of both the input and output dataset. Also, a simple sampling method is used that only takes the location of the element in the data set into account. It doesn't look at the quality of the sample point to drop. For example an optimal solution for sampling a 3D curve would take the curvature at each point into account when deciding to drop a data point.

Points of Interest

I suspected that moving the ownership of the actual base iterator object out of the functor object into the wrapping iterator objects would provide a greater flexibility and ease of use. I'm working on some more advanced sample functors right now that will use averaging/linear interpolation as well as spline interpolation, and I think these will be simplified by owning the iterator since they will make it easier for the algorithm to read/write the interpolated data without having excessive access to the iterator class internal objects. I hope to post the updated functors in the near future and will include any updates to these iterators that are required to make it work.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Andy Brummer
Software Developer (Senior) Standard Beagle Studios
United States United States
I co-founded Standard Beagle Studio, a software development consulting service in Austin Texas with my wife Cindy Brummer. We focus mostly on web projects, but have built some react native mobile apps, and even a windows screen saver or two.

I started my career back when ASP pages were state of the art, and IE3 was considered a web browser. I've worked with Microsoft technologies for most of that time, and have recently branched out into node, wordpress, and react native applications.

I'm a web developer, math and physics enthusiast, father of 2, and all around great guy. I live in Austin TX and love using technology to change people's lives for the better. When I manage scrape together some spare time, I build generative art at curvature of the mind.

You may also be interested in...


Comments and Discussions

GeneralCould not get it to compile. Pin
WREY5-Nov-03 1:09
memberWREY5-Nov-03 1:09 
GeneralRe: Could not get it to compile. Pin
andy brummer5-Nov-03 8:48
memberandy brummer5-Nov-03 8:48 
GeneralRe: Could not get it to compile. Pin
WREY5-Nov-03 11:02
memberWREY5-Nov-03 11:02 
GeneralWorks for some apps Pin
KevinHall4-Nov-03 6:31
memberKevinHall4-Nov-03 6:31 
GeneralRe: Works for some apps Pin
andy brummer4-Nov-03 14:10
memberandy brummer4-Nov-03 14:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171019.1 | Last Updated 4 Nov 2003
Article Copyright 2003 by Andy Brummer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid