folly/folly/container/Reserve.h

/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <stdexcept>

#include <folly/Likely.h>
#include <folly/Traits.h>
#include <folly/Utility.h>
#include <folly/lang/Exception.h>

namespace folly {

namespace detail {

detect_capacity;

detect_bucket_count;

detect_max_load_factor;

detect_reserve;

container_detect_reserve;

} // namespace detail

/**
 * Avoids quadratic behavior that could arise from c.reserve(c.size() + N).
 *
 * May reserve more than N in order to effect geometric growth.  Useful when
 * c.size() is unknown, but more elements must be added by discrete operations.
 * For example: N emplace calls in a loop, or a series of range inserts, the sum
 * of their sizes being N.  Behaves like reserve() if the container is empty.
 */
struct grow_capacity_by_fn {};

inline constexpr grow_capacity_by_fn grow_capacity_by{};

/**
 * Useful when writing generic code that handles containers.
 *
 * Examples:
 *  - std::unordered_map provides reserve(), but std::map does not
 *  - std::vector provides reserve(), but std::deque and std::list do not
 */
struct reserve_if_available_fn {};

inline constexpr reserve_if_available_fn reserve_if_available{};

} // namespace folly