Sese Framework
x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
RingQueue.h
浏览该文件的文档.
1
// Copyright 2024 libsese
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
20
21
#pragma once
22
23
#include <stdexcept>
24
25
namespace
sese
{
26
32
template
<
typename
T,
int
N>
33
class
RingQueue
{
34
public
:
35
// 获取队头元素,元素不足则抛出异常
36
T &
front
() {
37
if
(
empty
()) {
38
throw
std::out_of_range(
"queue is empty"
);
39
}
40
return
_data
[
_head
];
41
}
42
43
// 出队,元素不足则抛出异常
44
void
pop
() {
45
if
(
empty
()) {
46
throw
std::out_of_range(
"queue is empty"
);
47
}
48
_head
= (
_head
+ 1) % N;
49
_size
-= 1;
50
}
51
52
// 入队,队列已满则抛出异常
53
void
push
(
const
T &data) {
54
if
(
full
()) {
55
throw
std::out_of_range(
"queue is full"
);
56
}
57
_data
[
_tail
] = data;
58
_tail
= (
_tail
+ 1) % N;
59
_size
+= 1;
60
}
61
62
// 判断队列是否为空
63
[[nodiscard]]
bool
empty
()
const
{
64
return
_size
== 0;
65
}
66
67
// 判断队列是否已满
68
[[nodiscard]]
bool
full
()
const
{
69
return
_size
== N;
70
}
71
72
// 获取队列大小
73
[[nodiscard]]
size_t
size
()
const
{
74
return
_size
;
75
}
76
77
private
:
78
T
_data
[N];
79
size_t
_head
{};
80
size_t
_tail
{};
81
size_t
_size
{};
82
};
83
}
// namespace sese
sese
container
RingQueue.h
生成于 2024年 十一月 4日 星期一 09:57:59 , 为 Sese Framework使用
1.11.0