Skip to content
This repository has been archived by the owner on Oct 10, 2019. It is now read-only.

Commit

Permalink
Andreas Krennmair:
Browse files Browse the repository at this point in the history
	implemented partitioning algorithm as preparation for parallel reload.
  • Loading branch information
akrennmair committed Nov 15, 2008
1 parent 2c71da7 commit e457c9b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class utils {
static bool is_valid_color(const std::string& color);
static bool is_valid_attribute(const std::string& attrib);

static std::vector<std::pair<unsigned int, unsigned int> > partition_indexes(unsigned int start, unsigned int end, unsigned int parts);

private:
static void append_escapes(std::string& str, char c);

Expand Down
14 changes: 14 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,4 +604,18 @@ bool utils::is_valid_attribute(const std::string& attrib) {
return false;
}

std::vector<std::pair<unsigned int, unsigned int> > utils::partition_indexes(unsigned int start, unsigned int end, unsigned int parts) {
std::vector<std::pair<unsigned int, unsigned int> > partitions;
unsigned int count = end - start + 1;
unsigned int size = count / parts;

for (unsigned int i=0;i<parts-1;i++) {
partitions.push_back(std::pair<unsigned int, unsigned int>(start, start + size - 1));
start += size;
}

partitions.push_back(std::pair<unsigned int, unsigned int>(start, end));
return partitions;
}

}
35 changes: 35 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,38 @@ BOOST_AUTO_TEST_CASE(TestHtmlRenderer) {
BOOST_CHECK(lines.size() >= 1);
BOOST_CHECK_EQUAL(lines[0], "A[i]");
}

BOOST_AUTO_TEST_CASE(TestIndexPartitioning) {
std::vector<std::pair<unsigned int, unsigned int> > partitions = utils::partition_indexes(0, 9, 2);
BOOST_CHECK_EQUAL(partitions.size(), 2u);
BOOST_CHECK_EQUAL(partitions[0].first, 0u);
BOOST_CHECK_EQUAL(partitions[0].second, 4u);
BOOST_CHECK_EQUAL(partitions[1].first, 5u);
BOOST_CHECK_EQUAL(partitions[1].second, 9u);

partitions = utils::partition_indexes(0, 10, 3);
BOOST_CHECK_EQUAL(partitions.size(), 3u);
BOOST_CHECK_EQUAL(partitions[0].first, 0u);
BOOST_CHECK_EQUAL(partitions[0].second, 2u);
BOOST_CHECK_EQUAL(partitions[1].first, 3u);
BOOST_CHECK_EQUAL(partitions[1].second, 5u);
BOOST_CHECK_EQUAL(partitions[2].first, 6u);
BOOST_CHECK_EQUAL(partitions[2].second, 10u);

partitions = utils::partition_indexes(0, 11, 3);
BOOST_CHECK_EQUAL(partitions.size(), 3u);
BOOST_CHECK_EQUAL(partitions[0].first, 0u);
BOOST_CHECK_EQUAL(partitions[0].second, 3u);
BOOST_CHECK_EQUAL(partitions[1].first, 4u);
BOOST_CHECK_EQUAL(partitions[1].second, 7u);
BOOST_CHECK_EQUAL(partitions[2].first, 8u);
BOOST_CHECK_EQUAL(partitions[2].second, 11u);

partitions = utils::partition_indexes(0, 199, 200);
BOOST_CHECK_EQUAL(partitions.size(), 200u);

partitions = utils::partition_indexes(0, 103, 1);
BOOST_CHECK_EQUAL(partitions.size(), 1u);
BOOST_CHECK_EQUAL(partitions[0].first, 0u);
BOOST_CHECK_EQUAL(partitions[0].second, 103u);
}

0 comments on commit e457c9b

Please sign in to comment.