-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataBlockList.cc
88 lines (74 loc) · 1.84 KB
/
DataBlockList.cc
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
/*
* File: DataBlockList.cpp
* Author: Tobias Fleig <tobifleig@gmail.com>
*
* Created on September 16, 2015, 11:44 PM
*/
#include "DataBlockList.h"
#include "Block.h"
#include <cstdint>
#include <vector>
#include <list>
#include "Constants.inc"
#include "StreamUtils.inc"
#include "bits/stl_list.h"
namespace SDI4FS {
DataBlockList::DataBlockList(STREAM &input) : Block(input), entries() {
// read entries
for (int i = 0; i < SDI4FS_MAX_DATABLOCKS_PER_DATABLOCKLIST; ++i) {
uint32_t id;
read32(input, &id);
if (id != 0) {
// save
entries.push_back(id);
} else {
// no gaps allowed
break;
}
}
}
DataBlockList::DataBlockList(uint32_t id) : Block(id), entries() {
// nothing to do here
}
DataBlockList::~DataBlockList() {
}
void DataBlockList::save(STREAM &output) {
Block::save(output);
// write entries
for (auto iter = entries.begin(); iter != entries.end(); ++iter) {
write32(output, *iter);
}
// null ids of rest
for (int i = entries.size(); i < SDI4FS_MAX_DATABLOCKS_PER_DATABLOCKLIST; ++i) {
// null id
write32(output, 0);
}
}
bool DataBlockList::pushDataBlock(uint32_t id) {
if (entries.size() == SDI4FS_MAX_DATABLOCKS_PER_DATABLOCKLIST) {
// full
return false;
}
entries.push_back(id);
return true;
}
uint32_t DataBlockList::popDataBlock() {
if (entries.empty()) {
return 0;
}
uint32_t result = entries[entries.size() - 1];
entries.pop_back();
return result;
}
uint32_t DataBlockList::getDataBlock(size_t index) {
if (entries.size() <= index) {
return 0;
}
return entries[index];
}
void DataBlockList::blocks(std::list<uint32_t> &result) {
for (uint32_t id : entries) {
result.push_back(id);
}
}
} // SDI4FS