Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Jan Koniarik
emlabcpp
Commits
5531d30c
Commit
5531d30c
authored
Apr 12, 2022
by
Jan Veverak Koniarik
Browse files
changed API of static_vector/circular buffer
parent
c726c556
Pipeline
#127908
passed with stage
in 4 minutes and 32 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
include/emlabcpp/allocator/pool.h
View file @
5531d30c
...
...
@@ -43,7 +43,7 @@ public:
return
nullptr
;
#endif
}
std
::
size_t
i
=
free_
.
pop
_back
();
std
::
size_t
i
=
free_
.
take
_back
();
return
&
pools_
[
i
];
}
...
...
include/emlabcpp/static_circular_buffer.h
View file @
5531d30c
...
...
@@ -110,12 +110,17 @@ public:
return
ref_item
(
from_
);
}
T
pop
_front
()
[[
nodiscard
]]
T
take
_front
()
{
T
item
=
std
::
move
(
front
()
);
pop_front
();
return
item
;
}
void
pop_front
()
{
delete_item
(
from_
);
from_
=
next
(
from_
);
return
item
;
}
// methods for handling the back side of the circular buffer
...
...
@@ -140,11 +145,11 @@ public:
[[
nodiscard
]]
reference
back
()
{
return
ref_item
(
to_
-
1
);
return
ref_item
(
prev
(
to_
)
);
}
[[
nodiscard
]]
const_reference
back
()
const
{
return
ref_item
(
to_
-
1
);
return
ref_item
(
prev
(
to_
)
);
}
void
push_back
(
T
item
)
...
...
@@ -236,17 +241,18 @@ private:
void
emplace_item
(
size_type
i
,
Args
&&
...
args
)
{
std
::
construct_at
(
reinterpret_cast
<
T
*
>
(
&
data_
)
+
i
,
std
::
forward
<
Args
>
(
args
)...
);
reinterpret_cast
<
T
*
>
(
std
::
addressof
(
data_
)
)
+
i
,
std
::
forward
<
Args
>
(
args
)...
);
}
// Reference to the item in data_storage.
[[
nodiscard
]]
reference
ref_item
(
size_type
i
)
{
return
*
(
reinterpret_cast
<
T
*
>
(
&
data_
)
+
i
);
return
*
(
reinterpret_cast
<
T
*
>
(
std
::
addressof
(
data_
)
)
+
i
);
}
[[
nodiscard
]]
const_reference
ref_item
(
size_type
i
)
const
{
return
*
(
reinterpret_cast
<
const
T
*
>
(
&
data_
)
+
i
);
return
*
(
reinterpret_cast
<
const
T
*
>
(
std
::
addressof
(
data_
)
)
+
i
);
}
// Cleans entire buffer from items.
...
...
@@ -261,14 +267,14 @@ private:
{
to_
=
other
.
size
();
std
::
uninitialized_copy
(
other
.
begin
(),
other
.
end
(),
reinterpret_cast
<
T
*
>
(
&
data_
)
);
other
.
begin
(),
other
.
end
(),
reinterpret_cast
<
T
*
>
(
std
::
addressof
(
data_
)
)
);
}
void
move_from
(
static_circular_buffer
&
other
)
{
to_
=
other
.
size
();
std
::
uninitialized_move
(
other
.
begin
(),
other
.
end
(),
reinterpret_cast
<
T
*
>
(
&
data_
)
);
other
.
begin
(),
other
.
end
(),
reinterpret_cast
<
T
*
>
(
std
::
addressof
(
data_
)
)
);
}
// Use this only when moving the indexes in the circular buffer - bullet-proof.
...
...
include/emlabcpp/static_vector.h
View file @
5531d30c
...
...
@@ -127,12 +127,17 @@ public:
size_
+=
1
;
}
T
pop
_back
()
[[
nodiscard
]]
T
take
_back
()
{
T
item
=
std
::
move
(
back
()
);
pop_back
();
return
item
;
}
void
pop_back
()
{
delete_item
(
size_
-
1
);
size_
-=
1
;
return
item
;
}
[[
nodiscard
]]
reference
back
()
...
...
tests/static_circular_buffer_test.cpp
View file @
5531d30c
#include "emlabcpp/static_circular_buffer.h"
#include "emlabcpp/algorithm.h"
#include "operations_counter.h"
#include <gtest/gtest.h>
...
...
@@ -267,3 +268,31 @@ TEST( static_circular_buffer_test, back_inserter )
EXPECT_TRUE
(
are_equal
)
<<
"buff: "
<<
view
{
buff
}
<<
"
\n
"
<<
"expected: "
<<
view
{
expected
}
<<
"
\n
"
;
}
TEST
(
static_circular_buffer_test
,
back
)
{
trivial_buffer
tbuff
;
for
(
int
i
:
{
1
,
2
,
3
,
4
,
5
,
6
,
7
}
)
{
tbuff
.
push_back
(
i
);
}
EXPECT_TRUE
(
tbuff
.
full
()
);
tbuff
.
pop_front
();
tbuff
.
push_back
(
8
);
EXPECT_TRUE
(
tbuff
.
full
()
);
EXPECT_EQ
(
tbuff
.
back
(),
8
);
}
struct
operations_counter_circular_buffer
{
using
container_type
=
static_circular_buffer
<
operations_counter
,
32
>
;
static
constexpr
std
::
size_t
n
=
16
;
};
using
types
=
::
testing
::
Types
<
operations_counter_circular_buffer
>
;
INSTANTIATE_TYPED_TEST_SUITE_P
(
operations_counter_circular_buffer_fixture
,
operations_counter_fixture
,
types
);
Write
Preview
Supports
Markdown
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