JX Application Framework
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | List of all members
JArray< T > Class Template Reference

#include <JArray.h>

Inheritance diagram for JArray< T >:
[legend]

Public Member Functions

 JArray (const JSize blockSize=10)
 
 JArray (const JArray< T > &source)
 
 JArray (JArray< T > &&dyingSource) noexcept
 
 ~JArray () override
 
JArray< T > & operator= (const JArray< T > &source)
 
 operator const T * () const
 
const T * GetCArray () const
 
T * AllocateCArray () const
 
GetItem (const JIndex index) const
 
void SetItem (const JIndex index, const T &data)
 
GetItemFromEnd (const JIndex index) const
 
void SetItemFromEnd (const JIndex index, const T &data)
 
GetFirstItem () const override
 
GetLastItem () const override
 
void InsertItemAtIndex (const JIndex index, const T &data)
 
void PrependItem (const T &data) override
 
void AppendItem (const T &data) override
 
void RemoveItem (const JIndex index)
 
void RemoveNextItems (const JIndex firstIndex, const JSize count)
 
void RemovePrevItems (const JIndex lastIndex, const JSize count)
 
void RemoveItems (const JIndexRange &range)
 
void RemoveItems (const JListT::ItemsRemoved &info)
 
void RemoveAll () override
 
void MoveItemToIndex (const JIndex currentIndex, const JIndex newIndex)
 
void MoveItemToIndex (const JListT::ItemMoved &info)
 
void SwapItems (const JIndex index1, const JIndex index2)
 
void SwapItems (const JListT::ItemsSwapped &info)
 
JListIterator< T > * NewIterator (const JListT::Position start=JListT::kStartAtBeginning, const JIndex index=0) override
 
JListIterator< T > * NewIterator (const JListT::Position start=JListT::kStartAtBeginning, const JIndex index=0) const override
 
JSize GetBlockSize () const
 
void SetBlockSize (const JSize newBlockSize)
 
void Sort ()
 
bool InsertSorted (const T &data, const bool insertIfDuplicate=true, JIndex *index=nullptr)
 
JIndex GetInsertionSortIndex (const T &data, bool *isDuplicate=nullptr) const
 
bool SearchSorted (const T &target, const JListT::SearchReturn which, JIndex *index) const
 
JIndex SearchSortedOTI (const T &target, const JListT::SearchReturn which, bool *found) const
 
void QuickSort (int(*compare)(const void *, const void *))
 
const T * begin () const
 
const T * end () const
 
- Public Member Functions inherited from JList< T >
 JList ()
 
 JList (const JList< T > &source)
 
 ~JList () override
 
void SetCompareFunction (const std::function< std::weak_ordering(const T &, const T &)> compareFn)
 
void CopyCompareFunction (const JList< T > &source)
 
void ClearCompareFunction ()
 
JListT::SortOrder GetSortOrder () const
 
void SetSortOrder (const JListT::SortOrder order)
 
bool IsSorted () const
 
- Public Member Functions inherited from JCollection
 JCollection ()
 
 JCollection (const JCollection &source)
 
 ~JCollection () override
 
JCollectionoperator= (const JCollection &source)
 
JSize GetItemCount () const
 
bool IsEmpty () const
 
bool IndexValid (const JIndex index) const
 
JIndex GetIndexFromEnd (const JIndex index) const
 
JString ToString () const override
 
- Public Member Functions inherited from JBroadcaster
 JBroadcaster ()
 
virtual ~JBroadcaster ()
 
JBroadcasteroperator= (const JBroadcaster &source)
 
bool HasSenders () const
 
JSize GetSenderCount () const
 
bool HasRecipients () const
 
JSize GetRecipientCount () const
 
template<class T >
void ListenTo (const JBroadcaster *sender, const std::function< void(const T &)> &f)
 

Protected Member Functions

const T & ProtectedGetItem (const JIndex index) const
 
- Protected Member Functions inherited from JList< T >
std::function< std::weak_ordering(const T &, const T &)> * GetCompareFunction () const
 
void ListAssigned (const JList< T > &source)
 
void NotifyIterators (const JBroadcaster::Message &message)
 
- Protected Member Functions inherited from JCollection
void ItemAdded ()
 
void ItemRemoved ()
 
void SetItemCount (const JSize newItemCount)
 
- Protected Member Functions inherited from JBroadcaster
 JBroadcaster (const JBroadcaster &source)
 
void ListenTo (const JBroadcaster *sender)
 
void StopListening (const JBroadcaster *sender)
 
void ClearWhenGoingAway (const JBroadcaster *sender, void *pointerToMember)
 
void StopListening (const JBroadcaster *sender, const std::type_info &messageType)
 
template<class T >
void Send (JBroadcaster *recipient, const T &message)
 
template<class T >
void Broadcast (const T &message)
 
virtual void Receive (JBroadcaster *sender, const Message &message)
 
void SendWithFeedback (JBroadcaster *recipient, Message *message)
 
void BroadcastWithFeedback (Message *message)
 
virtual void ReceiveWithFeedback (JBroadcaster *sender, Message *message)
 
virtual void ReceiveGoingAway (JBroadcaster *sender)
 

Detailed Description

template<class T>
class JArray< T >

Interface for JArray class

Constructor & Destructor Documentation

◆ JArray() [1/3]

template<class T >
JArray< T >::JArray ( const JSize  blockSize = 10)
                    The JArray Template Class

Template for a dynamically sized array. The data is stored contiguously.

Since memcpy and memmove are used for large scale rearrangement of
the array items, this can -only- be used for structs.
To store objects, use the JPtrArray template class.

We do not override ToString(): Since this function is virtual, it is always instantiated. Thus, we cannot print the values, because they might not have operator<<

◆ JArray() [2/3]

template<class T >
JArray< T >::JArray ( const JArray< T > &  source)

◆ JArray() [3/3]

template<class T >
JArray< T >::JArray ( JArray< T > &&  dyingSource)
noexcept

◆ ~JArray()

template<class T >
JArray< T >::~JArray ( )
override

Member Function Documentation

◆ AllocateCArray()

template<class T >
T * JArray< T >::AllocateCArray ( ) const

◆ AppendItem()

template<class T >
void JArray< T >::AppendItem ( const T &  data)
overridevirtual

Implements JList< T >.

◆ begin()

template<class T >
const T * JArray< T >::begin ( ) const
inline

◆ end()

template<class T >
const T * JArray< T >::end ( ) const
inline

◆ GetBlockSize()

template<class T >
JSize JArray< T >::GetBlockSize ( ) const

itsBlockSize helps prevent constant reallocation of memory in a "noisy" environment. Increase it if you need to add and remove a lot of items without reallocating memory. The larger it is, the more memory can potentially be wasted (allocated but not used).

◆ GetCArray()

template<class T >
const T * JArray< T >::GetCArray ( ) const

◆ GetFirstItem()

template<class T >
T JArray< T >::GetFirstItem ( ) const
overridevirtual

Implements JList< T >.

◆ GetInsertionSortIndex()

template<class T >
JIndex JArray< T >::GetInsertionSortIndex ( const T &  data,
bool isDuplicate = nullptr 
) const

Returns index where jnew item should be inserted in order to keep the list sorted. If there is an item that matches the given one, isDuplicate is set to true.

◆ GetItem()

template<class T >
T JArray< T >::GetItem ( const JIndex  index) const

◆ GetItemFromEnd()

template<class T >
T JArray< T >::GetItemFromEnd ( const JIndex  index) const

◆ GetLastItem()

template<class T >
T JArray< T >::GetLastItem ( ) const
overridevirtual

Implements JList< T >.

◆ InsertItemAtIndex()

template<class T >
void JArray< T >::InsertItemAtIndex ( const JIndex  index,
const T &  data 
)

Insert an item into the array at the specified index. Any items at or below index are moved down. If index is any value greater than the current size, then the item is appended to the end of the array.

◆ InsertSorted()

template<class T >
bool JArray< T >::InsertSorted ( const T &  data,
const bool  insertIfDuplicate = true,
JIndex index = nullptr 
)

Returns true if the item was inserted. Obviously, this is only useful if insertIfDuplicate==false.

If index!=nullptr, it contains the index where the item was inserted.

◆ MoveItemToIndex() [1/2]

template<class T >
void JArray< T >::MoveItemToIndex ( const JIndex  currentIndex,
const JIndex  newIndex 
)

◆ MoveItemToIndex() [2/2]

template<class T >
void JArray< T >::MoveItemToIndex ( const JListT::ItemMoved info)

◆ NewIterator() [1/2]

template<class T >
JListIterator< T > * JArray< T >::NewIterator ( const JListT::Position  start = JListT::kStartAtBeginning,
const JIndex  index = 0 
) const
overridevirtual

Implements JList< T >.

◆ NewIterator() [2/2]

template<class T >
JListIterator< T > * JArray< T >::NewIterator ( const JListT::Position  start = JListT::kStartAtBeginning,
const JIndex  index = 0 
)
overridevirtual

Implements JList< T >.

◆ operator const T *()

template<class T >
JArray< T >::operator const T * ( ) const
explicit

◆ operator=()

template<class T >
JArray< T > & JArray< T >::operator= ( const JArray< T > &  source)

◆ PrependItem()

template<class T >
void JArray< T >::PrependItem ( const T &  data)
overridevirtual

Implements JList< T >.

◆ ProtectedGetItem()

template<class T >
const T & JArray< T >::ProtectedGetItem ( const JIndex  index) const
protected

◆ QuickSort()

template<class T >
void JArray< T >::QuickSort ( int(*)(const void *, const void *)  compare)

Calls qsort() and broadcasts JListT::Sorted afterwards.

Since qsort() wants the sort order to be encoded in the comparison function, and since the comparison function prototype is different, we do -not- use the sorting information from JList.

Do NOT use this for JPtrArrays:

Downcasting or upcasting a pointer in C++ usually requires changing its value to include or exclude parts of the object data. Casting from void* to a base class is therefore fatal because the compiler can't perform the adjustment.

◆ RemoveAll()

template<class T >
void JArray< T >::RemoveAll ( )
overridevirtual

Implements JList< T >.

◆ RemoveItem()

template<class T >
void JArray< T >::RemoveItem ( const JIndex  index)

◆ RemoveItems() [1/2]

template<class T >
void JArray< T >::RemoveItems ( const JIndexRange range)

◆ RemoveItems() [2/2]

template<class T >
void JArray< T >::RemoveItems ( const JListT::ItemsRemoved info)

◆ RemoveNextItems()

template<class T >
void JArray< T >::RemoveNextItems ( const JIndex  firstIndex,
const JSize  count 
)

◆ RemovePrevItems()

template<class T >
void JArray< T >::RemovePrevItems ( const JIndex  lastIndex,
const JSize  count 
)

◆ SearchSorted()

template<class T >
bool JArray< T >::SearchSorted ( const T &  target,
const JListT::SearchReturn  which,
JIndex index 
) const

Returns true if the specified target is found.

"which" specifies what to return if more than one item matches the given target.

◆ SearchSortedOTI()

template<class T >
JIndex JArray< T >::SearchSortedOTI ( const T &  target,
const JListT::SearchReturn  which,
bool found 
) const

Returns index where the item is or where it should be inserted in order to keep the list sorted. If there is an item that matches the given one, *found is set to true.

OTI = option to insert

◆ SetBlockSize()

template<class T >
void JArray< T >::SetBlockSize ( const JSize  newBlockSize)

◆ SetItem()

template<class T >
void JArray< T >::SetItem ( const JIndex  index,
const T &  data 
)

◆ SetItemFromEnd()

template<class T >
void JArray< T >::SetItemFromEnd ( const JIndex  index,
const T &  data 
)

◆ Sort()

template<class T >
void JArray< T >::Sort ( )

We minimize the number of moves (O(N)) because MoveItemToIndex() invokes JBroadcaster::Broadcast(), which can take arbitrary amounts of time.

By using insertion sort to sort item k+1 into the already sorted list of k items, we obtain O(N log N) comparisons.

The number of moves is always O(N^2).

◆ SwapItems() [1/2]

template<class T >
void JArray< T >::SwapItems ( const JIndex  index1,
const JIndex  index2 
)

◆ SwapItems() [2/2]

template<class T >
void JArray< T >::SwapItems ( const JListT::ItemsSwapped info)

The documentation for this class was generated from the following files: