358 lines
7.1 KiB
C++
358 lines
7.1 KiB
C++
// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
|
//
|
|
// This software owned by Onix Solutions Limited [OnixS] and is
|
|
// protected by copyright law and international copyright treaties.
|
|
//
|
|
// Access to and use of the software is governed by the terms of the applicable
|
|
// OnixS Software Services Agreement (the Agreement) and Customer end user license
|
|
// agreements granting a non-assignable, non-transferable and non-exclusive license
|
|
// to use the software for it's own data processing purposes under the terms defined
|
|
// in the Agreement.
|
|
//
|
|
// Except as otherwise granted within the terms of the Agreement, copying or
|
|
// reproduction of any part of this source code or associated reference material
|
|
// to any other location for further reproduction or redistribution, and any
|
|
// amendments to this copyright notice, are expressly prohibited.
|
|
//
|
|
// Any reproduction or redistribution for sale or hiring of the Software not in
|
|
// accordance with the terms of the Agreement is a violation of copyright law.
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
|
#include <stdexcept>
|
|
|
|
#include <OnixS/CME/MDH/Bootstrap.h>
|
|
|
|
ONIXS_CMEMDH_NAMESPACE_BEGIN
|
|
|
|
/// Implements vector-like container over
|
|
/// built-in array of fixed size being
|
|
/// referenced.
|
|
template
|
|
<
|
|
typename ItemType,
|
|
typename SizeType
|
|
>
|
|
class VectorOverArray
|
|
{
|
|
public:
|
|
/// Aliases container item type.
|
|
typedef ItemType Item;
|
|
|
|
/// Aliases dimension type.
|
|
typedef SizeType Size;
|
|
|
|
/// Aliases constant iterator type.
|
|
typedef const Item* ConstIterator;
|
|
|
|
/// Aliases mutable iterator type.
|
|
typedef Item* Iterator;
|
|
|
|
/// Initializes instance with no items.
|
|
VectorOverArray()
|
|
: items_(NULL)
|
|
, size_(0)
|
|
, capacity_(0)
|
|
{
|
|
}
|
|
|
|
/// Initializes instance over given fixed-size array.
|
|
VectorOverArray(
|
|
Item* items,
|
|
Size capacity)
|
|
: items_(items)
|
|
, size_(0)
|
|
, capacity_(capacity)
|
|
{
|
|
}
|
|
|
|
/// Cleans everything up.
|
|
~VectorOverArray()
|
|
{
|
|
resize(0);
|
|
}
|
|
|
|
/// Indicates whether instance has no items.
|
|
bool
|
|
empty() const
|
|
{
|
|
return (0 == size_);
|
|
}
|
|
|
|
/// Number of items.
|
|
Size
|
|
size() const
|
|
{
|
|
return size_;
|
|
}
|
|
|
|
/// Total capacity of container.
|
|
Size
|
|
capacity() const
|
|
{
|
|
return capacity_;
|
|
}
|
|
|
|
/// Iterator referencing to the first item.
|
|
Iterator
|
|
begin()
|
|
{
|
|
return items_;
|
|
}
|
|
|
|
/// Iterator referencing to the first item.
|
|
ConstIterator
|
|
begin() const
|
|
{
|
|
return items_;
|
|
}
|
|
|
|
/// Iterator referencing to the item behind the last item.
|
|
Iterator
|
|
end()
|
|
{
|
|
return (items_ + size_);
|
|
}
|
|
|
|
/// Iterator referencing to the item behind the last item.
|
|
ConstIterator
|
|
end() const
|
|
{
|
|
return (items_ + size_);
|
|
}
|
|
|
|
/// Provides access to the front item if available.
|
|
Item&
|
|
front()
|
|
{
|
|
assert(0 < size_);
|
|
|
|
return items_[0];
|
|
}
|
|
|
|
/// Provides access to the front item if available.
|
|
const Item&
|
|
front() const
|
|
{
|
|
assert(0 < size_);
|
|
|
|
return items_[0];
|
|
}
|
|
|
|
/// Accesses to item by index.
|
|
Item&
|
|
operator[](
|
|
Size index)
|
|
{
|
|
assert(index < size_);
|
|
|
|
return items_[index];
|
|
}
|
|
|
|
/// Accesses to item by index.
|
|
const Item&
|
|
operator[](
|
|
Size index) const
|
|
{
|
|
assert(index < size_);
|
|
|
|
return items_[index];
|
|
}
|
|
|
|
/// Accesses to item by index
|
|
/// checking access bounds.
|
|
Item&
|
|
at(
|
|
Size index)
|
|
{
|
|
if (index < size_)
|
|
return items_[index];
|
|
|
|
throw std::out_of_range
|
|
(
|
|
"Index outside of range."
|
|
);
|
|
}
|
|
|
|
/// Accesses to item by index
|
|
/// checking access bounds.
|
|
const Item&
|
|
at(
|
|
Size index) const
|
|
{
|
|
if (index < size_)
|
|
return items_[index];
|
|
|
|
throw std::out_of_range
|
|
(
|
|
"Index outside of range."
|
|
);
|
|
}
|
|
|
|
/// Inserts new item to given position.
|
|
/// Items under higher indices are moved back.
|
|
/// Last item is extruded thus keeping size of
|
|
/// or container constant.
|
|
///
|
|
/// @return Reference to an item at
|
|
/// requested position for further update.
|
|
Item&
|
|
insertExtrude(Size index)
|
|
{
|
|
assert(index < size_);
|
|
|
|
Item* const toBeUpdated = items_ + index;
|
|
|
|
Item* target = items_ + size_ - 1;
|
|
Item* source = target - 1;
|
|
|
|
while (target != toBeUpdated)
|
|
{
|
|
target->~Item();
|
|
|
|
new (target--) Item(*source--);
|
|
}
|
|
|
|
return *toBeUpdated;
|
|
}
|
|
|
|
/// Erases item at given position.
|
|
void
|
|
erase(
|
|
Size index)
|
|
{
|
|
assert(index < size_);
|
|
|
|
Item* const theEnd = end();
|
|
Item* const toBeErased = items_ + index;
|
|
|
|
Item* target = toBeErased;
|
|
Item* source = toBeErased + 1;
|
|
|
|
while (source != theEnd)
|
|
{
|
|
target->~Item();
|
|
|
|
new (target++) Item(*source++);
|
|
}
|
|
|
|
target->~Item();
|
|
|
|
--size_;
|
|
}
|
|
|
|
/// Resizes container.
|
|
///
|
|
/// If vector is enlarged, new items
|
|
/// constructed as copies of given instance.
|
|
void
|
|
resize(
|
|
Size newSize,
|
|
const Item& item = Item())
|
|
{
|
|
assert(newSize <= capacity_);
|
|
|
|
if (newSize > size_)
|
|
{
|
|
while (size_ != newSize)
|
|
new (items_ + size_++) Item(item);
|
|
}
|
|
else
|
|
{
|
|
while (size_ != newSize)
|
|
(items_ + --size_)->~Item();
|
|
}
|
|
}
|
|
|
|
private:
|
|
Item* items_;
|
|
Size size_;
|
|
|
|
const Size capacity_;
|
|
|
|
VectorOverArray(
|
|
const VectorOverArray&);
|
|
|
|
VectorOverArray&
|
|
operator =(
|
|
const VectorOverArray&);
|
|
};
|
|
|
|
// Throws exception on copy failure.
|
|
template
|
|
<
|
|
typename TargetSize,
|
|
typename SourceSize
|
|
>
|
|
void
|
|
throwVectorCopyOverflow(
|
|
TargetSize targetSize,
|
|
SourceSize sourceSize)
|
|
{
|
|
std::string error;
|
|
|
|
error +=
|
|
"Cannot copy items because number "
|
|
"of source items exceeds target capacity "
|
|
"[sourceItemCount=";
|
|
|
|
toStr(error, sourceSize);
|
|
|
|
error += ",targetCapacity=";
|
|
|
|
toStr(error, targetSize);
|
|
|
|
error += "]. ";
|
|
|
|
throw std::runtime_error(error);
|
|
}
|
|
|
|
/// Copies items from source to target.
|
|
/// Items are not required to be of same type
|
|
/// thus compatible types can be copied.
|
|
template
|
|
<
|
|
typename TargetItem,
|
|
typename TargetSize,
|
|
typename SourceItem,
|
|
typename SourceSize
|
|
>
|
|
void
|
|
copy(
|
|
VectorOverArray
|
|
<TargetItem, TargetSize>&
|
|
target,
|
|
const
|
|
VectorOverArray
|
|
<SourceItem, TargetSize>&
|
|
source,
|
|
SourceSize itemCount =
|
|
static_cast<SourceSize>(-1))
|
|
{
|
|
if (itemCount > source.size())
|
|
itemCount = source.size();
|
|
|
|
if (itemCount > target.capacity())
|
|
{
|
|
throwVectorCopyOverflow
|
|
(
|
|
target.capacity(),
|
|
itemCount
|
|
);
|
|
}
|
|
|
|
target.resize(itemCount);
|
|
|
|
while (itemCount--)
|
|
{
|
|
target[itemCount] =
|
|
source[itemCount];
|
|
}
|
|
}
|
|
|
|
ONIXS_CMEMDH_NAMESPACE_END
|