Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
wasm-watermarker
Project
Project
Details
Activity
Releases
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nagayama15
wasm-watermarker
Commits
ea7361dd
Commit
ea7361dd
authored
Mar 22, 2019
by
nagayama15
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🆕
Add circular bit stream
parent
772a6e38
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
0 deletions
+78
-0
circular_bit_stream.hpp
src/kyut/circular_bit_stream.hpp
+78
-0
No files found.
src/kyut/circular_bit_stream.hpp
0 → 100644
View file @
ea7361dd
#ifndef INCLUDE_kyut_circular_bit_stream_hpp
#define INCLUDE_kyut_circular_bit_stream_hpp
#include <cassert>
#include <cstdint>
#include <memory>
#include <string_view>
#include <vector>
namespace
kyut
{
class
CircularBitStream
{
public
:
explicit
CircularBitStream
(
const
std
::
uint8_t
*
data
,
std
::
size_t
size_bytes
)
noexcept
:
bytes_
(
data
,
data
+
size_bytes
)
,
pos_bits_
(
0
)
{
assert
(
data
!=
nullptr
||
size_bytes
==
0
);
}
CircularBitStream
(
const
CircularBitStream
&
)
=
delete
;
CircularBitStream
(
CircularBitStream
&&
)
=
delete
;
CircularBitStream
&
operator
=
(
const
CircularBitStream
&
)
=
delete
;
CircularBitStream
&
operator
=
(
CircularBitStream
&&
)
=
delete
;
~
CircularBitStream
()
noexcept
=
default
;
[[
nodiscard
]]
const
std
::
vector
<
std
::
uint8_t
>
&
bytes
()
const
noexcept
{
return
bytes_
;
}
[[
nodiscard
]]
std
::
size_t
size_bytes
()
const
noexcept
{
return
bytes_
.
size
();
}
[[
nodiscard
]]
std
::
size_t
size_bits
()
const
noexcept
{
return
size_bytes
()
*
8
;
}
bool
read_bit
()
noexcept
{
if
(
size_bits
()
==
0
)
{
return
false
;
}
const
auto
value
=
bytes_
[
pos_bits_
>>
3
]
>>
(
pos_bits_
&
0x7
);
if
(
++
pos_bits_
>=
size_bits
())
{
pos_bits_
=
0
;
}
return
value
;
}
std
::
uint64_t
read
(
std
::
size_t
size_bits
)
{
assert
(
size_bits
<=
64
);
std
::
uint64_t
value
=
0
;
for
(
size_t
i
=
0
;
i
<
size_bits
;
i
++
)
{
if
(
read_bit
())
{
value
|=
std
::
uint64_t
{
1
}
<<
i
;
}
}
return
value
;
}
static
std
::
unique_ptr
<
CircularBitStream
>
from_string
(
std
::
string_view
s
)
{
return
std
::
make_unique
<
CircularBitStream
>
(
reinterpret_cast
<
const
std
::
uint8_t
*>
(
s
.
data
()),
s
.
length
());
}
private
:
std
::
vector
<
std
::
uint8_t
>
bytes_
;
std
::
size_t
pos_bits_
;
};
}
// namespace kyut
#endif // INCLUDE_kyut_circular_bit_stream_hpp
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