Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:08

0001 /*
0002  *  Copyright (c), 2024, BlueBrain Project, EPFL
0003  *
0004  *  Distributed under the Boost Software License, Version 1.0.
0005  *    (See accompanying file LICENSE_1_0.txt or copy at
0006  *          http://www.boost.org/LICENSE_1_0.txt)
0007  *
0008  */
0009 #pragma once
0010 
0011 #include <vector>
0012 #include "../H5Exception.hpp"
0013 
0014 namespace HighFive {
0015 namespace detail {
0016 
0017 /// \brief Squeeze `axes` from `dims`.
0018 ///
0019 /// An axis can only be squeezed if it's dimension is `1`. The elements of
0020 /// `axes` must be in the range `0, ..., dims.size()` (exclusive) and don't
0021 /// have to be sorted.
0022 ///
0023 /// Example:
0024 ///   squeeze({1, 3, 2, 1}, {0, 3}) == {3, 2}
0025 inline std::vector<size_t> squeeze(const std::vector<size_t>& dims,
0026                                    const std::vector<size_t>& axes) {
0027     auto n_dims = dims.size();
0028     auto mask = std::vector<bool>(n_dims, false);
0029     for (size_t i = 0; i < axes.size(); ++i) {
0030         if (axes[i] >= n_dims) {
0031             throw Exception("Out of range: axes[" + std::to_string(i) +
0032                             "] == " + std::to_string(axes[i]) + " >= " + std::to_string(n_dims));
0033         }
0034 
0035         mask[axes[i]] = true;
0036     }
0037 
0038     auto squeezed_dims = std::vector<size_t>{};
0039     for (size_t i = 0; i < n_dims; ++i) {
0040         if (!mask[i]) {
0041             squeezed_dims.push_back(dims[i]);
0042         } else {
0043             if (dims[i] != 1) {
0044                 throw Exception("Squeezing non-unity axis: axes[" + std::to_string(i) +
0045                                 "] = " + std::to_string(axes[i]));
0046             }
0047         }
0048     }
0049 
0050     return squeezed_dims;
0051 }
0052 
0053 }  // namespace detail
0054 }  // namespace HighFive