Develop Biology
The language of life
Arrangement.h
Go to the documentation of this file.
1/*
2 * This file is a part of the Biology project by eons LLC.
3 * Biology (aka Develop Biology) is a framework for approaching software
4 * development from a natural sciences perspective.
5 *
6 * Copyright (C) 2022 Séon O'Shannon & eons LLC
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
26
27namespace bio {
28namespace physical {
29
34template < typename TYPE >
36 public Container
37{
38public:
39
43 Arrangement(const Index expectedSize = 2)
44 :
46 expectedSize,
47 sizeof(TYPE))
48 {
49
50 }
51
55 virtual ~Arrangement()
56 {
57
58 }
59
60 Index Add(const ByteStream content)
61 {
62 BIO_SANITIZE(content.Is< TYPE >(), ,
63 return InvalidIndex())
64 Index ret = this->GetNextAvailableIndex();
65 BIO_SANITIZE(ret, ,
66 return InvalidIndex())
67 TYPE toAdd = content;
68 std::memcpy(
69 &this->m_store[ret * sizeof(TYPE)],
70 &toAdd,
71 sizeof(TYPE));
72 return ret;
73 }
74
75 ByteStream Access(const Index index)
76 {
77 BIO_SANITIZE(this->IsAllocated(index), ,
78 return NULL)
79 TYPE* ret;
80 std::memcpy(
81 ret,
82 &this->m_store[index * sizeof(TYPE)],
83 sizeof(TYPE));
84 return *ret;
85 }
86
87 const ByteStream Access(const Index index) const
88 {
89 BIO_SANITIZE(this->IsAllocated(index), ,
90 return NULL)
91 TYPE* ret;
92 std::memcpy(
93 ret,
94 &this->m_store[index * sizeof(TYPE)],
95 sizeof(TYPE));
96 return *ret;
97 }
98
99 bool Erase(const Index index)
100 {
101 BIO_SANITIZE(this->IsAllocated(index), ,
102 return false)
103 TYPE* toDelete;
104 std::memcpy(
105 toDelete,
106 &this->m_store[index * sizeof(TYPE)],
107 sizeof(TYPE));
108 delete toDelete;
109 this->m_deallocated.push_back(index);
110 return true;
111 }
112
113 virtual bool AreEqual(
114 Index internal,
115 const ByteStream external
116 ) const
117 {
118 BIO_SANITIZE(external.Is< TYPE >(), ,
119 return false)
120 return this->Access(internal).template As< TYPE >() == external.template As< TYPE >();
121 }
122
128 virtual TYPE OptimizedAccess(Index index)
129 {
130 return this->Access(index);
131 }
132
138 virtual const TYPE OptimizedAccess(Index index) const
139 {
140 return this->Access(index).template As< TYPE >();
141 }
142
147 virtual const std::size_t GetStepSize() const
148 {
149 return sizeof(TYPE);
150 }
151};
152
153} //physical namespace
154} //bio namespace
#define BIO_SANITIZE(test, success, failure)
bool Is() const
Definition: ByteStream.h:183
unsigned char * m_store
Definition: Container.h:340
virtual Index GetNextAvailableIndex()
Definition: Container.cpp:329
virtual bool IsAllocated(const Index index) const
Definition: Container.cpp:107
std::deque< Index > m_deallocated
Definition: Container.h:344
virtual const TYPE OptimizedAccess(Index index) const
Definition: Arrangement.h:138
bool Erase(const Index index)
Definition: Arrangement.h:99
Index Add(const ByteStream content)
Definition: Arrangement.h:60
const ByteStream Access(const Index index) const
Definition: Arrangement.h:87
ByteStream Access(const Index index)
Definition: Arrangement.h:75
virtual TYPE OptimizedAccess(Index index)
Definition: Arrangement.h:128
virtual const std::size_t GetStepSize() const
Definition: Arrangement.h:147
Arrangement(const Index expectedSize=2)
Definition: Arrangement.h:43
virtual bool AreEqual(Index internal, const ByteStream external) const
Definition: Arrangement.h:113
Definition: Cell.h:31
uint32_t Index
Definition: Types.h:57
const Index InvalidIndex()
Definition: Types.cpp:26