Matter SDK Coverage Report
Current view: top level - lib/support - Fold.h (source / functions) Coverage Total Hit
Test: SHA:4d2388ac7eed75b2fe5e05e20de377999c632502 Lines: 100.0 % 6 6
Test Date: 2025-07-27 07:17:09 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2021 Project CHIP Authors
       4              :  *
       5              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       6              :  *    you may not use this file except in compliance with the License.
       7              :  *    You may obtain a copy of the License at
       8              :  *
       9              :  *        http://www.apache.org/licenses/LICENSE-2.0
      10              :  *
      11              :  *    Unless required by applicable law or agreed to in writing, software
      12              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      13              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14              :  *    See the License for the specific language governing permissions and
      15              :  *    limitations under the License.
      16              :  */
      17              : 
      18              : #pragma once
      19              : 
      20              : #include <functional>
      21              : 
      22              : namespace chip {
      23              : 
      24              : /**
      25              :  * Apply the functor sequentially to the previous value and a subsequent element of the container.
      26              :  *
      27              :  * This provides a similar functionality to std::acumulate, but can be used in constexpr contexts.
      28              :  */
      29              : template <class Container, class Functor, class ValueType = typename Container::value_type>
      30            9 : constexpr ValueType Fold(const Container & container, ValueType initial, Functor functor)
      31              : {
      32           25 :     for (const auto & element : container)
      33              :     {
      34           16 :         initial = functor(initial, element);
      35              :     }
      36              : 
      37            9 :     return initial;
      38              : }
      39              : 
      40              : /**
      41              :  * Sum all elements of the container using "+" operator.
      42              :  */
      43              : template <class Container, class ValueType = typename Container::value_type>
      44            4 : constexpr ValueType Sum(const Container & container)
      45              : {
      46            4 :     return Fold(container, ValueType{}, std::plus<ValueType>{});
      47              : }
      48              : 
      49              : } // namespace chip
        

Generated by: LCOV version 2.0-1