Develop Biology
The language of life
|
#include <Container.h>
Public Member Functions | |
Container (const Container &other) | |
Container (const Index expectedSize=2, std::size_t stepSize=sizeof(ByteStream)) | |
virtual | ~Container () |
virtual ByteStream | Access (const Index index) |
virtual const ByteStream | Access (const Index index) const |
ByteStream | Access (const SmartIterator itt) |
virtual const ByteStream | Access (const SmartIterator itt) const |
virtual Index | Add (const ByteStream content) |
template<typename T > | |
std::vector< T > | AsVector () const |
virtual SmartIterator | Begin () const |
virtual void | Clear () |
virtual Iterator * | ConstructClassIterator (const Index index=InvalidIndex()) const |
virtual SmartIterator | End () const |
bool | Erase (const SmartIterator itt) |
virtual bool | Erase (Index index) |
virtual void | Expand () |
virtual Index | GetAllocatedSize () const |
virtual Index | GetBeginIndex () const |
virtual Index | GetCapacity () const |
virtual Index | GetEndIndex () const |
virtual Index | GetNumberOfElements () const |
bool | Has (const ByteStream content) const |
virtual void | Import (const Container *other) |
virtual Index | Insert (const ByteStream content, const Index index) |
virtual bool | IsAllocated (const Index index) const |
virtual bool | IsFree (const Index index) const |
virtual bool | IsInRange (const Index index) const |
virtual ByteStream | operator[] (const Index index) |
virtual const ByteStream | operator[] (const Index index) const |
virtual ByteStream | operator[] (const SmartIterator itt) |
virtual const ByteStream | operator[] (const SmartIterator itt) const |
Index | SeekTo (const ByteStream content) const |
Protected Member Functions | |
virtual bool | AreEqual (Index internal, const ByteStream external) const |
virtual Index | GetNextAvailableIndex () |
virtual const std::size_t | GetStepSize () const |
Protected Attributes | |
std::deque< Index > | m_deallocated |
Index | m_firstFree |
Index | m_size |
unsigned char * | m_store |
Iterator * | m_tempItt |
Containers are the singular Biology container construct. Essentially we need a generic, non-template std::vector<> base class, so we'll make our own. Different containers (e.g. the corollaries to std::set vs std::map vs std::vector) are all children of *this and we use typical inheritance to modify the behavior of each specialization. By using inheritance for our Containers, we can pass Container*s around and use a standard interface to manipulate the contents. In other words, we can treat std::sets like std::vectors or even treat std::maps like std::queues, all by overriding virtual methods and handling inputs consistently (obviously containers with key value pairs will need to pull a new piece of information from somewhere if given only a key or only a value).
NOTE: we still use stl containers where the additional features of *this class are not needed. Those usages may be changed to implement *this in a future release.
You can think of Containers as our own internal RAM. We allocated a block of bytes and manipulate them as necessary. By default, we use ByteStreams to store arbitrary data in our allocated memory. ByteStreams can be wasteful though (e.g. a whole extra std::string for every Index); so, overrides of this can use the same internal memory block but store other data structures for more efficient memory usage. We maintain ByteStreams as our data conversion type, as they are flexible but safer than a void.
When using Containers, we make no guarantees regarding the type of data stored. All we provide is a consistent means of accessing those data. To this end, we ensure that a Index's validity follows the lifecycle of the datum at that Index. This is identical to pointers: a Index represents the memory address of what is stored in *this. This means that as data are erased from *this, the memory is not moved, consolidated, or manipulated in any way that destroys the old references. This rule does have some exceptions and you are allowed to break it yourself. However, we try to stick by this as much as possible (e.g. see Insert(), below).
When using an Iterator, you will be given a SmartIterator which dynamically determines its implementation. Thus, we allow for full inheritance of *this base class.
There is another tradeoff here that we are leaning into: our interface does not make for easy use of std:: containers under-the-hood. By enforcing consistency on access, we've made the system less flexible. This may be changed in a major release down the road but its what we're sticking with for now.
When using Containers there are a few guidelines we recommend:
Definition at line 61 of file Container.h.
bio::Container::Container | ( | const Index | expectedSize = 2 , |
std::size_t | stepSize = sizeof(ByteStream) |
||
) |
NOTE: We cannot use GetStepSize() here as virtual functions are not available to ctors.
Definition at line 29 of file Container.cpp.
References BIO_ASSERT, m_size, and m_store.
bio::Container::Container | ( | const Container & | other | ) |
Copy ctor. Imports all contents from other into *this.
other |
Definition at line 42 of file Container.cpp.
References BIO_ASSERT, GetStepSize(), Import(), m_size, and m_store.
|
virtual |
Definition at line 53 of file Container.cpp.
|
virtual |
Get access to an element. NOTE: THIS DOES NOT CHECK IF THE ELEMENT IsFree!!! Free checks can be done independently. This is done for speed.
index |
Reimplemented in bio::physical::Arrangement< TYPE >, bio::physical::Arrangement< Linear >, bio::physical::Arrangement< Bond * >, and bio::physical::Line.
Definition at line 185 of file Container.cpp.
References BIO_SANITIZE, IsAllocated(), and m_store.
Referenced by Access(), bio::chemical::UnorderedMotif< CONTENT_TYPE >::AddImplementation(), AreEqual(), bio::Iterator::operator*(), operator[](), and bio::chemical::UnorderedMotif< CONTENT_TYPE >::RemoveImplementation().
|
virtual |
Get access to an element. NOTE: THIS DOES NOT CHECK IF THE ELEMENT IsFree!!! Free checks can be done independently. This is done for speed.
index |
Reimplemented in bio::physical::Arrangement< TYPE >, bio::physical::Arrangement< Linear >, bio::physical::Arrangement< Bond * >, and bio::physical::Line.
Definition at line 197 of file Container.cpp.
References BIO_SANITIZE, IsAllocated(), and m_store.
|
inline |
Access wrapper for SmartIterators.
itt |
Definition at line 183 of file Container.h.
References Access(), and bio::SmartIterator::GetIndex().
|
inlinevirtual |
Access wrapper for SmartIterators.
itt |
Definition at line 193 of file Container.h.
References Access(), and bio::SmartIterator::GetIndex().
|
virtual |
Adds content to *this.
content |
Reimplemented in bio::physical::Arrangement< TYPE >, bio::physical::Arrangement< Linear >, and bio::physical::Arrangement< Bond * >.
Definition at line 129 of file Container.cpp.
References BIO_SANITIZE, GetNextAvailableIndex(), and m_store.
Referenced by bio::chemical::UnorderedMotif< CONTENT_TYPE >::AddImplementation(), bio::chemical::LinearMotif< CONTENT_TYPE >::AddImplementation(), Import(), Insert(), and bio::chemical::LinearMotif< CONTENT_TYPE >::InsertImplementation().
|
protectedvirtual |
To make comparisons easier and reduce the work needed to optimize *this, children can define a comparison method which will be used for all searches.
internal | |
external |
Reimplemented in bio::physical::Arrangement< TYPE >, bio::physical::Arrangement< Linear >, bio::physical::Arrangement< Bond * >, and bio::physical::Line.
Definition at line 345 of file Container.cpp.
References Access().
Referenced by SeekTo().
|
inline |
Ease of use wrapper around casting *this to a std::vector. Since template methods are not virtual, you must make sure to properly implement Access() and make sure that *this contains the given type.
T |
Definition at line 297 of file Container.h.
References End(), and bio::SmartIterator::IsAtBeginning().
|
virtual |
NOTE: This does not need to be overridden if you've already defined ConstructClassIterator().
Definition at line 295 of file Container.cpp.
References GetBeginIndex().
Referenced by bio::organic::Habitat::AdaptInhabitants(), bio::cellular::Tissue::DifferentiateCells(), bio::chemical::UnorderedMotif< CONTENT_TYPE >::GetStringFromImplementation(), bio::organic::Organism::Morphogenesis(), bio::cellular::OrganSystem::Organogenesis(), bio::chemical::Axis::Rotate(), and bio::cellular::Organ::SpecializeTissues().
|
virtual |
Remove all elements from *this.
Definition at line 265 of file Container.cpp.
References ConstructClassIterator(), Erase(), GetEndIndex(), bio::Iterator::GetIndex(), bio::Iterator::IsAtBeginning(), m_deallocated, m_firstFree, m_tempItt, and bio::Iterator::MoveTo().
Referenced by ~Container(), bio::chemical::LinearMotif< CONTENT_TYPE >::~LinearMotif(), bio::chemical::LinearMotif< CONTENT_TYPE >::ClearImplementation(), and bio::chemical::UnorderedMotif< CONTENT_TYPE >::ClearImplementation().
|
virtual |
Override this to construct Iterators for your Containers.
index |
Definition at line 285 of file Container.cpp.
References BIO_SANITIZE, and IsAllocated().
Referenced by Clear(), SeekTo(), bio::physical::Line::SeekToId(), and bio::physical::Line::SeekToName().
|
virtual |
NOTE: This does not need to be overridden if you've already defined ConstructClassIterator().
Definition at line 302 of file Container.cpp.
References GetEndIndex().
Referenced by bio::molecular::Surface::~Surface(), AsVector(), bio::chemical::Atom::Attenuate(), bio::chemical::Atom::Disattenuate(), bio::chemical::Atom::GetBondPosition(), bio::chemical::UnorderedMotif< CONTENT_TYPE >::GetNumMatchingImplementation(), Import(), bio::chemical::UnorderedStructureInterface::ImportAll(), bio::chemical::LinearMotif< CONTENT_TYPE >::InsertImplementation(), bio::molecular::Surface::Release(), and bio::molecular::Surface::ReleaseAll().
|
inline |
Erase wrapper for SmartIterators.
itt |
Definition at line 223 of file Container.h.
References Erase(), and bio::SmartIterator::GetIndex().
|
virtual |
Removes content from *this.
index |
Reimplemented in bio::physical::Arrangement< TYPE >, bio::physical::Arrangement< Linear >, and bio::physical::Arrangement< Bond * >.
Definition at line 237 of file Container.cpp.
References BIO_SANITIZE, IsAllocated(), m_deallocated, and m_store.
Referenced by Clear(), Erase(), bio::chemical::LinearMotif< CONTENT_TYPE >::InsertImplementation(), and bio::chemical::UnorderedMotif< CONTENT_TYPE >::RemoveImplementation().
|
virtual |
Grow store to accommodate dynamic allocation.
datumSize | the memory size a Index should jump over. |
Definition at line 112 of file Container.cpp.
References BIO_SANITIZE, GetStepSize(), m_size, and m_store.
Referenced by GetNextAvailableIndex(), and Insert().
|
virtual |
GetCapacity - the number of free Indices at the end (ignores any deallocated Indices in the middle).
Definition at line 79 of file Container.cpp.
References m_firstFree.
Referenced by GetEndIndex(), GetNumberOfElements(), bio::Iterator::Increment(), Insert(), and bio::Iterator::IsAtEnd().
|
virtual |
Definition at line 64 of file Container.cpp.
Referenced by Begin(), bio::chemical::LinearMotif< CONTENT_TYPE >::GetStringFromImplementation(), and bio::chemical::LinearMotif< CONTENT_TYPE >::InsertImplementation().
|
virtual |
Definition at line 74 of file Container.cpp.
References m_size.
Referenced by Insert().
|
virtual |
Definition at line 69 of file Container.cpp.
References GetAllocatedSize().
Referenced by Clear(), End(), bio::chemical::LinearMotif< CONTENT_TYPE >::InsertImplementation(), SeekTo(), bio::physical::Line::SeekToId(), and bio::physical::Line::SeekToName().
|
protectedvirtual |
For ease of use when Add()ing. NOTE: This will mark the returned Index as filled, so please make sure it actually receives content.
Definition at line 329 of file Container.cpp.
References Expand(), bio::InvalidIndex(), m_deallocated, m_firstFree, and m_size.
Referenced by Add(), and bio::physical::Arrangement< TYPE >::Add().
|
virtual |
GetAllocatedSize - the number of deallocated Indices.
Definition at line 84 of file Container.cpp.
References GetAllocatedSize(), and m_deallocated.
Referenced by bio::chemical::UnorderedMotif< CONTENT_TYPE >::GetCountImplementation(), and bio::chemical::UnorderedMotif< CONTENT_TYPE >::HasAllImplementation().
|
protectedvirtual |
Please override this to return the size of the type your Container interface is working with.
Reimplemented in bio::physical::Arrangement< TYPE >, bio::physical::Arrangement< Linear >, and bio::physical::Arrangement< Bond * >.
Definition at line 353 of file Container.cpp.
Referenced by Container(), Expand(), and Insert().
bool bio::Container::Has | ( | const ByteStream | content | ) | const |
content |
Definition at line 232 of file Container.cpp.
References SeekTo().
Referenced by bio::chemical::LinearMotif< CONTENT_TYPE >::HasImplementation(), and bio::chemical::UnorderedMotif< CONTENT_TYPE >::HasImplementation().
|
virtual |
Copy the contents of other into *this.
other |
Definition at line 251 of file Container.cpp.
References Add(), BIO_SANITIZE, End(), and bio::Iterator::IsAtBeginning().
Referenced by Container(), bio::chemical::LinearMotif< CONTENT_TYPE >::LinearMotif(), bio::chemical::LinearMotif< CONTENT_TYPE >::ImportImplementation(), and bio::chemical::UnorderedMotif< CONTENT_TYPE >::ImportImplementation().
|
virtual |
Adds content to *this at the specified position. All content past the given position is shifted down. NOTE: This explicitly breaks our rule about Indices being preserved. However, this logic is necessary if the items being inserted need to be accessed in the specified order; for example: the items in *this are molecular::Proteins that have a set execution order.
To make implementing your own Containers easier, this method simply hacks the GetNextAvailableIndex method to return the desired index and then calls Add. Thus, by implementing Add, you also implement Insert.
content |
Definition at line 141 of file Container.cpp.
References Add(), BIO_SANITIZE, Expand(), GetAllocatedSize(), GetCapacity(), GetStepSize(), bio::InvalidIndex(), m_deallocated, m_firstFree, and m_store.
Referenced by bio::chemical::LinearMotif< CONTENT_TYPE >::InsertImplementation().
|
virtual |
index |
Definition at line 107 of file Container.cpp.
References IsFree(), and IsInRange().
Referenced by Access(), bio::physical::Arrangement< TYPE >::Access(), bio::chemical::Atom::BreakBondImplementation(), ConstructClassIterator(), bio::physical::Arrangement< TYPE >::Erase(), Erase(), bio::chemical::Atom::FormBondImplementation(), bio::chemical::Atom::GetBonded(), bio::chemical::Atom::GetBondType(), bio::chemical::LinearMotif< CONTENT_TYPE >::InsertImplementation(), and bio::Iterator::MoveTo().
|
virtual |
Checks if the given Index is available to be allocated, i.e. the Index should not be used. NOTE: Just because a Index is not free does not necessarily mean the Index has been allocated.
index |
Definition at line 94 of file Container.cpp.
References m_deallocated, and m_firstFree.
Referenced by bio::Iterator::Decrement(), bio::Iterator::Increment(), and IsAllocated().
|
virtual |
NOTE: Just because a Index IsInRange does not mean it is free or allocated.
index |
Definition at line 89 of file Container.cpp.
References m_size.
Referenced by IsAllocated().
|
virtual |
Ease of use wrapper around Access. See Access for details.
index |
Definition at line 309 of file Container.cpp.
References Access().
|
virtual |
Ease of use wrapper around Access. See Access for details.
index |
Definition at line 314 of file Container.cpp.
References Access().
|
virtual |
Ease of use wrapper around Access. See Access for details.
itt |
Definition at line 319 of file Container.cpp.
References Access().
|
virtual |
Ease of use wrapper around Access. See Access for details.
itt |
Definition at line 324 of file Container.cpp.
References Access().
Index bio::Container::SeekTo | ( | const ByteStream | content | ) | const |
Find the Index of content within *this.
content |
Definition at line 209 of file Container.cpp.
References AreEqual(), ConstructClassIterator(), GetEndIndex(), bio::Iterator::GetIndex(), bio::InvalidIndex(), bio::Iterator::IsAtBeginning(), m_tempItt, and bio::Iterator::MoveTo().
Referenced by Has(), and bio::chemical::UnorderedMotif< CONTENT_TYPE >::RemoveImplementation().
|
protected |
Definition at line 344 of file Container.h.
Referenced by Clear(), bio::physical::Arrangement< TYPE >::Erase(), Erase(), GetNextAvailableIndex(), GetNumberOfElements(), Insert(), and IsFree().
|
protected |
Definition at line 343 of file Container.h.
Referenced by Clear(), GetAllocatedSize(), GetNextAvailableIndex(), Insert(), and IsFree().
|
protected |
Definition at line 342 of file Container.h.
Referenced by Container(), Expand(), GetCapacity(), GetNextAvailableIndex(), and IsInRange().
|
protected |
cannot be void* as we need an object type for pointer arithmetic.
Definition at line 340 of file Container.h.
Referenced by Container(), ~Container(), Access(), bio::physical::Arrangement< TYPE >::Access(), Add(), bio::physical::Arrangement< TYPE >::Add(), bio::physical::Arrangement< TYPE >::Erase(), Erase(), Expand(), and Insert().
|
mutableprotected |
An iterator for use in loops. Matches lifecycle of object for better performance.
Definition at line 350 of file Container.h.
Referenced by ~Container(), Clear(), SeekTo(), bio::physical::Line::SeekToId(), and bio::physical::Line::SeekToName().