• Advertisement
Sign in to follow this  

boost / stl algorithm replacement?

This topic is 3481 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a neater way to do this with the stl algorithms (and boost) instead of the for loops? I can't really think of one (I thought maybe unique or unique_copy, or something might work, but couldn't see how). It doesn't really matter, but I'm just curious to see if this could be made rather more elegant. Use of boost bind / lambda is fine.

// Component / widget class.
struct TestClass {
	TestClass(bool resizeable, int size):
			resizeable(resizeable), size(size) {}
	
	bool getResizeable() const {
		return resizeable;
	}
	int getSize() const {
		return size;
	}

	// placeholder for proper overlap comparison
	bool overlaps(int s) const {
		if (s == size) {
			return true;
		}
		return false;
	}
	
	bool resizeable;

	// placeholder for proper size
	int size;
	
};

	// ...
	typedef std::vector<boost::shared_ptr<TestClass> > items_type;
	items_type items;
	
	// fill container ...
	
	// Start with a container filled with widgets. Some widgets are automatically layed out (getResizeable == true). 
	// Others have their size specified.

	// Partition between resizeable (auto-layout) items and size specified items.
	items_type::size_type endAutoLayout = std::partition(items.begin(), items.end(), boost::mem_fn(&TestClass::getResizeable)) - items.begin();
	
	// REPLACEMENT FROM HERE DOWN.
	// Go through the size specified items. 
	// If two of them overlap, move one of the items to the start of the partition and increment endAutoLayout, so it'll be layed out automatically too.
	int count = 0;
	for (items_type::size_type i = endAutoLayout; i != items.size()-1; ++i) {
		int size = items->getSize();
		for (items_type::size_type j = i+1; j != items.size(); ++j) {
			if (items->overlaps(items[j]->getSize())) {
				items.insert(items.begin() + endAutoLayout, *(items.begin()+j));
				items.erase(items.begin() + j + 1);
				++count;
			}
		}
	}
	
	endAutoLayout += count;
	// ...

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement