BEEP-8 Helper Lib 1.0.0
Loading...
Searching...
No Matches
sublibc.h
Go to the documentation of this file.
1
56#pragma once
57#include <string.h>
58#include <cstddef>
59#include <cstdio>
60#include <string>
61#include <beep8.h>
62#include <mt.h>
63
76template < typename TYPE, std::size_t SIZE >
77std::size_t numof(const TYPE (&)[SIZE]) {
78 return SIZE;
79}
80
110template <class T>
111constexpr auto wrap_at(const T& c, std::size_t i) -> const typename T::value_type&
112{
113 return c[i % c.size()];
114}
115
148template <class T>
149constexpr auto clamp_to_edge(const T& c, std::size_t i) -> const typename T::value_type& {
150 if (i >= c.size()) {
151 return c.back(); // 最後の要素
152 }
153 return c[i];
154}
155
179template <typename T, std::size_t N>
180constexpr const T& random_at(const T (&array)[N]) {
181 uint32_t random_value = genrand_int32();
182 std::size_t index = random_value % N;
183 return array[index];
184}
185
209template <typename T>
210constexpr const typename T::value_type& random_at(const T& container, const typename T::value_type& default_value = typename T::value_type{}) {
211 if (container.empty()) {
212 return default_value;
213 }
214 uint32_t random_value = genrand_int32();
215 std::size_t index = random_value % container.size();
216 return container[index];
217}
218
219#define memsetz(addr_,bytesize_) memset(addr_,0x00,bytesize_)
220
235extern std::string fs(const char* format, ...);
236
248extern void* mallocz( size_t bytesize_ );
249
257extern void print_mallinfo();
Header file for the Mersenne Twister random number generator.
uint32_t genrand_int32(void)
Generates a random number on [0, 0xffffffff] interval.
Definition mt.c:110
void * mallocz(size_t bytesize_)
Allocate and zero-initialize a block of memory.
Definition sublibc.cpp:25
constexpr auto clamp_to_edge(const T &c, std::size_t i) -> const typename T::value_type &
Returns a clamped reference to an element at index i in container c.
Definition sublibc.h:149
std::size_t numof(const TYPE(&)[SIZE])
Retrieve the size of an array.
Definition sublibc.h:77
constexpr auto wrap_at(const T &c, std::size_t i) -> const typename T::value_type &
Returns a wrapped reference to an element at index i in container c.
Definition sublibc.h:111
void print_mallinfo()
Print memory allocation information.
Definition sublibc.cpp:10
std::string fs(const char *format,...)
Format a string with variable arguments.
Definition sublibc.cpp:32