`cardinality`'s return type can't represent the number of elements of large containers

Open
#61 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
cpp
Domain
backend

Research direction

Start by locating the implementation and tests for cardinality in the Boost ICL repository, then trace how it counts elements and chooses its return type. The issue establishes that a closed interval from 0 through SIZE_MAX produces an unrepresentable count, but leaves the API behavior undecided among a precondition, an exception, or saturation. Done means the API decision is made and the resulting behavior is covered by tests.

Written by the indexing model from the issue text.

Description

Consider https://godbolt.org/z/nooz8adKa:

#include <boost/icl/interval_set.hpp>
#include <climits>
#include <iostream>

namespace icl = boost::icl;

int main()
{
  icl::interval_set<std::size_t> s;
  s.insert(icl::interval<std::size_t>::closed(0, SIZE_MAX));
  std::cout << s.empty() << "\n";       // outputs 0
  std::cout << cardinality(s) << "\n";  // outputs 0
}

s has SIZE_MAX + 1 elements, which can't be represented by s's size_type (std::size_t). This is a fundamental problem with cardinality's API, not an implementation bug; in particular, #60 does not solve this issue. Possible API-changing solutions are:

  • Require as a precondition to calling cardinality that the number of elements not be greater than std::numeric_limits<size_t>::max().
  • Throw std::length_error when the number of elements exceeds std::numeric_limits<size_t>::max().
  • Saturate the returned value at std::numeric_limits<size_t>::max().
Dominant language
C++
Stars
15
Forks
51
Avg merge
8d 6h
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/icl

All issues in boostorg/icl

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.