<commit_before>#ifndef RESIZE_H
#define RESIZE_H

#include <cstddef>
#include <cstdint>

#include <algorithm>
#include <iterator>

namespace cs375 {

/**
 * Determines whether a table should resize, given a maximum load factor,
 * a number of buckets, and a desired number of elements.
 *
 * @param max_load_factor the maximum load factor (number of elements per bucket).
 * @param num_buckets the number of buckets.
 * @param num_elements the (desired) number of elements.
 * @return Whether the table should resize.
 */
constexpr bool should_resize(float max_load_factor, std::size_t num_buckets, std::size_t num_elements) {
   return static_cast<std::size_t>(max_load_factor * static_cast<long double>(num_buckets)) < num_elements;
}

static_assert(!should_resize(0.75f, 4, 3), "No need to resize a table with just the right number of elements");
static_assert(should_resize(0.75f, 4, 4), "Must resize a table with one too many elements");

/**
 * Determine the next table size in buckets, which is at least a given size.
 *
 * @param current_size the current size in buckets.
 * @return The next size in buckets.
 */
static std::size_t next_table_size(std::size_t current_size) {
    // List of primes ripped from libstdc++'s implementation.
    // Includes SIZE_MAX to avoid that edge case.
    // Assumes size_t is either 32- or 64-bit.
    constexpr std::size_t primes[] = {
        2u, 3u, 5u, 7u, 11u, 13u, 17u, 19u, 23u, 29u, 31u,
        37u, 41u, 43u, 47u, 53u, 59u, 61u, 67u, 71u, 73u, 79u,
        83u, 89u, 97u, 103u, 109u, 113u, 127u, 137u, 139u, 149u,
        157u, 167u, 179u, 193u, 199u, 211u, 227u, 241u, 257u,
        277u, 293u, 313u, 337u, 359u, 383u, 409u, 439u, 467u,
        503u, 541u, 577u, 619u, 661u, 709u, 761u, 823u, 887u,
        953u, 1031u, 1109u, 1193u, 1289u, 1381u, 1493u, 1613u,
        1741u, 1879u, 2029u, 2179u, 2357u, 2549u, 2753u, 2971u,
        3209u, 3469u, 3739u, 4027u, 4349u, 4703u, 5087u, 5503u,
        5953u, 6427u, 6949u, 7517u, 8123u, 8783u, 9497u, 10273u,
        11113u, 12011u, 12983u, 14033u, 15173u, 16411u, 17749u,
        19183u, 20753u, 22447u, 24281u, 26267u, 28411u, 30727u,
        33223u, 35933u, 38873u, 42043u, 45481u, 49201u, 53201u,
        57557u, 62233u, 67307u, 72817u, 78779u, 85229u, 92203u,
        99733u, 107897u, 116731u, 126271u, 136607u, 147793u,
        159871u, 172933u, 187091u, 202409u, 218971u, 236897u,
        256279u, 277261u, 299951u, 324503u, 351061u, 379787u,
        410857u, 444487u, 480881u, 520241u, 562841u, 608903u,
        658753u, 712697u, 771049u, 834181u, 902483u, 976369u,
        1056323u, 1142821u, 1236397u, 1337629u, 1447153u, 1565659u,
        1693859u, 1832561u, 1982627u, 2144977u, 2320627u, 2510653u,
        2716249u, 2938679u, 3179303u, 3439651u, 3721303u, 4026031u,<commit_msg>Better explain next_table_size<commit_after>#ifndef RESIZE_H
#define RESIZE_H

#include <cstddef>
#include <cstdint>

#include <algorithm>
#include <iterator>

namespace cs375 {

/**
 * Determines whether a table should resize, given a maximum load factor,
 * a number of buckets, and a desired number of elements.
 *
 * @param max_load_factor the maximum load factor (number of elements per bucket).
 * @param num_buckets the number of buckets.
 * @param num_elements the (desired) number of elements.
 * @return Whether the table should resize.
 */
constexpr bool should_resize(float max_load_factor, std::size_t num_buckets, std::size_t num_elements) {
   return static_cast<std::size_t>(max_load_factor * static_cast<long double>(num_buckets)) < num_elements;
}

static_assert(!should_resize(0.75f, 4, 3), "No need to resize a table with just the right number of elements");
static_assert(should_resize(0.75f, 4, 4), "Must resize a table with one too many elements");

/**
 * Determine the next table size in buckets, which is at least a given size.
 * The next table size is the first prime greater than or equal to
 * twice the given size; if that prime falls outside std::size_t's range,
 * the next table size is SIZE_MAX.
 *
 * @param current_size the current size in buckets.
 * @return The next size in buckets.
 */
static std::size_t next_table_size(std::size_t current_size) {
    // List of primes ripped from libstdc++'s implementation.
    // Includes SIZE_MAX to avoid that edge case.
    // Assumes size_t is either 32- or 64-bit.
    constexpr std::size_t primes[] = {
        2u, 3u, 5u, 7u, 11u, 13u, 17u, 19u, 23u, 29u, 31u,
        37u, 41u, 43u, 47u, 53u, 59u, 61u, 67u, 71u, 73u, 79u,
        83u, 89u, 97u, 103u, 109u, 113u, 127u, 137u, 139u, 149u,
        157u, 167u, 179u, 193u, 199u, 211u, 227u, 241u, 257u,
        277u, 293u, 313u, 337u, 359u, 383u, 409u, 439u, 467u,
        503u, 541u, 577u, 619u, 661u, 709u, 761u, 823u, 887u,
        953u, 1031u, 1109u, 1193u, 1289u, 1381u, 1493u, 1613u,
        1741u, 1879u, 2029u, 2179u, 2357u, 2549u, 2753u, 2971u,
        3209u, 3469u, 3739u, 4027u, 4349u, 4703u, 5087u, 5503u,
        5953u, 6427u, 6949u, 7517u, 8123u, 8783u, 9497u, 10273u,
        11113u, 12011u, 12983u, 14033u, 15173u, 16411u, 17749u,
        19183u, 20753u, 22447u, 24281u, 26267u, 28411u, 30727u,
        33223u, 35933u, 38873u, 42043u, 45481u, 49201u, 53201u,
        57557u, 62233u, 67307u, 72817u, 78779u, 85229u, 92203u,
        99733u, 107897u, 116731u, 126271u, 136607u, 147793u,
        159871u, 172933u, 187091u, 202409u, 218971u, 236897u,
        256279u, 277261u, 299951u, 324503u, 351061u, 379787u,
        410857u, 444487u, 480881u, 520241u, 562841u, 608903u,
        658753u, 712697u, 771049u, 834181u, 902483u, 976369u,
        1056323u, 1142821u, 1236397u, 1337629u, 1447153u, 1565659u,
        1693859u, 1832561u, 1982627u, 2144977u, 2320627u, 2510653u,
        2716249u, 2938679u, 3179303u, 3439651u, 3721303u, 4026031u,
{'fraction_non_alphanumeric': 0.1057825484764543, 'fraction_numerical': 0.25554016620498615, 'mean_word_length': 3.7197712418300655, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5650602', 'n_tokens_mistral': 3446, 'n_tokens_neox': 2274, 'n_words': 687}