Skip to content

MergeSkylines could be O(n) #8

@briterator

Description

@briterator

MergeSkylines calls erase in a loop potentially N times. Each call is O(n), making this function O(n^2).

If you use an algorithm like std::unique this function would be O(n).

pseudocode:

auto compare = [](SkylineNode& a, SkylineNode& b)
{
	if (a.y == b.y)
	{
		a.width += b.width;
		return true;
	}
	return false;
};
auto new_end = std::unique(skyLine.begin(), skyLine.end(), compare);
skyLine.erase( new_end , skyLine.end());

cppreference states that the predicate is not allowed to modify a and b in the above code which we are doing. To work around this limitation you could simply copy the implementation of std::unique found on cppreference and put it into your own algorithm. The reference implementation of std::unique that they provide should work fine with mutating the values. Alternatively you could just use the implementation of std::unique as inspiration for a specialized solution.

Thanks for providing this library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions