 |
Gorgon Game Engine
|
Go to the documentation of this file.
5 #pragma warning(error: 4239)
14 namespace Containers {
25 sorter(
const P_ &predicate) : predicate(predicate) {}
27 bool operator ()(
const T_ *left,
const T_*right)
const {
28 if(left==NULL || right==NULL)
31 return predicate(*left,*right);
39 template<
class O_,
class C_>
46 Iterator_() : col(NULL), offset(-1) {
49 Iterator_(
const Iterator_ &it) : col(it.col), offset(it.offset) {
69 Iterator_(C_ &c,
long offset=0) : col(&c), offset(offset) {
77 throw std::out_of_range(
"Iterator is not valid.");
79 return *col->list[offset];
82 bool isvalid()
const {
83 return offset>=0 && offset<(col->GetCount());
86 bool isinrange()
const {
87 return offset>=0 || offset<(col->GetCount());
90 bool moveby(
long amount) {
92 if(amount==0)
return isvalid();
99 bool compare(
const Iterator_ &it)
const {
100 if(!it.isvalid() && !isvalid())
return true;
101 return it.offset==offset;
104 void set(
const Iterator_ &it) {
109 long distance(
const Iterator_ &it)
const {
110 return it.offset-offset;
113 bool isbefore(
const Iterator_ &it)
const {
114 return offset<it.offset;
121 Iterator_ &
operator =(
const Iterator_ &iterator) {
143 this->offset=it.offset;
161 Adder &operator ,(T_ &item) {
166 Adder &operator ,(T_ *item) {
185 template<
typename... Args_>
187 list.reserve(
sizeof...(Args_)+1);
197 template<
typename... Args_>
199 list.reserve(
sizeof...(Args_)+1);
237 return (
long)list.size();
242 return (
long)list.size();
249 if(std::find(list.begin(), list.end(),
Data)==list.end()) {
250 list.push_back(
Data);
279 throw std::out_of_range(
"No items in the collection");
290 template<
typename... Args_>
299 template<
typename... Args_>
300 int Add(T_& data, Args_ &... args) {
301 int ret =
Add(&data) ? 1 : 0;
308 template<
typename... Args_>
310 auto t =
new T_(std::forward<Args_>(args)...);
319 if(std::find(list.begin(), list.end(), data)!=list.end())
return false;
322 before=(long)list.size();
324 if(before<0 || before>(
long)list.size())
325 throw std::out_of_range(
"Invalid location");
327 if(before==list.size()) {
331 list.resize(list.size()+1);
333 for(
long i=(
long)list.size()-1;i>before;i--)
344 return Insert(&data, before);
349 bool Insert(T_* data,
const T_ &before) {
355 bool Insert(T_& data,
const T_ &before) {
361 template<
typename... Args_>
363 auto t=
new T_(args...);
371 template<
typename... Args_>
373 auto t=
new T_(args...);
382 before = list.size();
383 if(index>=list.size())
384 throw std::out_of_range(
"Invalid location");
385 if(before>list.size())
386 throw std::out_of_range(
"Invalid location");
393 for(
long i=index; i>before; i--)
398 else if(before==list.size()) {
401 for(
long i=index; i<list.size()-1; i++)
404 list[list.size()-1]=t;
408 for(
long i=index; i<before; i++)
446 list.erase(list.begin()+index);
467 list.erase(list.begin() + tmp.offset);
473 list.erase(list.begin() + tmp.offset);
481 list.erase(list.begin()+index);
506 delete tmp.CurrentPtr();
507 list.erase(list.begin() + tmp.offset);
513 delete tmp.CurrentPtr();
514 list.erase(list.begin() + tmp.offset);
528 ConstIterator
Find(
const T_ *item)
const {
533 ConstIterator
Find(
const T_ &item)
const {
539 auto it=find(list.begin(), list.end(), item);
544 return long(it-list.begin());
556 std::sort(list.begin(), list.end(), sorter<P_>(predicate));
562 std::sort(list.begin(), list.end(), sorter<std::less<T_>>(std::less<T_>()));
570 throw std::out_of_range(
"Index out of range");
576 T_ &
Get(
long index)
const {
577 const T_ *r=get_(index);
580 throw std::out_of_range(
"Index out of range");
604 return Iterator(*
this, (
long)list.size());
614 return Iterator(*
this,
long(list.size()-1));
619 return ConstIterator(*
this, 0);
623 ConstIterator
end()
const {
624 return ConstIterator(*
this, (
long)list.size());
629 return ConstIterator(*
this, 0);
634 return ConstIterator(*
this,
long(list.size()-1));
646 std::vector<T_*> newlist;
666 std::vector<T_*> newlist;
674 list.reserve(amount);
679 std::vector<T_ *> list;
681 void removeat(
long absolutepos) {
685 void deleteat(
long absolutepos) {
689 T_ *get_(
long Index) {
690 if(Index<0 || Index>=(
long)list.size())
696 const T_ *get_(
long Index)
const {
697 if(Index<0 || Index>list.size())
void Remove(const I_ &first, const I_ &end)
This function works with collection iterators.
Definition: Iterator.h:386
void Remove(long index)
Removes an item from the collection using its index.
Definition: Collection.h:445
void Remove(Iterator &it)
Definition: Collection.h:470
constexpr Type Data
Data resource.
Definition: GID.h:164
void Remove(const T_ &data)
Removes an item from the collection using its reference.
Definition: Collection.h:460
void Remove(const T_ *item)
Removes an item from the collection using its pointer.
Definition: Collection.h:451
void swap(Collection< T_ > &l, Collection< T_ > &r)
Swaps two collections.
Definition: Collection.h:707
ConstIterator Last() const
returns the iterator to the last item
Definition: Collection.h:633
Collection(T_ &t, Args_ &... args)
Initializing constructor.
Definition: Collection.h:186
bool Add(T_ *Data)
Adds the given item to the end of the list if it is not already in the list.
Definition: Collection.h:248
void MoveBefore(long index, const T_ &before)
this method moves the given object in the collection in front of the reference
Definition: Collection.h:416
Adder operator+=(T_ *Data)
Adds items to the end of the list. Use comma to add more than one item.
Definition: Collection.h:431
Iterator end()
end iterator
Definition: Collection.h:603
void MoveBefore(const T_ &index, const T_ &before)
this method moves the given object in the collection in front of the reference
Definition: Collection.h:426
int Add(T_ *Data, Args_ *... args)
Adds the given item to the end of the list, returns the number of added elements.
Definition: Collection.h:291
long GetSize() const
Returns number of elements.
Definition: Collection.h:241
void Remove(ConstIterator &it)
Definition: Collection.h:464
Iterator begin()
Definition: Collection.h:598
void Sort()
Sorts items in the collection.
Definition: Collection.h:561
bool Insert(T_ &data, const T_ &before)
this method adds the given object in front of the reference.
Definition: Collection.h:355
Iterator Last()
returns the iterator to the last item
Definition: Collection.h:613
Collection & operator=(Collection &&col)
Move assignment.
Definition: Collection.h:214
Iterator Find(const T_ *item)
Searches the position of a given item, if not found end iterator returned.
Definition: Collection.h:518
void Delete(const T_ *item)
Deletes an item from the collection using its pointer.
Definition: Collection.h:487
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
void MoveBefore(long index, long before)
this method moves the given object in the collection in front of the reference
Definition: Collection.h:380
void Delete(ConstIterator &it)
Definition: Collection.h:503
void Sort(P_ predicate=P_())
Sorts items in the collection.
Definition: Collection.h:555
void Delete(T_ &data)
Deletes an item from the collection using its reference.
Definition: Collection.h:499
ConstIterator end() const
end iterator
Definition: Collection.h:623
bool Add(T_ &data)
Adds a the given item to the end of the list if it is not already in the list.
Definition: Collection.h:260
Collection is a container for reference typed objects.
Definition: Collection.h:21
bool Insert(T_ *data, const T_ &before)
this method adds the given object in front of the reference.
Definition: Collection.h:349
Collection(Collection &&col)
Move constructor.
Definition: Collection.h:210
contains filesystem Iterator. Lists file and directories.
Const iterator allows iteration of const collections.
Definition: Collection.h:137
Iterator_< T_, Collection > Iterator
Regular iterator.
Definition: Collection.h:134
Iterator First()
returns the iterator to the first item
Definition: Collection.h:608
long GetCount() const
Returns number of elements.
Definition: Collection.h:236
Collection(const Collection &)=delete
Disabled.
void Reserve(long amount)
Allocates memory for the given amount of items.
Definition: Collection.h:673
Iterator Find(const T_ &item)
Searches the position of a given item, if not found end iterator returned.
Definition: Collection.h:523
ConstIterator(const Iterator &it)
Regular iterators can be converted to const iterators.
Definition: Collection.h:141
T_ & InsertNew(const T_ &before, Args_... args)
Creates a new item and inserts it before the given reference.
Definition: Collection.h:362
void Collapse()
Clears the contents of the collection and releases the memory used for the list.
Definition: Collection.h:645
void Clear()
Removes all items from the list, allocated memory for the list stays.
Definition: Collection.h:639
T_ & AddNew(Args_ &&... args)
Creates a new item and adds to the end of the collection.
Definition: Collection.h:309
ConstIterator begin() const
begin iterator
Definition: Collection.h:618
int Add(T_ &data, Args_ &... args)
Adds a the given item to the end of the list.
Definition: Collection.h:300
T_ & Get(long index) const
Returns the element at the given index. Checks and throws if out of range.
Definition: Collection.h:576
void Swap(Collection &col)
Swaps the given collection with this one.
Definition: Collection.h:222
void MoveBefore(const T_ &index, long before)
this method moves the given object in the collection in front of the reference
Definition: Collection.h:421
T_ & InsertNew(long before, Args_... args)
Creates a new item and inserts it before the given reference.
Definition: Collection.h:372
ConstIterator First() const
returns the iterator to the first item
Definition: Collection.h:628
T_ & operator[](long index)
Returns the item at a given index.
Definition: Collection.h:586
void Delete(long index)
Deletes an item from the collection using its index.
Definition: Collection.h:478
void Destroy()
Destroys the entire collection, effectively deleting the contents and the list including all the memo...
Definition: Collection.h:662
bool Insert(T_ *data, long before)
this method adds the given object in front of the reference.
Definition: Collection.h:318
Collection & operator=(const Collection &)=delete
Disabled.
void DeleteAll()
Deletes and removes all elements in the collection.
Definition: Collection.h:653
T_ & Get(long index)
Returns the element at the given index. Checks and throws if out of range.
Definition: Collection.h:566
bool Push(T_ *Data)
Adds the given item to the end of the list if it is not already in the list.
Definition: Collection.h:266
T_ & Pop()
Removes and returns the last item in the collection.
Definition: Collection.h:277
void Delete(Iterator &it)
Definition: Collection.h:510
Collection(T_ *t, Args_ *... args)
Initializing constructor.
Definition: Collection.h:198
Generic iterator interface.
Definition: Iterator.h:31
ConstIterator Find(const T_ &item) const
Searches the position of a given item, if not found end iterator returned.
Definition: Collection.h:533
long FindLocation(const T_ &item) const
Searches the position of a given item, if not found -1 is returned.
Definition: Collection.h:548
bool Insert(T_ &data, long before)
this method adds the given object in front of the reference.
Definition: Collection.h:343
Collection()=default
Default constructor.
bool Push(T_ &data)
Adds a the given item to the end of the list if it is not already in the list.
Definition: Collection.h:272
long FindLocation(const T_ *item) const
Searches the position of a given item, if not found -1 is returned.
Definition: Collection.h:538
Collection Duplicate() const
Duplicates this collection.
Definition: Collection.h:228
ConstIterator Find(const T_ *item) const
Searches the position of a given item, if not found end iterator returned.
Definition: Collection.h:528