Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
llvm-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
llvm-watermarker
Commits
ffdff2ba
Commit
ffdff2ba
authored
Dec 09, 2018
by
nagayama15
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add circular bit stream.
parent
8fd05c4a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
126 additions
and
0 deletions
+126
-0
CircularBitStream.hpp
src/nykk/CircularBitStream.hpp
+97
-0
Test.cpp
test/Test.cpp
+2
-0
TestCircularBitStream.hpp
test/TestCircularBitStream.hpp
+27
-0
No files found.
src/nykk/CircularBitStream.hpp
0 → 100644
View file @
ffdff2ba
#ifndef INCLUDE_NYKK_CIRCULARBITSTREAM_HPP
#define INCLUDE_NYKK_CIRCULARBITSTREAM_HPP
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <string_view>
#include <vector>
namespace
nykk
{
class
CircularBitStream
{
public
:
static
CircularBitStream
from_string
(
std
::
string_view
s
)
{
return
CircularBitStream
{
reinterpret_cast
<
const
std
::
byte
*>
(
s
.
data
()),
s
.
size
()
};
}
explicit
CircularBitStream
(
const
std
::
byte
*
pointer
,
std
::
size_t
length
)
:
data_
(
pointer
,
pointer
+
length
)
,
bit_pos_
(
0
)
{
assert
(
pointer
!=
nullptr
||
length
==
0
);
}
// Uncopyable, movable.
CircularBitStream
(
const
CircularBitStream
&
)
=
delete
;
CircularBitStream
(
CircularBitStream
&&
)
=
default
;
CircularBitStream
&
operator
=
(
const
CircularBitStream
&
)
=
delete
;
CircularBitStream
&
operator
=
(
CircularBitStream
&&
)
=
default
;
~
CircularBitStream
()
=
default
;
[[
nodiscard
]]
const
std
::
byte
*
data
()
const
noexcept
{
return
data_
.
data
();
}
[[
nodiscard
]]
std
::
size_t
size_bytes
()
const
noexcept
{
return
data_
.
size
();
}
[[
nodiscard
]]
std
::
size_t
size_bits
()
const
noexcept
{
return
size_bytes
()
*
8
;
}
[[
nodiscard
]]
std
::
size_t
pos_bit
()
const
noexcept
{
return
bit_pos_
;
}
std
::
uint64_t
read
(
std
::
size_t
size_bits
)
{
assert
(
size_bits
<=
64
);
std
::
uint64_t
data
=
0
;
for
(
std
::
size_t
i
=
0
;
i
<
size_bits
;
i
++
)
{
data
|=
read_bit
()
?
1
<<
i
:
0
;
}
return
data
;
}
bool
read_bit
()
{
if
(
size_bits
()
==
0
)
{
return
false
;
}
const
std
::
byte
value
=
data_
[
bit_pos_
>>
3
]
>>
(
bit_pos_
&
0x7
);
if
(
++
bit_pos_
>=
size_bits
())
{
bit_pos_
=
0
;
}
return
std
::
to_integer
<
bool
>
(
value
&
std
::
byte
{
1
});
}
private
:
std
::
vector
<
std
::
byte
>
data_
;
std
::
size_t
bit_pos_
;
};
}
#endif
test/Test.cpp
View file @
ffdff2ba
#include "TestCircularBitStream.hpp"
#include "TestPermutationTable.hpp"
int
main
()
{
run_test_circular_bit_stream
();
run_test_permutation_table
();
}
test/TestCircularBitStream.hpp
0 → 100644
View file @
ffdff2ba
#ifndef INCLUDE_TEST_TESTCIRCULARBITSTREAM_HPP
#define INCLUDE_TEST_TESTCIRCULARBITSTREAM_HPP
#include "../src/nykk/CircularBitStream.hpp"
#include <iostream>
inline
void
run_test_circular_bit_stream
()
{
auto
s
=
nykk
::
CircularBitStream
::
from_string
(
u8"
\xab\xcd\xef\x98
"
);
assert
(
s
.
size_bytes
()
==
4
);
assert
(
s
.
size_bits
()
==
32
);
assert
(
s
.
read_bit
()
==
true
);
assert
(
s
.
read_bit
()
==
true
);
assert
(
s
.
read_bit
()
==
false
);
assert
(
s
.
pos_bit
()
==
3
);
assert
(
s
.
read
(
17
)
==
0x1f9b5
);
assert
(
s
.
pos_bit
()
==
20
);
assert
(
s
.
read
(
16
)
==
0xb98e
);
assert
(
s
.
pos_bit
()
==
4
);
}
#endif
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