diff --git a/src/core/data_structures/int64_hash_set.h b/src/core/data_structures/int64_hash_set.h index d81cb957..f5a2eea8 100644 --- a/src/core/data_structures/int64_hash_set.h +++ b/src/core/data_structures/int64_hash_set.h @@ -43,7 +43,14 @@ #define INT64_HASH_SET_EMPTY_KEY INT64_MIN /** - * @brief A hash set optimized for storing 64-bit integer keys. + * @brief Highly optimized linear probing hash set for storing 64-bit integer + * keys. + * + * @note `INT64_MIN` (`INT64_HASH_SET_EMPTY_KEY`) is reserved as a sentinel + * value for the hash table to represent an empty slot for optimization + * purposes. Because of this, `Int64HashSet` cannot be used to store + * `INT64_MIN`. The user must make sure that `INT64_MIN` is never inserted as a + * key. */ typedef struct Int64HashSet { int64_t *keys; /**< Array of keys in the hash set. */ diff --git a/src/core/types/CMakeLists.txt b/src/core/types/CMakeLists.txt index de04e52f..88285c2a 100644 --- a/src/core/types/CMakeLists.txt +++ b/src/core/types/CMakeLists.txt @@ -22,12 +22,7 @@ set(HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/tier_to_ptr_chained_hash_map.h) set(SOURCES - ${CMAKE_CURRENT_SOURCE_DIR}/move_array.c - ${CMAKE_CURRENT_SOURCE_DIR}/position_array.c - ${CMAKE_CURRENT_SOURCE_DIR}/tier_array.c - ${CMAKE_CURRENT_SOURCE_DIR}/tier_hash_map.c ${CMAKE_CURRENT_SOURCE_DIR}/tier_position_array.c - ${CMAKE_CURRENT_SOURCE_DIR}/tier_position_hash_set.c - ${CMAKE_CURRENT_SOURCE_DIR}/tier_stack.c) + ${CMAKE_CURRENT_SOURCE_DIR}/tier_position_hash_set.c) target_sources(gamesman_core PRIVATE ${HEADERS} ${SOURCES}) diff --git a/src/core/types/move_array.c b/src/core/types/move_array.c deleted file mode 100644 index a117ed03..00000000 --- a/src/core/types/move_array.c +++ /dev/null @@ -1,53 +0,0 @@ -/** - * @file move_array.c - * @author Robert Shi (robertyishi@berkeley.edu) - * @author GamesCrafters Research Group, UC Berkeley - * Supervised by Dan Garcia - * @brief Dynamic Move array implementation. - * @version 2.0.0 - * @date 2024-12-10 - * - * @copyright This file is part of GAMESMAN, The Finite, Two-person - * Perfect-Information Game Generator released under the GPL: - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -#include "core/types/move_array.h" - -#include "core/data_structures/int64_array.h" -#include "core/types/base.h" - -void MoveArrayInit(MoveArray *array) { Int64ArrayInit(array); } - -void MoveArrayDestroy(MoveArray *array) { Int64ArrayDestroy(array); } - -bool MoveArrayAppend(MoveArray *array, Move move) { - return Int64ArrayPushBack(array, move); -} - -bool MoveArrayPopBack(MoveArray *array) { - if (array->size <= 0) return false; - --array->size; - return true; -} - -void MoveArraySortExplicit(MoveArray *array, - int (*comp)(const void *, const void *)) { - Int64ArraySortExplicit(array, comp); -} - -bool MoveArrayContains(const MoveArray *array, Move move) { - return Int64ArrayContains(array, move); -} diff --git a/src/core/types/move_array.h b/src/core/types/move_array.h index 980aaa6f..edbc7681 100644 --- a/src/core/types/move_array.h +++ b/src/core/types/move_array.h @@ -4,8 +4,6 @@ * @author GamesCrafters Research Group, UC Berkeley * Supervised by Dan Garcia * @brief Dynamic Move array. - * @version 2.0.0 - * @date 2024-12-10 * * @copyright This file is part of GAMESMAN, The Finite, Two-person * Perfect-Information Game Generator released under the GPL: @@ -32,48 +30,80 @@ #include "core/data_structures/int64_array.h" #include "core/types/base.h" -/** @brief Dynamic Move array. */ +/** + * @brief Dynamic `Move` array. + */ typedef Int64Array MoveArray; -/** @brief Initializes the move ARRAY to an empty array. */ -void MoveArrayInit(MoveArray *array); +/** + * @brief Initializes `array`. + * + * @param[out] array Array to initialize. + */ +static inline void MoveArrayInit(MoveArray *array) { Int64ArrayInit(array); } -/** @brief Destroyes the move ARRAY. */ -void MoveArrayDestroy(MoveArray *array); +/** + * @brief Deallocates `array`. + * + * @param[in,out] array Array to deallocate. + */ +static inline void MoveArrayDestroy(MoveArray *array) { + Int64ArrayDestroy(array); +} /** - * @brief Appends MOVE to the end of ARRAY. + * @brief Appends a new `move` to the back of the `array`. + * + * @param[in,out] array Destination array. + * @param[in] move New move. * - * @param array Destination array. - * @param move Move to append. - * @return true on success, - * @return false otherwise. + * @retval true on success. + * @retval false otherwise. */ -bool MoveArrayAppend(MoveArray *array, Move move); +static inline bool MoveArrayAppend(MoveArray *array, Move move) { + return Int64ArrayPushBack(array, move); +} /** - * @brief Removes the last Move in ARRAY. + * @brief Pops the move at the back of the `array`. * - * @param array Target array. - * @return true on success, - * @return false if ARRAY is empty. + * @param[in,out] array Array to pop the move from. + * + * @retval true on success. + * @retval false if the array is empty. */ -bool MoveArrayPopBack(MoveArray *array); +static inline bool MoveArrayPopBack(MoveArray *array) { + if (array->size <= 0) return false; + --array->size; + return true; +} /** - * @brief Sorts the move ARRAY in ascending order using the given COMP-arison - * function. + * @brief Sorts the given `array` according to the given comparison function. * - * @param array Array to be sorted. - * @param comp Comparison function which returns ​a negative integer value if + * @param[in,out] array The array to be sorted. + * @param[in] comp Comparison function which returns a negative integer value if * the first argument is less than the second, a positive integer value if the - * first argument is greater than the second and zero if the arguments are + * first argument is greater than the second, and zero if the arguments are * equivalent. */ -void MoveArraySortExplicit(MoveArray *array, - int (*comp)(const void *, const void *)); +static inline void MoveArraySortExplicit(MoveArray *array, + int (*comp)(const void *, + const void *)) { + Int64ArraySortExplicit(array, comp); +} -/** @brief Returns true if ARRAY contains MOVE, or false otherwise. */ -bool MoveArrayContains(const MoveArray *array, Move move); +/** + * @brief Returns whether the given `array` contains the given `move`. + * + * @param[in] array Array to check. + * @param[in] move Move to look for. + * + * @retval true if the array contains the move. + * @retval false otherwise. + */ +static inline bool MoveArrayContains(const MoveArray *array, Move move) { + return Int64ArrayContains(array, move); +} #endif // GAMESMANONE_CORE_TYPES_MOVE_ARRAY_H_ diff --git a/src/core/types/position_array.c b/src/core/types/position_array.c deleted file mode 100644 index b6a1de41..00000000 --- a/src/core/types/position_array.c +++ /dev/null @@ -1,48 +0,0 @@ -/** - * @file position_array.c - * @author Robert Shi (robertyishi@berkeley.edu) - * @author GamesCrafters Research Group, UC Berkeley - * Supervised by Dan Garcia - * @brief Dynamic Position array implementation. - * @version 1.1.0 - * @date 2025-04-26 - * - * @copyright This file is part of GAMESMAN, The Finite, Two-person - * Perfect-Information Game Generator released under the GPL: - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -#include "core/types/position_array.h" - -#include "core/data_structures/int64_array.h" -#include "core/gamesman_memory.h" -#include "core/types/base.h" - -void PositionArrayInit(PositionArray *array) { Int64ArrayInit(array); } - -void PositionArrayInitAllocator(PositionArray *array, - GamesmanAllocator *allocator) { - Int64ArrayInitAllocator(array, allocator); -} - -void PositionArrayDestroy(PositionArray *array) { Int64ArrayDestroy(array); } - -bool PositionArrayAppend(PositionArray *array, Position position) { - return Int64ArrayPushBack(array, position); -} - -bool PositionArrayContains(PositionArray *array, Position position) { - return Int64ArrayContains(array, position); -} diff --git a/src/core/types/position_array.h b/src/core/types/position_array.h index 28ae805f..f91e76dc 100644 --- a/src/core/types/position_array.h +++ b/src/core/types/position_array.h @@ -4,8 +4,6 @@ * @author GamesCrafters Research Group, UC Berkeley * Supervised by Dan Garcia * @brief Dynamic Position array. - * @version 1.1.0 - * @date 2025-04-26 * * @copyright This file is part of GAMESMAN, The Finite, Two-person * Perfect-Information Game Generator released under the GPL: @@ -33,29 +31,72 @@ #include "core/gamesman_memory.h" #include "core/types/base.h" -/** @brief Dynamic Position array. */ +/** + * @brief Dynamic `Position` array. + */ typedef Int64Array PositionArray; -/** @brief Initializes the position ARRAY to an empty array. */ -void PositionArrayInit(PositionArray *array); +/** + * @brief Initializes `array`. + * + * @param[out] array Array to initialize. + */ +static inline void PositionArrayInit(PositionArray *array) { + Int64ArrayInit(array); +} -void PositionArrayInitAllocator(PositionArray *array, - GamesmanAllocator *allocator); +/** + * @brief Initializes `array` using `allocator` as the underlying memory + * allocator. + * + * @details If `allocator` is `NULL`, the function call is equivalent to + * `PositionArrayInit(array)`. Note that this function does not transfer the + * ownership of `allocator` to the new array object. The caller is responsible + * for releasing its own copy of the allocator. + * + * @param[out] array Array to initialize. + * @param[in,out] allocator Memory allocator to use. + */ +static inline void PositionArrayInitAllocator(PositionArray *array, + GamesmanAllocator *allocator) { + Int64ArrayInitAllocator(array, allocator); +} -/** @brief Destroyes the position ARRAY. */ -void PositionArrayDestroy(PositionArray *array); +/** + * @brief Deallocates `array`. + * + * @param[in,out] array Array to deallocate. + */ +static inline void PositionArrayDestroy(PositionArray *array) { + Int64ArrayDestroy(array); +} /** - * @brief Appends POSITION to the end of ARRAY. + * @brief Appends a new `position` to the back of the `array`. + * + * @param[in,out] array Destination array. + * @param[in] position New position. * - * @param array Destination array. - * @param position Position to append. - * @return true on success, - * @return false otherwise. + * @retval true on success. + * @retval false otherwise. */ -bool PositionArrayAppend(PositionArray *array, Position position); +static inline bool PositionArrayAppend(PositionArray *array, + Position position) { + return Int64ArrayPushBack(array, position); +} -/** @brief Returns true if ARRAY contains POSITION, or false otherwise. */ -bool PositionArrayContains(PositionArray *array, Position position); +/** + * @brief Returns whether the given `array` contains the given `position`. + * + * @param[in] array Array to check. + * @param[in] position Position to look for. + * + * @retval true if the array contains the position. + * @retval false otherwise. + */ +static inline bool PositionArrayContains(PositionArray *array, + Position position) { + return Int64ArrayContains(array, position); +} #endif // GAMESMANONE_CORE_TYPES_POSITION_ARRAY_H_ diff --git a/src/core/types/simd.h b/src/core/types/simd.h index c24eb853..5223e5a7 100644 --- a/src/core/types/simd.h +++ b/src/core/types/simd.h @@ -29,6 +29,10 @@ #include +#ifdef GAMESMAN_HAS_BMI1 +#include +#endif // GAMESMAN_HAS_BMI1 + /** * @brief 128-bit SIMD vector of 16 signed 8-bit integers. */ @@ -44,12 +48,6 @@ typedef uint8_t U8x16 __attribute__((vector_size(16))); */ typedef uint64_t U64x2 __attribute__((vector_size(16))); -#include - -#ifdef GAMESMAN_HAS_BMI1 -#include -#endif // GAMESMAN_HAS_BMI1 - /** * @brief Extract bits from unsigned 64-bit integer `val` at the corresponding * bit locations specified by mask to contiguous low bits; the remaining diff --git a/src/core/types/tier_array.c b/src/core/types/tier_array.c deleted file mode 100644 index da337fa0..00000000 --- a/src/core/types/tier_array.c +++ /dev/null @@ -1,57 +0,0 @@ -/** - * @file tier_array.c - * @author Robert Shi (robertyishi@berkeley.edu) - * @author GamesCrafters Research Group, UC Berkeley - * Supervised by Dan Garcia - * @brief Dynamic tier array implementation. - * @version 1.1.0 - * @date 2024-09-07 - * - * @copyright This file is part of GAMESMAN, The Finite, Two-person - * Perfect-Information Game Generator released under the GPL: - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -#include "core/types/tier_array.h" - -#include "core/data_structures/int64_array.h" -#include "core/types/base.h" - -void TierArrayInit(TierArray *array) { Int64ArrayInit(array); } - -bool TierArrayInitCopy(TierArray *dest, const TierArray *src) { - return Int64ArrayInitCopy(dest, src); -} - -void TierArrayDestroy(TierArray *array) { Int64ArrayDestroy(array); } - -bool TierArrayAppend(TierArray *array, Tier tier) { - return Int64ArrayPushBack(array, tier); -} - -bool TierArrayRemoveUnordered(TierArray *array, Tier tier) { - return Int64ArrayRemoveUnordered(array, tier); -} - -void TierArrayPopBack(TierArray *array) { Int64ArrayPopBack(array); } - -Tier TierArrayBack(const TierArray *array) { return Int64ArrayBack(array); } - -bool TierArrayEmpty(const TierArray *array) { return Int64ArrayEmpty(array); } - -void TierArraySortExplicit(TierArray *array, - int (*comp)(const void *, const void *)) { - Int64ArraySortExplicit(array, comp); -} diff --git a/src/core/types/tier_array.h b/src/core/types/tier_array.h index 488a8d0c..f564d746 100644 --- a/src/core/types/tier_array.h +++ b/src/core/types/tier_array.h @@ -4,8 +4,6 @@ * @author GamesCrafters Research Group, UC Berkeley * Supervised by Dan Garcia * @brief Dynamic tier array. - * @version 1.1.0 - * @date 2024-09-07 * * @copyright This file is part of GAMESMAN, The Finite, Two-person * Perfect-Information Game Generator released under the GPL: @@ -32,62 +30,120 @@ #include "core/data_structures/int64_array.h" #include "core/types/base.h" -/** @brief Dynamic Tier array. */ +/** + * @brief Dynamic `Tier` array. + */ typedef Int64Array TierArray; -/** @brief Initializes the Tier ARRAY. */ -void TierArrayInit(TierArray *array); +/** + * @brief Initializes `array`. + * + * @param[out] array Array to initialize. + */ +static inline void TierArrayInit(TierArray *array) { Int64ArrayInit(array); } -/** @brief Initializes the DEST tier array as a copy of tier array SRC. */ -bool TierArrayInitCopy(TierArray *dest, const TierArray *src); +/** + * @brief Initializes `dest` array to be a copy of the `src` array. + * + * @details If `src` uses a custom memory allocator, a new reference will be + * copied to the `dest` array. + * + * @param[out] dest Array to initialize. + * @param[in] src Source array to copy from. + * + * @retval true on success. + * @retval false otherwise. + */ +static inline bool TierArrayInitCopy(TierArray *dest, const TierArray *src) { + return Int64ArrayInitCopy(dest, src); +} -/** @brief Destroys the Tier ARRAY. */ -void TierArrayDestroy(TierArray *array); +/** + * @brief Deallocates `array`. + * + * @param[in,out] array Array to deallocate. + */ +static inline void TierArrayDestroy(TierArray *array) { + Int64ArrayDestroy(array); +} /** - * @brief Appends TIER to the end of the given Tier ARRAY. + * @brief Appends a new `tier` to the back of the `array`. * - * @param array Destination Tier array. - * @param tier Tier to append. - * @return true on success, - * @return false otherwise. + * @param[in,out] array Destination array. + * @param[in] tier New tier. + * + * @retval true on success. + * @retval false otherwise. */ -bool TierArrayAppend(TierArray *array, Tier tier); +static inline bool TierArrayAppend(TierArray *array, Tier tier) { + return Int64ArrayPushBack(array, tier); +} /** - * @brief Removes the first occurrence of TIER from ARRAY if it exists. + * @brief Removes the first occurrence of `tier` from `array`, if it exists. + * + * @param[in,out] array Array of `Tier`. + * @param[in] tier Value to remove. * - * @param array Tier array. - * @param tier Tier to remove. - * @return true if TIER exists in ARRAY, or - * @return false otherwise. + * @retval true if `tier` exists in `array` and was removed. + * @retval false otherwise. */ -bool TierArrayRemoveUnordered(TierArray *array, Tier tier); +static inline bool TierArrayRemoveUnordered(TierArray *array, Tier tier) { + return Int64ArrayRemoveUnordered(array, tier); +} /** - * @brief Pops the item at the back of the ARRAY. Calling this function on an - * empty ARRAY results in undefined behavior. + * @brief Pops the item at the back of the `array`. + * + * @details Calling this function on an empty `array` results in undefined + * behavior. + * + * @param[in,out] array Array to pop the item from. */ -void TierArrayPopBack(TierArray *array); +static inline void TierArrayPopBack(TierArray *array) { + Int64ArrayPopBack(array); +} /** - * @brief Returns the Tier at the back of the array. Calling this function on an - * empty ARRAY results in undefined behavior. + * @brief Returns the item at the back of `array`. + * + * @details Calling this function on an empty `array` results in undefined + * behavior. + * + * @param[in] array Array to get the item from. + * + * @return Item at the back of `array`. */ -Tier TierArrayBack(const TierArray *array); +static inline Tier TierArrayBack(const TierArray *array) { + return Int64ArrayBack(array); +} -/** @brief Returns true if ARRAY is empty, or false otherwise. */ -bool TierArrayEmpty(const TierArray *array); +/** + * @brief Returns whether the given `array` is empty. + * + * @param[in] array Array to check. + * + * @retval true if the array is empty. + * @retval false otherwise. + */ +static inline bool TierArrayEmpty(const TierArray *array) { + return Int64ArrayEmpty(array); +} /** - * @brief Sorts the given ARRAY according to the given comparison function. - * @param array The array to be sorted. - * @param comp Comparison function which returns ​a negative integer value if + * @brief Sorts the given `array` according to the given comparison function. + * + * @param[in,out] array The array to be sorted. + * @param[in] comp Comparison function which returns a negative integer value if * the first argument is less than the second, a positive integer value if the * first argument is greater than the second, and zero if the arguments are - * equivalent. Both parameters are assumed to be pointers to \c Tier objects. + * equivalent. */ -void TierArraySortExplicit(TierArray *array, - int (*comp)(const void *, const void *)); +static inline void TierArraySortExplicit(TierArray *array, + int (*comp)(const void *, + const void *)) { + Int64ArraySortExplicit(array, comp); +} #endif // GAMESMANONE_CORE_TYPES_TIER_ARRAY_H_ diff --git a/src/core/types/tier_hash_map.c b/src/core/types/tier_hash_map.c deleted file mode 100644 index 209ad867..00000000 --- a/src/core/types/tier_hash_map.c +++ /dev/null @@ -1,77 +0,0 @@ -/** - * @file tier_hash_map.c - * @author Robert Shi (robertyishi@berkeley.edu) - * @author GamesCrafters Research Group, UC Berkeley - * Supervised by Dan Garcia - * @brief Implementation of linear-probing Tier hash map. - * @version 1.0.1 - * @date 2024-09-02 - * - * @copyright This file is part of GAMESMAN, The Finite, Two-person - * Perfect-Information Game Generator released under the GPL: - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -#include "core/types/tier_hash_map.h" - -#include - -#include "core/data_structures/int64_hash_map.h" -#include "core/gamesman_memory.h" -#include "core/types/base.h" - -void TierHashMapInit(TierHashMap *map, double max_load_factor) { - Int64HashMapInit(map, max_load_factor); -} - -void TierHashMapInitAllocator(TierHashMap *map, double max_load_factor, - GamesmanAllocator *allocator) { - Int64HashMapInitAllocator(map, max_load_factor, allocator); -} - -void TierHashMapDestroy(TierHashMap *map) { Int64HashMapDestroy(map); } - -TierHashMapIterator TierHashMapGet(TierHashMap *map, Tier key) { - return Int64HashMapGet(map, key); -} - -bool TierHashMapSet(TierHashMap *map, Tier tier, int64_t value) { - return Int64HashMapSet(map, tier, value); -} - -bool TierHashMapContains(const TierHashMap *map, Tier tier) { - return Int64HashMapContains(map, tier); -} - -TierHashMapIterator TierHashMapBegin(TierHashMap *map) { - return Int64HashMapBegin(map); -} - -Tier TierHashMapIteratorKey(const TierHashMapIterator *it) { - return Int64HashMapIteratorKey(it); -} - -int64_t TierHashMapIteratorValue(const TierHashMapIterator *it) { - return Int64HashMapIteratorValue(it); -} - -bool TierHashMapIteratorIsValid(const TierHashMapIterator *it) { - return Int64HashMapIteratorIsValid(it); -} - -bool TierHashMapIteratorNext(TierHashMapIterator *iterator, Tier *tier, - int64_t *value) { - return Int64HashMapIteratorNext(iterator, tier, value); -} diff --git a/src/core/types/tier_hash_map.h b/src/core/types/tier_hash_map.h index 8f51ff46..998013ee 100644 --- a/src/core/types/tier_hash_map.h +++ b/src/core/types/tier_hash_map.h @@ -5,8 +5,6 @@ * Supervised by Dan Garcia * @brief Linear-probing Tier hash map that maps Tiers to 64-bit signed * integers. - * @version 1.0.1 - * @date 2024-09-02 * * @copyright This file is part of GAMESMAN, The Finite, Two-person * Perfect-Information Game Generator released under the GPL: @@ -55,7 +53,9 @@ typedef Int64HashMapIterator TierHashMapIterator; * value will be set to 0.25 and 0.75, respectively, regardless of the * user-specified value. */ -void TierHashMapInit(TierHashMap *map, double max_load_factor); +static inline void TierHashMapInit(TierHashMap *map, double max_load_factor) { + Int64HashMapInit(map, max_load_factor); +} /** * @brief Initializes the given \p map to an empty map with maximum load @@ -73,11 +73,16 @@ void TierHashMapInit(TierHashMap *map, double max_load_factor); * @param allocator Memory allocator to use. If \c NULL is passed, the effect is * equivalent to calling TierHashMapInit. */ -void TierHashMapInitAllocator(TierHashMap *map, double max_load_factor, - GamesmanAllocator *allocator); +static inline void TierHashMapInitAllocator(TierHashMap *map, + double max_load_factor, + GamesmanAllocator *allocator) { + Int64HashMapInitAllocator(map, max_load_factor, allocator); +} /** @brief Destroys the Tier hash map MAP. */ -void TierHashMapDestroy(TierHashMap *map); +static inline void TierHashMapDestroy(TierHashMap *map) { + Int64HashMapDestroy(map); +} /** * @brief Returns an iterator to the MAP entry containing the given KEY. @@ -89,7 +94,9 @@ void TierHashMapDestroy(TierHashMap *map); * @return TierHashMapIterator pointing to the entry with KEY, or invalid * if KEY is not found in MAP. */ -TierHashMapIterator TierHashMapGet(TierHashMap *map, Tier key); +static inline TierHashMapIterator TierHashMapGet(TierHashMap *map, Tier key) { + return Int64HashMapGet(map, key); +} /** * @brief Sets the entry with key equal to TIER in MAP to the given VALUE and @@ -103,13 +110,17 @@ TierHashMapIterator TierHashMapGet(TierHashMap *map, Tier key); * @return true on success, * @return false otherwise. */ -bool TierHashMapSet(TierHashMap *map, Tier tier, int64_t value); +static inline bool TierHashMapSet(TierHashMap *map, Tier tier, int64_t value) { + return Int64HashMapSet(map, tier, value); +} /** * @brief Returns true if the given MAP contains an entry with key equal to * TIER, or false otherwise. */ -bool TierHashMapContains(const TierHashMap *map, Tier tier); +static inline bool TierHashMapContains(const TierHashMap *map, Tier tier) { + return Int64HashMapContains(map, tier); +} /** * @brief Returns an invalid iterator to the entry before the first entry of @@ -118,18 +129,10 @@ bool TierHashMapContains(const TierHashMap *map, Tier tier); * @note This function is designed to be used in conjunction with * TierHashMapIteratorNext to iterate through all the entries in the Tier hash * map. - * - * @example - * TierHashMapIterator it = TierHashMapBegin(map); - * Tier key; - * int64_t value; - * - * // While the next entry exists... - * while (TierHashMapIteratorNext(&it, &key, &value)) { - * // Do stuff with key and value... - * } */ -TierHashMapIterator TierHashMapBegin(TierHashMap *map); +static inline TierHashMapIterator TierHashMapBegin(TierHashMap *map) { + return Int64HashMapBegin(map); +} /** * @brief Returns the Tier that was used as the key to the entry IT is pointing @@ -140,7 +143,9 @@ TierHashMapIterator TierHashMapBegin(TierHashMap *map); * @param it Tier hash map iterator. * @return Key to the entry. */ -Tier TierHashMapIteratorKey(const TierHashMapIterator *it); +static inline Tier TierHashMapIteratorKey(const TierHashMapIterator *it) { + return Int64HashMapIteratorKey(it); +} /** * @brief Returns the value of the entry IT is pointing to. The user should @@ -151,10 +156,14 @@ Tier TierHashMapIteratorKey(const TierHashMapIterator *it); * @param it Tier hash map iterator. * @return Value of the entry. */ -int64_t TierHashMapIteratorValue(const TierHashMapIterator *it); +static inline int64_t TierHashMapIteratorValue(const TierHashMapIterator *it) { + return Int64HashMapIteratorValue(it); +} /** @brief Returns true if the given IT-erator is valid, or false otherwise. */ -bool TierHashMapIteratorIsValid(const TierHashMapIterator *it); +static inline bool TierHashMapIteratorIsValid(const TierHashMapIterator *it) { + return Int64HashMapIteratorIsValid(it); +} /** * @brief Moves iterator IT to the next valid entry inside the Tier hash map @@ -174,7 +183,9 @@ bool TierHashMapIteratorIsValid(const TierHashMapIterator *it); * @return true if next entry exists, * @return false otherwise. */ -bool TierHashMapIteratorNext(TierHashMapIterator *iterator, Tier *tier, - int64_t *value); +static inline bool TierHashMapIteratorNext(TierHashMapIterator *iterator, + Tier *tier, int64_t *value) { + return Int64HashMapIteratorNext(iterator, tier, value); +} #endif // GAMESMANONE_CORE_TYPES_TIER_HASH_MAP_H_ diff --git a/src/core/types/tier_hash_set.h b/src/core/types/tier_hash_set.h index e917fb98..cd6e9fbb 100644 --- a/src/core/types/tier_hash_set.h +++ b/src/core/types/tier_hash_set.h @@ -4,8 +4,6 @@ * @author GamesCrafters Research Group, UC Berkeley * Supervised by Dan Garcia * @brief Linear-probing Tier hash set. - * @version 2.0.0 - * @date 2025-05-11 * * @copyright This file is part of GAMESMAN, The Finite, Two-person * Perfect-Information Game Generator released under the GPL: @@ -32,52 +30,52 @@ #include "core/data_structures/int64_hash_set.h" #include "core/types/base.h" -/** @brief Linear-probing Tier hash set using Int64HashSet as underlying type. +/** + * @brief A linear-probing Tier hash set using `Int64HashSet` as the underlying + * type. */ typedef Int64HashSet TierHashSet; /** - * @brief Initializes the given \p set to an empty set with maximum load - * factor \p max_load_factor. + * @brief Initializes a Tier hash set. * - * @param set Set to initialize. - * @param max_load_factor Set maximum load factor of \p set to this value. The - * hash set will automatically expand its capacity if (double)size/capacity is - * greater than \p max_load_factor. A small value trades memory for speed - * whereas a large value trades speed for memory. This value is restricted to be - * in the range [0.25, 0.75] to provide optimal performance. The actual max load - * factor is capped at 0.25 and 0.75 respectively if the user passes a value - * that is smaller than 0.25 or greater than 0.75. + * @param[out] set The `TierHashSet` to initialize. + * @param[in] max_load_factor The maximum load factor, clamped to [0.5, 0.8]. */ static inline void TierHashSetInit(TierHashSet *set, double max_load_factor) { Int64HashSetInit(set, max_load_factor); } -/** @brief Deallocates the given \p set. */ +/** + * @brief Frees the memory associated with the hash set and resets its state. + * + * @param[in,out] set The `TierHashSet` to destroy. + */ static inline void TierHashSetDestroy(TierHashSet *set) { Int64HashSetDestroy(set); } /** - * @brief Tests if \p tier is in \p set. + * @brief Checks if a specific Tier is present in the hash set. + * + * @param[in] set The `TierHashSet` to search. + * @param[in] tier The `Tier` value to look for. * - * @param set Set from which the given \p tier is looked up. - * @param tier Tier value to look for. - * @return true if \p set contains \p tier, or - * @return false otherwise. + * @retval true The `tier` is present in the set. + * @retval false The `tier` is not present, or the set is uninitialized. */ static inline bool TierHashSetContains(const TierHashSet *set, Tier tier) { return Int64HashSetContains(set, tier); } /** - * @brief Adds \p tier to the given \p set or does nothing if \p set - * already contains \p tier. + * @brief Adds a Tier value to the hash set. + * + * @param[in,out] set The `TierHashSet` to add the Tier to. + * @param[in] tier The `Tier` value to add. * - * @param set Set to add \p tier to. - * @param tier Tier to be added. - * @return \c true if \p tier was added into \p set as a new key, or - * @return \c false if \p set already contains \p tier or an error occurred. + * @retval true The `tier` was successfully added. + * @retval false The `tier` already exists, or memory allocation failed. */ static inline bool TierHashSetAdd(TierHashSet *set, Tier tier) { return Int64HashSetAdd(set, tier); diff --git a/src/core/types/tier_position_array.c b/src/core/types/tier_position_array.c index 10c26957..074c0436 100644 --- a/src/core/types/tier_position_array.c +++ b/src/core/types/tier_position_array.c @@ -4,8 +4,6 @@ * @author GamesCrafters Research Group, UC Berkeley * Supervised by Dan Garcia * @brief Dynamic TierPosition array implementation. - * @version 1.0.1 - * @date 2024-12-10 * * @copyright This file is part of GAMESMAN, The Finite, Two-person * Perfect-Information Game Generator released under the GPL: @@ -70,15 +68,3 @@ bool TierPositionArrayAppend(TierPositionArray *array, TierPosition TierPositionArrayBack(const TierPositionArray *array) { return array->array[array->size - 1]; } - -bool TierPositionArrayContains(const TierPositionArray *array, - TierPosition target) { - for (int64_t i = 0; i < array->size; ++i) { - if (array->array[i].position == target.position && - array->array[i].tier == target.tier) { - return true; - } - } - - return false; -} diff --git a/src/core/types/tier_position_array.h b/src/core/types/tier_position_array.h index fc1ab3a6..834b113b 100644 --- a/src/core/types/tier_position_array.h +++ b/src/core/types/tier_position_array.h @@ -4,8 +4,6 @@ * @author GamesCrafters Research Group, UC Berkeley * Supervised by Dan Garcia * @brief Dynamic TierPosition array. - * @version 1.0.1 - * @date 2024-12-10 * * @copyright This file is part of GAMESMAN, The Finite, Two-person * Perfect-Information Game Generator released under the GPL: @@ -32,40 +30,51 @@ #include "core/types/base.h" -/** @brief Dynamic TierPosition Array. */ +/** + * @brief Dynamic `TierPosition` array. + */ typedef struct TierPositionArray { - TierPosition *array; /**< Array contents. */ - int64_t size; /**< Size of the array in number of items. */ - int64_t capacity; /**< Capacity of the array in number of items. */ + TierPosition *array; /**< The actual array. */ + int64_t size; /**< Number of items in the array. */ + int64_t capacity; /**< Current capacity of the array. */ } TierPositionArray; -/** @brief Initializes the TierPosition ARRAY to an empty array. */ +/** + * @brief Initializes `array`. + * + * @param[out] array Array to initialize. + */ void TierPositionArrayInit(TierPositionArray *array); -/** @brief Destroyes the TierPosition ARRAY. */ +/** + * @brief Deallocates `array`. + * + * @param[in,out] array Array to deallocate. + */ void TierPositionArrayDestroy(TierPositionArray *array); /** - * @brief Appends TIER_POSITION to the end of ARRAY. + * @brief Appends a new `tier_position` to the back of the `array`. + * + * @param[in,out] array Destination array. + * @param[in] tier_position New item. * - * @param array Destination array. - * @param position Tier position to append. - * @return true on success, - * @return false otherwise. + * @retval true on success. + * @retval false otherwise. */ bool TierPositionArrayAppend(TierPositionArray *array, TierPosition tier_position); /** - * @brief Returns the last TierPosition in ARRAY. + * @brief Returns the item at the back of `array`. * - * @param array Source tier position array. - * @return Last item in ARRAY. + * @details Calling this function on an empty `array` results in undefined + * behavior. + * + * @param[in] array Array to get the item from. + * + * @return Item at the back of `array`. */ TierPosition TierPositionArrayBack(const TierPositionArray *array); -/** @brief Returns whether the given TierPosition ARRAY contains the TARGET. */ -bool TierPositionArrayContains(const TierPositionArray *array, - TierPosition target); - #endif // GAMESMANONE_CORE_TYPES_TIER_POSITION_ARRAY_H_ diff --git a/src/core/types/tier_position_hash_set.c b/src/core/types/tier_position_hash_set.c index d6ecd439..ab6a233b 100644 --- a/src/core/types/tier_position_hash_set.c +++ b/src/core/types/tier_position_hash_set.c @@ -30,6 +30,7 @@ #include #include +#include "core/data_structures/hash.h" #include "core/gamesman_memory.h" #include "core/types/base.h" @@ -42,42 +43,6 @@ void TierPositionHashSetInit(TierPositionHashSet *set, double max_load_factor) { set->capacity_mask = -1; } -// This function is adapted from Google CityHash -// Copyright (c) 2011 Google, Inc. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -// CityHash, by Geoff Pike and Jyrki Alakuijala -// -// http://code.google.com/p/cityhash/ -static uint64_t Hash128to64(uint64_t lo, uint64_t hi) { - // Murmur-inspired hashing. - const uint64_t kMul = 0x9ddfea08eb382d69ULL; - uint64_t a = (lo ^ hi) * kMul; - a ^= (a >> 47); - uint64_t b = (hi ^ a) * kMul; - b ^= (b >> 47); - b *= kMul; - - return b; -} - static int64_t TierPositionHashSetHash(TierPosition key, int64_t mask) { int64_t a = (int64_t)key.tier; int64_t b = (int64_t)key.position; diff --git a/src/core/types/tier_stack.c b/src/core/types/tier_stack.c deleted file mode 100644 index 0c3d0c89..00000000 --- a/src/core/types/tier_stack.c +++ /dev/null @@ -1,44 +0,0 @@ -/** - * @file tier_stack.c - * @author Robert Shi (robertyishi@berkeley.edu) - * @author GamesCrafters Research Group, UC Berkeley - * Supervised by Dan Garcia - * @brief Tier stack implementation. - * @version 1.0.1 - * @date 2024-12-10 - * - * @copyright This file is part of GAMESMAN, The Finite, Two-person - * Perfect-Information Game Generator released under the GPL: - * - * This program is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free Software - * Foundation, either version 3 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -#include "core/types/tier_stack.h" - -#include "core/data_structures/int64_array.h" -#include "core/types/base.h" - -void TierStackInit(TierStack *stack) { Int64ArrayInit(stack); } - -void TierStackDestroy(TierStack *stack) { Int64ArrayDestroy(stack); } - -bool TierStackPush(TierStack *stack, Tier tier) { - return Int64ArrayPushBack(stack, tier); -} - -void TierStackPop(TierStack *stack) { Int64ArrayPopBack(stack); } - -Tier TierStackTop(const TierStack *stack) { return Int64ArrayBack(stack); } - -bool TierStackEmpty(const TierStack *stack) { return Int64ArrayEmpty(stack); } diff --git a/src/core/types/tier_stack.h b/src/core/types/tier_stack.h index 5b4ba776..6919ce96 100644 --- a/src/core/types/tier_stack.h +++ b/src/core/types/tier_stack.h @@ -4,8 +4,6 @@ * @author GamesCrafters Research Group, UC Berkeley * Supervised by Dan Garcia * @brief Tier stack. - * @version 1.0.1 - * @date 2024-12-10 * * @copyright This file is part of GAMESMAN, The Finite, Two-person * Perfect-Information Game Generator released under the GPL: @@ -32,43 +30,74 @@ #include "core/data_structures/int64_array.h" #include "core/types/base.h" -/** @brief Dynamic Tier stack using Int64Array. */ +/** + * @brief Dynamic `Tier` stack using `Int64Array`. + */ typedef Int64Array TierStack; -/** @brief Initializes Tier stack STACK. */ -void TierStackInit(TierStack *stack); +/** + * @brief Initializes `stack`. + * + * @param[out] stack Stack to initialize. + */ +static inline void TierStackInit(TierStack *stack) { Int64ArrayInit(stack); } -/** @brief Destroys Tier stack STACK. */ -void TierStackDestroy(TierStack *stack); +/** + * @brief Deallocates `stack`. + * + * @param[in,out] stack Stack to deallocate. + */ +static inline void TierStackDestroy(TierStack *stack) { + Int64ArrayDestroy(stack); +} /** - * @brief Pushes a new TIER into the STACK. + * @brief Pushes a new `tier` into the `stack`. * - * @param stack Destination stack. - * @param tier New tier. - * @return true on success, - * @return false otherwise. + * @param[in,out] stack Destination stack. + * @param[in] tier New tier. + * + * @retval true on success. + * @retval false otherwise. */ -bool TierStackPush(TierStack *stack, Tier tier); +static inline bool TierStackPush(TierStack *stack, Tier tier) { + return Int64ArrayPushBack(stack, tier); +} /** - * @brief Pops a tier from the STACK. Calling this function on an empty STACK - * results in undefined behavior. + * @brief Pops a tier from the `stack`. + * + * @details Calling this function on an empty `stack` results in undefined + * behavior. * - * @param stack Stack to pop the tier from. + * @param[in,out] stack Stack to pop the tier from. */ -void TierStackPop(TierStack *stack); +static inline void TierStackPop(TierStack *stack) { Int64ArrayPopBack(stack); } /** - * @brief Returns the item at the top of the STACK. Calling this function on an - * empty STACK results in undefined behavior. + * @brief Returns the item at the top of the `stack`. + * + * @details Calling this function on an empty `stack` results in undefined + * behavior. + * + * @param[in] stack Stack to peek into. * - * @param stack Stack to peak into. - * @return Tier at the top of STACK. + * @return Tier at the top of `stack`. */ -Tier TierStackTop(const TierStack *stack); +static inline Tier TierStackTop(const TierStack *stack) { + return Int64ArrayBack(stack); +} -/** @brief Returns true if the given STACK is empty, or false otherwise. */ -bool TierStackEmpty(const TierStack *stack); +/** + * @brief Returns whether the given `stack` is empty. + * + * @param[in] stack Stack to check. + * + * @retval true if the stack is empty. + * @retval false otherwise. + */ +static inline bool TierStackEmpty(const TierStack *stack) { + return Int64ArrayEmpty(stack); +} #endif // GAMESMANONE_CORE_TYPES_TIER_STACK_H_