-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
fixed_list.hpp
98 lines (81 loc) · 2.72 KB
/
fixed_list.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#pragma once
//=====================================================================//
/*! @file
@brief 固定サイズ・リスト・クラス @n
※作業中
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2017, 2020 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
namespace utils {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief 固定サイズ・リスト・クラス
@param[in] ITEM 格納型
@param[in] SIZE リスト・サイズ
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class ITEM, uint32_t SIZE>
struct fixed_list {
typedef ITEM value_type;
private:
struct list_t {
list_t* back_;
ITEM item_;
list_t* next_;
list_t() : back_(nullptr), item_(), next_(nullptr) { }
};
list_t list_[SIZE];
uint32_t count_;
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクタ
*/
//-----------------------------------------------------------------//
fixed_list() noexcept : list_{ }, count_(0) { }
//-----------------------------------------------------------------//
/*!
@brief 格納可能な最大サイズを返す
@return 格納可能な最大サイズ
*/
//-----------------------------------------------------------------//
uint32_t capacity() const noexcept { return SIZE; }
//-----------------------------------------------------------------//
/*!
@brief 現在のサイズを返す
@return 現在のサイズ
*/
//-----------------------------------------------------------------//
uint32_t size() const noexcept { return count_; }
//-----------------------------------------------------------------//
/*!
@brief リストをクリア(リセット)
*/
//-----------------------------------------------------------------//
void clear() noexcept { count_ = 0; }
//-----------------------------------------------------------------//
/*!
@brief リストを追加
@param[in] item アイテム
@return 追加出来ない場合「false」
*/
//-----------------------------------------------------------------//
bool insert(const ITEM& item)
{
if(count_ >= capacity()) return false;
list_t* back = nullptr;
if(count_ > 0) {
back = &list_[count_ - 1];
list_[count_ - 1].next_ = &list_[count_];
}
list_[count_].back_ = back;
list_[count_].item_ = item;
list_[count_].next_ = nullptr;
++count_;
return true;
}
};
}