mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
separate rb_random_t
* random.c: separate abstract rb_random_t and rb_random_mt_t for Mersenne Twister implementation. * include/ruby/random.h: the interface for extensions of Random class. * DLL imported symbol reference is not constant on Windows. * check if properly initialized.
This commit is contained in:
parent
f4d5273989
commit
af5e87ab21
Notes:
git
2020-09-07 20:08:35 +09:00
8 changed files with 774 additions and 121 deletions
80
include/ruby/random.h
Normal file
80
include/ruby/random.h
Normal file
|
@ -0,0 +1,80 @@
|
|||
#ifndef RUBY_RANDOM_H
|
||||
#define RUBY_RANDOM_H 1
|
||||
/**
|
||||
* @file
|
||||
* @date Sat May 7 11:51:14 JST 2016
|
||||
* @copyright 2007-2020 Yukihiro Matsumoto
|
||||
* @copyright This file is a part of the programming language Ruby.
|
||||
* Permission is hereby granted, to either redistribute and/or
|
||||
* modify this file, provided that the conditions mentioned in the
|
||||
* file COPYING are met. Consult the file for details.
|
||||
*/
|
||||
|
||||
#include "ruby/ruby.h"
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#if 0
|
||||
} /* satisfy cc-mode */
|
||||
#endif
|
||||
#endif
|
||||
|
||||
RUBY_SYMBOL_EXPORT_BEGIN
|
||||
|
||||
typedef struct {
|
||||
VALUE seed;
|
||||
} rb_random_t;
|
||||
|
||||
typedef void rb_random_init_func(rb_random_t *, const uint32_t *, size_t);
|
||||
typedef unsigned int rb_random_get_int32_func(rb_random_t *);
|
||||
typedef void rb_random_get_bytes_func(rb_random_t *, void *, size_t);
|
||||
|
||||
typedef struct {
|
||||
size_t default_seed_bits;
|
||||
rb_random_init_func *init;
|
||||
rb_random_get_int32_func *get_int32;
|
||||
rb_random_get_bytes_func *get_bytes;
|
||||
} rb_random_interface_t;
|
||||
|
||||
#define rb_rand_if(obj) \
|
||||
((const rb_random_interface_t *)RTYPEDDATA_TYPE(obj)->data)
|
||||
|
||||
#define RB_RANDOM_INTERFACE_DECLARE(prefix) \
|
||||
static void prefix##_init(rb_random_t *, const uint32_t *, size_t); \
|
||||
static unsigned int prefix##_get_int32(rb_random_t *); \
|
||||
static void prefix##_get_bytes(rb_random_t *, void *, size_t); \
|
||||
/* end */
|
||||
|
||||
#define RB_RANDOM_INTERFACE_DEFINE(prefix) \
|
||||
prefix##_init, \
|
||||
prefix##_get_int32, \
|
||||
prefix##_get_bytes, \
|
||||
/* end */
|
||||
|
||||
#if defined _WIN32 && !defined __CYGWIN__
|
||||
typedef rb_data_type_t rb_random_data_type_t;
|
||||
# define RB_RANDOM_PARENT 0
|
||||
# define RB_RANDOM_DATA_INIT_PARENT(random_data) \
|
||||
(random_data.parent = &rb_random_data_type)
|
||||
#else
|
||||
typedef const rb_data_type_t rb_random_data_type_t;
|
||||
# define RB_RANDOM_PARENT &rb_random_data_type
|
||||
# define RB_RANDOM_DATA_INIT_PARENT(random_data) ((void)0)
|
||||
#endif
|
||||
|
||||
void rb_random_mark(void *ptr);
|
||||
double rb_int_pair_to_real_exclusive(uint32_t a, uint32_t b);
|
||||
double rb_int_pair_to_real_inclusive(uint32_t a, uint32_t b);
|
||||
void rb_rand_bytes_int32(rb_random_get_int32_func *, rb_random_t *, void *, size_t);
|
||||
RUBY_EXTERN const rb_data_type_t rb_random_data_type;
|
||||
|
||||
RUBY_SYMBOL_EXPORT_END
|
||||
|
||||
#if defined(__cplusplus)
|
||||
#if 0
|
||||
{ /* satisfy cc-mode */
|
||||
#endif
|
||||
} /* extern "C" { */
|
||||
#endif
|
||||
|
||||
#endif /* RUBY_RANDOM_H */
|
Loading…
Add table
Add a link
Reference in a new issue