mirror of
https://github.com/ETLCPP/etl.git
synced 2026-06-16 00:46:03 +08:00
Added list container
This commit is contained in:
parent
4e156d3590
commit
5f2795185e
113
list.h
Normal file
113
list.h
Normal file
@ -0,0 +1,113 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
|
||||
Copyright(c) 2014 jwellbelove
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files(the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __etl_list__
|
||||
#define __etl_list__
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "ilist.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
//*************************************************************************
|
||||
/// A templated list implementation that uses a fixed size buffer.
|
||||
/// MAX_SIZE_ elements will be always be constructed.
|
||||
/// The list will allocate one more element than 'MAX_SIZE'.
|
||||
///\note 'merge', 'swap' and 'splice' and are not supported.
|
||||
//*************************************************************************
|
||||
template <typename T, const size_t MAX_SIZE_>
|
||||
class list : public ilist<T>
|
||||
{
|
||||
public:
|
||||
|
||||
static const size_t MAX_SIZE = MAX_SIZE_;
|
||||
|
||||
public:
|
||||
|
||||
typedef T value_type;
|
||||
typedef T* pointer;
|
||||
typedef const T* const_pointer;
|
||||
typedef T& reference;
|
||||
typedef const T& const_reference;
|
||||
typedef size_t size_type;
|
||||
|
||||
//*************************************************************************
|
||||
/// Default constructor.
|
||||
//*************************************************************************
|
||||
inline list()
|
||||
: ilist<T>(&node_pool[0], MAX_SIZE)
|
||||
{
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from size and value.
|
||||
//*************************************************************************
|
||||
inline explicit list(size_t initialSize, T value = T())
|
||||
: ilist<T>(&node_pool[0], MAX_SIZE)
|
||||
{
|
||||
ilist<T>::assign(initialSize, value);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Copy constructor.
|
||||
//*************************************************************************
|
||||
explicit list(const list& other)
|
||||
: ilist<T>(&node_pool[0], MAX_SIZE)
|
||||
{
|
||||
ilist<T>::assign(other.cbegin(), other.cend());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Construct from range.
|
||||
//*************************************************************************
|
||||
template <typename TIterator>
|
||||
list(TIterator first, TIterator last)
|
||||
: ilist<T>(&node_pool[0], MAX_SIZE)
|
||||
{
|
||||
ilist<T>::assign(first, last);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Assignment operator.
|
||||
//*************************************************************************
|
||||
list& operator = (const list& rhs)
|
||||
{
|
||||
ilist<T>::assign(rhs.cbegin(), rhs.cend());
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// The pool of nodes used in the list.
|
||||
typename ilist<T>::Node node_pool[MAX_SIZE];
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
143
list_base.h
Normal file
143
list_base.h
Normal file
@ -0,0 +1,143 @@
|
||||
///\file
|
||||
|
||||
/******************************************************************************
|
||||
The MIT License(MIT)
|
||||
|
||||
Embedded Template Library.
|
||||
|
||||
Copyright(c) 2014 jwellbelove
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files(the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef __etl_in_ilist_h__
|
||||
#error This header is a private element of etl::list & etl::ilist
|
||||
#endif
|
||||
|
||||
#ifndef __etl_list_base__
|
||||
#define __etl_list_base__
|
||||
|
||||
#include <cstddef>
|
||||
#include "exception.h"
|
||||
|
||||
namespace etl
|
||||
{
|
||||
#ifdef ETL_USE_EXCEPTIONS
|
||||
//***************************************************************************
|
||||
/// Exception for the list.
|
||||
///\ingroup list
|
||||
//***************************************************************************
|
||||
class list_exception : public exception
|
||||
{
|
||||
public:
|
||||
|
||||
list_exception(const char* what)
|
||||
: exception(what)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Full exception for the list.
|
||||
///\ingroup list
|
||||
//***************************************************************************
|
||||
class list_full_exception : public list_exception
|
||||
{
|
||||
public:
|
||||
|
||||
list_full_exception()
|
||||
: list_exception("List full")
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
//***************************************************************************
|
||||
/// Iterator exception for the list.
|
||||
///\ingroup list
|
||||
//***************************************************************************
|
||||
class list_iterator_exception : public list_exception
|
||||
{
|
||||
public:
|
||||
|
||||
list_iterator_exception()
|
||||
: list_exception("Iterator problem")
|
||||
{
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
//***************************************************************************
|
||||
/// The base class for all lists.
|
||||
///\ingroup list
|
||||
//***************************************************************************
|
||||
class list_base
|
||||
{
|
||||
public:
|
||||
|
||||
typedef size_t size_type; ///< The type used for determining the size of list.
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the size of the list.
|
||||
//*************************************************************************
|
||||
size_type size() const
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Gets the maximum possible size of the list.
|
||||
//*************************************************************************
|
||||
size_type max_size() const
|
||||
{
|
||||
return MAX_SIZE;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Checks to see if the list is empty.
|
||||
//*************************************************************************
|
||||
bool empty() const
|
||||
{
|
||||
return count == 0;
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Checks to see if the list is full.
|
||||
//*************************************************************************
|
||||
bool full() const
|
||||
{
|
||||
return count == MAX_SIZE;
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
//*************************************************************************
|
||||
/// The constructor that is called from derived classes.
|
||||
//*************************************************************************
|
||||
list_base(size_type max_size)
|
||||
: MAX_SIZE(max_size)
|
||||
{
|
||||
}
|
||||
|
||||
size_type next_free; ///< The index of the next free node.
|
||||
size_type count; ///< The number of the used nodes.
|
||||
const size_type MAX_SIZE; ///< The maximum size of the list.
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Loading…
x
Reference in New Issue
Block a user