HRCore V1.1.0
A High Resolution Calculation Library
Loading...
Searching...
No Matches
VirtualMMU.hpp
Go to the documentation of this file.
1
9#ifndef __HRCORE_VIRTUALMMU_HPP__
10#define __HRCORE_VIRTUALMMU_HPP__
11
12#include "../HRCore.h"
13
14namespace HRCore {
15
16#if HRCORE_ENABLE_STORAGE_VMMU
17namespace Storage {
18
23 template <size_t N>
24 class VirtualMMU : public Interface {
25 private:
26 struct data_t : public item_t {
27 friend class VirtualMMU<N>;
28 };
29
30 struct node_t {
31 ival_t data[N] = { 0 };
32 ival_t protect;
33 };
34
35 node_t* _pool[HRCORE_STORAGE_VMMU_MAX_LEN] = { nullptr };
36 size_t _count = 1; // "Actul count of items" from outside, initially it's 1.
37 size_t _len = 1;
38
39 public:
41 {
42 this->_pool[0] = new node_t;
43 }
44
45 virtual ~VirtualMMU()
46 {
47 while (this->_len)
48 delete this->_pool[--this->_len];
49 this->_count = 0;
50 }
51
52 virtual item_t at(size_t n)
53 {
54 data_t ret;
55 if (n >= this->_count)
56 return ret;
57 size_t p = n / N;
58 size_t f = n % N;
59 auto x = this->_pool[p];
60 if (!x)
61 return ret;
62 ret.data = x->data + f;
63 ret.index = n;
64 return ret;
65 }
66
67 virtual item_t begin() override { return this->at(0); }
68 virtual item_t end() override { return this->at(this->_count - 1); }
69 virtual size_t length() override { return this->_count; }
70
71 virtual item_t next(item_t cur) override
72 {
73 data_t* helper = (data_t*)&cur;
74 data_t ret;
75 if (!cur || helper->index >= this->_count - 1 || helper->index + 1 < helper->index) // BUG3! When (!cur) should not get next
76 return ret;
77 helper->index++;
78 size_t p = helper->index / N;
79 size_t f = helper->index % N;
80 auto x = this->_pool[p];
81 if (!x)
82 return ret;
83 ret.index = helper->index;
84 ret.data = x->data + f;
85 return ret;
86 }
87
88 virtual item_t prev(item_t cur) override
89 {
90 data_t* helper = (data_t*)&cur;
91 data_t ret;
92 if (!cur || helper->index >= this->_count + 1 || helper->index - 1 > helper->index)
93 return ret;
94 helper->index--;
95 size_t p = helper->index / N;
96 size_t f = helper->index % N;
97 auto x = this->_pool[p];
98 if (!x)
99 return ret;
100 ret.index = helper->index;
101 ret.data = x->data + f;
102 return ret;
103 }
104
105 virtual bool shrink(size_t c) override
106 {
107 if (c > HRCORE_STORAGE_VMMU_MAX_LEN * N || c >= this->_count)
108 return false;
109 size_t rlen = c / N + (c % N ? 1 : 0);
110 while (this->_len > rlen) {
111 delete this->_pool[--(this->_len)];
112 this->_pool[this->_len] = nullptr;
113 }
114 this->_count = c;
115 return true;
116 }
117
118 virtual bool extend(size_t c) override
119 {
120 if (c > HRCORE_STORAGE_VMMU_MAX_LEN * N || c <= this->_count)
121 return false;
122 size_t rlen = c / N + (c % N ? 1 : 0);
123 while (this->_len < rlen)
124 this->_pool[this->_len++] = new node_t;
125 this->_count = c;
126 return true;
127 }
128
129 virtual Interface* newObject() override { return new VirtualMMU<N>(); }
130 virtual void delObject(const Interface* ptr) override { delete static_cast<const VirtualMMU<N>*>(ptr); }
131 };
132
133} // namespace Storage
134
135#endif
136
137} // namespace HRCore
138
139#endif
A class as a storage interface, pure virtual.
Definition: StorageIF.hpp:28
An implement of Storage::Interface as Virtual MMU.
Definition: VirtualMMU.hpp:24
virtual item_t begin() override
Return the first item of storage.
Definition: VirtualMMU.hpp:67
virtual item_t at(size_t n)
Access to storage[pos].
Definition: VirtualMMU.hpp:52
virtual void delObject(const Interface *ptr) override
Delete a object from newObject()
Definition: VirtualMMU.hpp:130
virtual item_t end() override
Returns the last item of storage.
Definition: VirtualMMU.hpp:68
virtual size_t length() override
Get the length of storage.
Definition: VirtualMMU.hpp:69
virtual bool extend(size_t c) override
Extend the storage to a given length.
Definition: VirtualMMU.hpp:118
virtual item_t prev(item_t cur) override
Definition: VirtualMMU.hpp:88
virtual item_t next(item_t cur) override
Get next item.
Definition: VirtualMMU.hpp:71
virtual Interface * newObject() override
Get a new object of same type.
Definition: VirtualMMU.hpp:129
virtual bool shrink(size_t c) override
Remove data from the end to shrink the size to a given length.
Definition: VirtualMMU.hpp:105
#define HRCORE_STORAGE_VMMU_MAX_LEN
Define the max length of table of VMMU. Default: 1024.
Definition: HRCore.h:118
HRCore main namespace, contains all classes.
Definition: LinkedList.hpp:14
Storage item descriptor.
Definition: StorageIF.hpp:32
size_t index
Store the index of data.
Definition: StorageIF.hpp:45