summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libcamera/internal/utils.h86
-rw-r--r--src/libcamera/utils.cpp34
-rw-r--r--test/utils.cpp59
3 files changed, 179 insertions, 0 deletions
diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h
index d0146b71..83dada7c 100644
--- a/include/libcamera/internal/utils.h
+++ b/include/libcamera/internal/utils.h
@@ -9,12 +9,14 @@
#include <algorithm>
#include <chrono>
+#include <iterator>
#include <memory>
#include <ostream>
#include <sstream>
#include <string>
#include <string.h>
#include <sys/time.h>
+#include <utility>
#include <vector>
#ifndef __DOXYGEN__
@@ -230,6 +232,90 @@ details::reverse_adapter<T> reverse(T &&iterable)
return { iterable };
}
+namespace details {
+
+template<typename Base>
+class enumerate_iterator
+{
+private:
+ using base_reference = typename std::iterator_traits<Base>::reference;
+
+public:
+ using difference_type = typename std::iterator_traits<Base>::difference_type;
+ using value_type = std::pair<const difference_type, base_reference>;
+ using pointer = value_type *;
+ using reference = value_type &;
+ using iterator_category = std::input_iterator_tag;
+
+ explicit enumerate_iterator(Base iter)
+ : current_(iter), pos_(0)
+ {
+ }
+
+ enumerate_iterator &operator++()
+ {
+ ++current_;
+ ++pos_;
+ return *this;
+ }
+
+ bool operator!=(const enumerate_iterator &other) const
+ {
+ return current_ != other.current_;
+ }
+
+ value_type operator*() const
+ {
+ return { pos_, *current_ };
+ }
+
+private:
+ Base current_;
+ difference_type pos_;
+};
+
+template<typename Base>
+class enumerate_adapter
+{
+public:
+ using iterator = enumerate_iterator<Base>;
+
+ enumerate_adapter(Base begin, Base end)
+ : begin_(begin), end_(end)
+ {
+ }
+
+ iterator begin() const
+ {
+ return iterator{ begin_ };
+ }
+
+ iterator end() const
+ {
+ return iterator{ end_ };
+ }
+
+private:
+ const Base begin_;
+ const Base end_;
+};
+
+} /* namespace details */
+
+template<typename T>
+auto enumerate(T &iterable) -> details::enumerate_adapter<decltype(iterable.begin())>
+{
+ return { std::begin(iterable), std::end(iterable) };
+}
+
+#ifndef __DOXYGEN__
+template<typename T, size_t N>
+auto enumerate(T (&iterable)[N]) -> details::enumerate_adapter<T *>
+{
+ return { std::begin(iterable), std::end(iterable) };
+}
+#endif
+
} /* namespace utils */
} /* namespace libcamera */
diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp
index c4098a74..826144d3 100644
--- a/src/libcamera/utils.cpp
+++ b/src/libcamera/utils.cpp
@@ -472,6 +472,40 @@ std::string libcameraSourcePath()
* loop, will cause the loop to iterate over the \a iterable in reverse order
*/
+/**
+ * \fn enumerate(T &iterable)
+ * \brief Wrap an iterable to enumerate index and value in a range-based loop
+ * \param[in] iterable The iterable
+ *
+ * Range-based for loops are handy and widely preferred in C++, but are limited
+ * in their ability to replace for loops that require access to a loop counter.
+ * The enumerate() function solves this problem by wrapping the \a iterable in
+ * an adapter that, when used as a range-expression, will provide iterators
+ * whose value_type is a pair of index and value reference.
+ *
+ * The iterable must support std::begin() and std::end(). This includes all
+ * containers provided by the standard C++ library, as well as C-style arrays.
+ *
+ * A typical usage pattern would use structured binding to store the index and
+ * value in two separate variables:
+ *
+ * \code{.cpp}
+ * std::vector<int> values = ...;
+ *
+ * for (auto [index, value] : utils::enumerate(values)) {
+ * ...
+ * }
+ * \endcode
+ *
+ * Note that the argument to enumerate() has to be an lvalue, as the lifetime
+ * of any rvalue would not be extended to the whole for loop. The compiler will
+ * complain if an rvalue is passed to the function, in which case it should be
+ * stored in a local variable before the loop.
+ *
+ * \return A value of unspecified type that, when used in a range-based for
+ * loop, iterates over an indexed view of the \a iterable
+ */
+
} /* namespace utils */
} /* namespace libcamera */
diff --git a/test/utils.cpp b/test/utils.cpp
index 08f29389..7e24c71e 100644
--- a/test/utils.cpp
+++ b/test/utils.cpp
@@ -12,6 +12,7 @@
#include <vector>
#include <libcamera/geometry.h>
+#include <libcamera/span.h>
#include "libcamera/internal/utils.h"
@@ -73,6 +74,60 @@ protected:
return TestPass;
}
+ int testEnumerate()
+ {
+ std::vector<int> integers{ 1, 2, 3, 4, 5 };
+ int i = 0;
+
+ for (auto [index, value] : utils::enumerate(integers)) {
+ if (index != i || value != i + 1) {
+ cerr << "utils::enumerate(<vector>) test failed: i=" << i
+ << ", index=" << index << ", value=" << value
+ << std::endl;
+ return TestFail;
+ }
+
+ /* Verify that we can modify the value. */
+ --value;
+ ++i;
+ }
+
+ if (integers != std::vector<int>{ 0, 1, 2, 3, 4 }) {
+ cerr << "Failed to modify container in enumerated range loop" << endl;
+ return TestFail;
+ }
+
+ Span<const int> span{ integers };
+ i = 0;
+
+ for (auto [index, value] : utils::enumerate(span)) {
+ if (index != i || value != i) {
+ cerr << "utils::enumerate(<span>) test failed: i=" << i
+ << ", index=" << index << ", value=" << value
+ << std::endl;
+ return TestFail;
+ }
+
+ ++i;
+ }
+
+ const int array[] = { 0, 2, 4, 6, 8 };
+ i = 0;
+
+ for (auto [index, value] : utils::enumerate(array)) {
+ if (index != i || value != i * 2) {
+ cerr << "utils::enumerate(<array>) test failed: i=" << i
+ << ", index=" << index << ", value=" << value
+ << std::endl;
+ return TestFail;
+ }
+
+ ++i;
+ }
+
+ return TestPass;
+ }
+
int run()
{
/* utils::hex() test. */
@@ -177,6 +232,10 @@ protected:
return TestFail;
}
+ /* utils::enumerate() test. */
+ if (testEnumerate() != TestPass)
+ return TestFail;
+
return TestPass;
}
};
href='#n436'>436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
{
    "version": 2.0,
    "target": "bcm2835",
    "algorithms": [
        {
            "rpi.black_level":
            {
                "black_level": 4096
            }
        },
        {
            "rpi.dpc": { }
        },
        {
            "rpi.lux":
            {
                "reference_shutter_speed": 27685,
                "reference_gain": 1.0,
                "reference_aperture": 1.0,
                "reference_lux": 998,
                "reference_Y": 12744
            }
        },
        {
            "rpi.noise":
            {
                "reference_constant": 0,
                "reference_slope": 3.67
            }
        },
        {
            "rpi.geq":
            {
                "offset": 204,
                "slope": 0.01633
            }
        },
        {
            "rpi.sdn": { }
        },
        {
            "rpi.awb":
            {
                "priors": [
                    {
                        "lux": 0,
                        "prior":
                        [
                            2000, 1.0,
                            3000, 0.0,
                            13000, 0.0
                        ]
                    },