Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
e783c2bd
Unverified
Commit
e783c2bd
authored
Nov 14, 2021
by
Shen-Ta Hsieh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
util/LazyRandomEngine: use std::optional to avoid allocation
Signed-off-by:
Shen-Ta Hsieh
<
ibmibmibm.tw@gmail.com
>
parent
a8c77a6f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
9 additions
and
17 deletions
+9
-17
LazyRandomEngine.cxx
src/util/LazyRandomEngine.cxx
+3
-5
LazyRandomEngine.hxx
src/util/LazyRandomEngine.hxx
+6
-12
No files found.
src/util/LazyRandomEngine.cxx
View file @
e783c2bd
...
...
@@ -19,12 +19,10 @@
#include "LazyRandomEngine.hxx"
void
LazyRandomEngine
::
AutoCreate
()
{
if
(
engine
!=
nullptr
)
void
LazyRandomEngine
::
AutoCreate
()
{
if
(
engine
)
return
;
std
::
random_device
rd
;
engine
=
new
std
::
mt19937
(
rd
());
engine
.
emplace
(
rd
());
}
src/util/LazyRandomEngine.hxx
View file @
e783c2bd
...
...
@@ -21,21 +21,19 @@
#define MPD_LAZY_RANDOM_ENGINE_HXX
#include <cassert>
#include <optional>
#include <random>
/**
* A random engine that will be created and seeded on demand.
*/
class
LazyRandomEngine
{
std
::
mt19937
*
engine
;
std
::
optional
<
std
::
mt19937
>
engine
;
public
:
typedef
std
::
mt19937
::
result_type
result_type
;
LazyRandomEngine
()
:
engine
(
nullptr
)
{}
~
LazyRandomEngine
()
{
delete
engine
;
}
LazyRandomEngine
()
:
engine
(
std
::
nullopt
)
{}
LazyRandomEngine
(
const
LazyRandomEngine
&
other
)
=
delete
;
LazyRandomEngine
&
operator
=
(
const
LazyRandomEngine
&
other
)
=
delete
;
...
...
@@ -46,16 +44,12 @@ public:
*/
void
AutoCreate
();
static
constexpr
result_type
min
()
{
return
std
::
mt19937
::
min
();
}
static
constexpr
result_type
min
()
{
return
std
::
mt19937
::
min
();
}
static
constexpr
result_type
max
()
{
return
std
::
mt19937
::
max
();
}
static
constexpr
result_type
max
()
{
return
std
::
mt19937
::
max
();
}
result_type
operator
()()
{
assert
(
engine
!=
nullptr
);
assert
(
engine
);
return
engine
->
operator
()();
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment