Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/spotify-json/include/spotify/json/detail/stack.hpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2016-2017 Spotify AB
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
 * use this file except in compliance with the License. You may obtain a copy of
6
 * the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 * License for the specific language governing permissions and limitations under
14
 * the License.
15
 */
16
17
#pragma once
18
19
#include <array>
20
#include <cassert>
21
#include <cstddef>
22
#include <memory>
23
#include <vector>
24
#include <spotify/json/detail/macros.hpp>
25
26
namespace spotify {
27
namespace json {
28
namespace detail {
29
30
template <typename T, std::size_t inline_capacity>
31
struct stack {
32
1.11M
  void push(T value) {
33
1.11M
    if (json_unlikely(_vector)) {
34
1.07M
      _vector->push_back(std::move(value));
35
1.07M
    } else if (json_likely(_inline_size < inline_capacity)) {
36
36.2k
      _array[_inline_size++] = std::move(value);
37
36.2k
    } else {
38
534
      _vector.reset(new std::vector<T>(_array.begin(), _array.end()));
39
534
      _vector->push_back(std::move(value));
40
534
    }
41
1.11M
  }
42
43
37.1k
  T pop() {
44
37.1k
    if (json_unlikely(_vector)) {
45
36.5k
      auto top = _vector->back();
46
36.5k
      _vector->pop_back();
47
36.5k
      return top;
48
36.5k
    } else {
49
572
      assert(_inline_size);
50
572
      return _array[--_inline_size];
51
572
    }
52
37.1k
  }
53
54
 private:
55
  std::array<T, inline_capacity> _array;
56
  std::unique_ptr<std::vector<T>> _vector;
57
  std::size_t _inline_size = 0;
58
};
59
60
}  // namespace detail
61
}  // namespace json
62
}  // namespace spotify