Skip to content

Commit

Permalink
implement manual update
Browse files Browse the repository at this point in the history
  • Loading branch information
applejxd committed Dec 25, 2023
1 parent 26f234b commit ea9db29
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,33 @@ For local installation, please download the above file to your project folder an
You can use this tqdm library with range-based for sytax.

```cpp
// wrap std::vector
std::cout << "wrap std::vector" << std::endl;
for (const auto& elem : tqdm::Tqdm<int>({0, 1, 2, 3, 4})) {
std::cout << elem << std::endl;
sleep(1);
}

// add description
std::cout << "add description" << std::endl;
for (const auto& elem : tqdm::Tqdm<int>({0, 1, 2, 3, 4}, "test")) {
std::cout << elem << std::endl;
sleep(1);
}

// trange syntax
std::cout << "trange syntax" << std::endl;
for (const auto& elem : tqdm::trange(5)) {
std::cout << elem << std::endl;
sleep(1);
}

std::cout << "manual update" << std::endl;
{
auto tqdm = tqdm::Tqdm<int>(100);
auto pbar = tqdm.begin();
for (int i = 0; i < 10; i++) {
sleep(1);
pbar.update(10);
}
}
```

## ToDo

- [ ] Nested loops
- [ ] Manual control
- [ ] Loop and printing compatibility
16 changes: 13 additions & 3 deletions sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,30 @@
#include "tqdm-cpp.hpp"

int main() {
std::cout << "wrap std::vector" << std::endl;
for (const auto& elem : tqdm::Tqdm<int>({0, 1, 2, 3, 4})) {
std::cout << elem << std::endl;
sleep(1);
}

std::cout << "add description" << std::endl;
for (const auto& elem : tqdm::Tqdm<int>({0, 1, 2, 3, 4}, "test")) {
std::cout << elem << std::endl;
sleep(1);
}

std::cout << "trange syntax" << std::endl;
for (const auto& elem : tqdm::trange(5)) {
std::cout << elem << std::endl;
sleep(1);
}

std::cout << "manual update" << std::endl;
{
auto tqdm = tqdm::Tqdm<int>(100);
auto pbar = tqdm.begin();
for (int i = 0; i < 10; i++) {
sleep(1);
pbar.update(10);
}
}

return 0;
}
22 changes: 10 additions & 12 deletions tqdm-cpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ class Tqdm {
bool operator==(const iterator& rhs) { return _ptr == rhs._ptr; }
bool operator!=(const iterator& rhs) { return _ptr != rhs._ptr; }

iterator operator++();

void update(int n);
iterator operator++() {
// The same with usual iterators
iterator i = *this;
_ptr++;
update(1);
return i;
};
iterator operator++(int junk) {
_ptr++;

Expand Down Expand Up @@ -113,13 +119,8 @@ class Tqdm {
};

template <typename T>
typename Tqdm<T>::iterator Tqdm<T>::iterator::operator++() {
// The same with usual iterators
iterator i = *this;
_ptr++;

// ----- custom ----- //
_counter++;
void Tqdm<T>::iterator::update(int n) {
_counter = _counter + n;

std::cout << "\r";

Expand Down Expand Up @@ -168,9 +169,6 @@ typename Tqdm<T>::iterator Tqdm<T>::iterator::operator++() {
<< std::flush;

_pre_time = curr_time;
// ----- custom ----- //

return i;
}

Tqdm<int> trange(int num) {
Expand Down

0 comments on commit ea9db29

Please sign in to comment.