Skip to content

Commit 782d703

Browse files
committed
change README to match integer division stuff
1 parent 0fc3464 commit 782d703

File tree

1 file changed

+65
-84
lines changed

1 file changed

+65
-84
lines changed

README.md

Lines changed: 65 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,66 @@
1-
# beman.exemplar: A Beman Library Exemplar
1+
# beman.integer_division: Integer division utilities
22

33
<!--
44
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55
-->
66

77
<!-- markdownlint-disable-next-line line-length -->
8-
![Library Status](https://raw.githubusercontent.com/bemanproject/beman/refs/heads/main/images/badges/beman_badge-beman_library_under_development.svg) ![Continuous Integration Tests](https://github.com/bemanproject/exemplar/actions/workflows/ci_tests.yml/badge.svg) ![Lint Check (pre-commit)](https://github.com/bemanproject/exemplar/actions/workflows/pre-commit-check.yml/badge.svg) [![Coverage](https://coveralls.io/repos/github/bemanproject/exemplar/badge.svg?branch=main)](https://coveralls.io/github/bemanproject/exemplar?branch=main) ![Standard Target](https://github.com/bemanproject/beman/blob/main/images/badges/cpp29.svg) [![Compiler Explorer Example](https://img.shields.io/badge/Try%20it%20on%20Compiler%20Explorer-grey?logo=compilerexplorer&logoColor=67c52a)](https://godbolt.org/z/4qEPK87va)
8+
![Library Status](https://raw.githubusercontent.com/bemanproject/beman/refs/heads/main/images/badges/beman_badge-beman_library_under_development.svg) ![Continuous Integration Tests](https://github.com/bemanproject/integer_division/actions/workflows/ci_tests.yml/badge.svg) ![Lint Check (pre-commit)](https://github.com/bemanproject/integer_division/actions/workflows/pre-commit-check.yml/badge.svg) [![Coverage](https://coveralls.io/repos/github/bemanproject/integer_division/badge.svg?branch=main)](https://coveralls.io/github/bemanproject/integer_division?branch=main) ![Standard Target](https://github.com/bemanproject/beman/blob/main/images/badges/cpp29.svg) [![Compiler Explorer Example](https://img.shields.io/badge/Try%20it%20on%20Compiler%20Explorer-grey?logo=compilerexplorer&logoColor=67c52a)](https://godbolt.org/z/4qEPK87va)
99

10-
`beman.exemplar` is a minimal C++ library conforming to [The Beman Standard](https://github.com/bemanproject/beman/blob/main/docs/beman_standard.md).
11-
This can be used as a template for those intending to write Beman libraries.
12-
It may also find use as a minimal and modern C++ project structure.
13-
14-
**Implements**: `std::identity` proposed in [Standard Library Concepts (P0898R3)](https://wg21.link/P0898R3).
10+
**Implements**: Integer division function templates
11+
proposed in [Integer division (P3724R1)](https://wg21.link/P3724R1).
1512

1613
**Status**: [Under development and not yet ready for production use.](https://github.com/bemanproject/beman/blob/main/docs/beman_library_maturity_model.md#under-development-and-not-yet-ready-for-production-use)
1714

1815
## License
1916

20-
`beman.exemplar` is licensed under the Apache License v2.0 with LLVM Exceptions.
17+
`beman.integer_division` is licensed under the Apache License v2.0 with LLVM Exceptions.
2118

2219
## Usage
2320

24-
`std::identity` is a function object type whose `operator()` returns its argument unchanged.
25-
`std::identity` serves as the default projection in constrained algorithms.
26-
Its direct usage is usually not needed.
21+
- <code>std::div_<i>mode</i></code> is a family of functions that computes the quotient
22+
of an integer division with rounding mode of choice
23+
- <code>std::div_rem_<i>mode</i></code> is a family of functions that computes
24+
the quotient and remainder simultaneously
25+
- `std::mod` computes the remainder of the division with rounding towards negative infinity
2726

2827
### Usage: default projection in constrained algorithms
2928

30-
The following code snippet illustrates how we can achieve a default projection using `beman::exemplar::identity`:
29+
The following demonstrates how these functions may be used:
3130

3231
```cpp
33-
#include <beman/exemplar/identity.hpp>
34-
35-
namespace exe = beman::exemplar;
36-
37-
// Class with a pair of values.
38-
struct Pair
39-
{
40-
int n;
41-
std::string s;
42-
43-
// Output the pair in the form {n, s}.
44-
// Used by the range-printer if no custom projection is provided (default: identity projection).
45-
friend std::ostream &operator<<(std::ostream &os, const Pair &p)
46-
{
47-
return os << "Pair" << '{' << p.n << ", " << p.s << '}';
48-
}
49-
};
50-
51-
// A range-printer that can print projected (modified) elements of a range.
52-
// All the elements of the range are printed in the form {element1, element2, ...}.
53-
// e.g., pairs with identity: Pair{1, one}, Pair{2, two}, Pair{3, three}
54-
// e.g., pairs with custom projection: {1:one, 2:two, 3:three}
55-
template <std::ranges::input_range R,
56-
typename Projection>
57-
void print(const std::string_view rem, R &&range, Projection projection = exe::identity>)
58-
{
59-
std::cout << rem << '{';
60-
std::ranges::for_each(
61-
range,
62-
[O = 0](const auto &o) mutable
63-
{ std::cout << (O++ ? ", " : "") << o; },
64-
projection);
65-
std::cout << "}\n";
66-
};
67-
68-
int main()
69-
{
70-
// A vector of pairs to print.
71-
const std::vector<Pair> pairs = {
72-
{1, "one"},
73-
{2, "two"},
74-
{3, "three"},
75-
};
76-
77-
// Print the pairs using the default projection.
78-
print("\tpairs with beman: ", pairs);
79-
80-
return 0;
81-
}
82-
32+
#include <beman/integer_division/integer_division.hpp>
33+
34+
namespace idiv = beman::integer_division;
35+
36+
int bucket_size = 1000;
37+
int items = 100;
38+
39+
// Compute ceil(100 / 1000), i.e. round the quotient towards positive infinity.
40+
// Using (100 / 1000) would be wrong here because it would mean that
41+
// zero buckets are required to fit 100 elements.
42+
int buckets_required = idiv::div_to_inf(items, bucket_size);
43+
44+
// result.quotient equals 1.
45+
// result.remainder equals 900.
46+
idiv::div_result<int> result = idiv::div_rem_to_inf(items, bucket_size);
47+
// Alternative syntax using structured bindings.
48+
auto [quotient, remainder] = idiv::div_rem_to_inf(items, bucket_size);
49+
50+
// The following examples use the mod function,
51+
// which computes the remainder of an integer division with rounding towards negative infinity.
52+
// This rounding mode is also known as "flooring", and is widely used in other languages.
53+
// For example, Python's "%" operator uses this mode.
54+
// It is particularly useful because the sign of the remainder is the sign of the divisor,
55+
// unlike the builtin C++ "%" operator, which round towards zero,
56+
// so the sign of the remainder is the sign of the dividend.
57+
58+
// Computes (100 mod 1000), which equals 100.
59+
int wrap_100 = idiv::mod( 100, bucket_size);
60+
// Computes (1400 mod 1000), which equals 400.
61+
int wrap_1400 = idiv::mod(1400, bucket_size);
62+
// Computes (-100 mod 1000), which equals 900.
63+
int wrap_minus_100 = idiv::mod( -100, bucket_size);
8364
```
8465
8566
Full runnable examples can be found in [`examples/`](examples/).
@@ -145,7 +126,7 @@ requires minimal setup.
145126
146127
Click the following badge to create a codespace:
147128
148-
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/bemanproject/exemplar)
129+
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/bemanproject/integer_division)
149130
150131
For more documentation on GitHub codespaces, please see
151132
[this doc](https://docs.github.com/en/codespaces/).
@@ -325,43 +306,43 @@ This is required so that users of `beman.exemplar` can use
325306

326307
## Integrate beman.exemplar into your project
327308

328-
To use `beman.exemplar` in your C++ project,
329-
include an appropriate `beman.exemplar` header from your source code.
309+
To use `beman.integer_division` in your C++ project,
310+
include an appropriate `beman.integer_division` header from your source code.
330311

331312
```c++
332-
#include <beman/exemplar/identity.hpp>
313+
#include <beman/integer_division/integer_division.hpp>
333314
```
334315

335316
> [!NOTE]
336317
>
337-
> `beman.exemplar` headers are to be included with the `beman/exemplar/` prefix.
318+
> `beman.integer_division` headers are to be included with the `beman/integer_division/` prefix.
338319
> Altering include search paths to spell the include target another way (e.g.
339320
> `#include <identity.hpp>`) is unsupported.
340321
341-
The process for incorporating `beman.exemplar` into your project depends on the
322+
The process for incorporating `beman.integer_division` into your project depends on the
342323
build system being used. Instructions for CMake are provided in following sections.
343324

344-
### Incorporating `beman.exemplar` into your project with CMake
325+
### Incorporating `beman.integer_division` into your project with CMake
345326

346327
For CMake based projects,
347-
you will need to use the `beman.exemplar` CMake module
348-
to define the `beman::exemplar` CMake target:
328+
you will need to use the `beman.integer_division` CMake module
329+
to define the `beman::integer_division` CMake target:
349330

350331
```cmake
351-
find_package(beman.exemplar REQUIRED)
332+
find_package(beman.integer_division REQUIRED)
352333
```
353334

354-
You will also need to add `beman::exemplar` to the link libraries of
355-
any libraries or executables that include `beman.exemplar` headers.
335+
You will also need to add `beman::integer_division` to the link libraries of
336+
any libraries or executables that include `beman.integer_division` headers.
356337

357338
```cmake
358-
target_link_libraries(yourlib PUBLIC beman::exemplar)
339+
target_link_libraries(yourlib PUBLIC beman::integer_division)
359340
```
360341

361-
### Produce beman.exemplar static library
342+
### Produce beman.integer_division static library
362343

363344
You can include exemplar's headers locally
364-
by producing a static `libbeman.exemplar.a` library.
345+
by producing a static `libbeman.integer_division.a` library.
365346

366347
```bash
367348
cmake --workflow --preset gcc-release
@@ -374,14 +355,14 @@ This will generate the following directory structure at `/opt/beman`.
374355
/opt/beman
375356
├── include
376357
│ └── beman
377-
│ └── exemplar
358+
│ └── integer_division
378359
│ └── identity.hpp
379360
└── lib
380361
├── cmake
381-
│   └── beman.exemplar
382-
│   ├── beman.exemplar-config-version.cmake
383-
│   ├── beman.exemplar-config.cmake
384-
│   ├── beman.exemplar-targets-debug.cmake
385-
│   └── beman.exemplar-targets.cmake
386-
└── libbeman.exemplar.a
362+
│   └── beman.integer_division
363+
│   ├── beman.integer_division-config-version.cmake
364+
│   ├── beman.integer_division-config.cmake
365+
│   ├── beman.integer_division-targets-debug.cmake
366+
│   └── beman.integer_division-targets.cmake
367+
└── libbeman.integer_division.a
387368
```

0 commit comments

Comments
 (0)