MQLplus/lib_structures/lib_linked_list.mqh
super.admin 466f9ca5c5 convert
2025-05-30 16:09:52 +02:00

200 lines
13 KiB
MQL5

#ifndef LIB_MQLPLUS_OBJ_LIST_TEMPLATES_MQH_INCLUDED
#define LIB_MQLPLUS_OBJ_LIST_TEMPLATES_MQH_INCLUDED
#property version "1.0";
/**********************************************************************************
* Copyright (C) 2010-2022 Dominik Egert <info@freie-netze.de>
*
* This file is the objects include file.
*
* MQLplus, including this file may not be copied and/or distributed
* without explecit permit by the author.
* Author Dominik Egert / Freie Netze UG.
**********************************************************************************
*
* Version: 1.0
* State: production
*
* File information
* ================
*
*
*
*/
#ifdef DBG_MSG_TRACE_FILE_LOADER
DBG_MSG_TRACE_FILE_LOADER;
#endif
// Incluse base object
#include "nodes/lib_obj_node_linkedlist.mqh"
#include "base/lib_base_linked_list.mqh"
/*********************************************************************************************************************************************************/
/* */
/* MQLplus data structures */
/* */
/*********************************************************************************************************************************************************/
///////////////////////////////////////
//
// Linked list object
//
// Derived simple type class object
#define LIST_NODE_TYPE object_list_node<USER_DATA>
#define LIST_TYPE _linked_list<USER_DATA, LIST_NODE_TYPE>
template <typename USER_DATA>
struct linked_list : public LIST_TYPE
{
// Constructors / Destructor
linked_list() : LIST_TYPE() { };
linked_list(const linked_list<USER_DATA>& p_in) : LIST_TYPE(p_in) { };
~linked_list() { };
// Assignment operator
void operator=(const linked_list<USER_DATA>& p_in) { LIST_TYPE::operator=((LIST_TYPE)p_in); };
// Element functions
void add(USER_DATA& p_in) { LIST_TYPE::append(p_in); };
void append(USER_DATA& p_in) { LIST_TYPE::append(p_in); };
void prepend(USER_DATA& p_in) { LIST_TYPE::prepend(p_in); };
void insert(const int idx, USER_DATA& p_in) { LIST_TYPE::insert(idx, p_in); };
void remove(const int idx) { LIST_TYPE::remove(idx, false); };
// Comparison operators
const bool operator==(const linked_list<USER_DATA>& p_in) { return(LIST_TYPE::operator==((LIST_TYPE)p_in)); }
const bool operator!=(const linked_list<USER_DATA>& p_in) { return(!operator==(p_in)); }
};
#undef LIST_NODE_TYPE
#undef LIST_TYPE
// Derived complex type class for heap objects
#define LIST_NODE_TYPE ptr_object_list_node<USER_DATA>
#define LIST_TYPE _linked_list<USER_DATA*, LIST_NODE_TYPE>
template <typename USER_DATA>
struct ptr_linked_list : public LIST_TYPE
{
// Constructors / Destructor
ptr_linked_list() : LIST_TYPE() { };
ptr_linked_list(const ptr_linked_list<USER_DATA>& p_in) : LIST_TYPE(p_in) { };
ptr_linked_list(const bool _do_not_destroy) : _linked_list() { p_params.do_not_destroy = _do_not_destroy; };
~ptr_linked_list() { };
// Operator
void operator=(const ptr_linked_list<USER_DATA>& p_in) { LIST_TYPE::operator=((LIST_TYPE)p_in); };
// Buffer functions
const bool dnd(const bool _do_not_destroy = true) { p_params.do_not_destroy |= _do_not_destroy; return(p_params.do_not_destroy); };
// Element functions
void add(USER_DATA& p_in) { USER_DATA* tmp_ptr = new USER_DATA(p_in); LIST_TYPE::append(tmp_ptr); };
void append(USER_DATA& p_in) { USER_DATA* tmp_ptr = new USER_DATA(p_in); LIST_TYPE::append(tmp_ptr); };
void prepend(USER_DATA& p_in) { USER_DATA* tmp_ptr = new USER_DATA(p_in); LIST_TYPE::prepend(tmp_ptr); };
void insert(const int idx, USER_DATA& p_in) { USER_DATA* tmp_ptr = new USER_DATA(p_in); LIST_TYPE::insert(idx, tmp_ptr); };
void add(USER_DATA* p_in) { LIST_TYPE::append(p_in); };
void append(USER_DATA* p_in) { LIST_TYPE::append(p_in); };
void prepend(USER_DATA* p_in) { LIST_TYPE::prepend(p_in); };
void insert(const int idx, USER_DATA* p_in) { LIST_TYPE::insert(idx, p_in); };
void remove(const int idx, const bool _do_not_destroy = false) { LIST_TYPE::remove(idx, p_params.do_not_destroy || _do_not_destroy); };
// Comparison operators
const bool operator==(const ptr_linked_list<USER_DATA>& p_in) { return(LIST_TYPE::operator==((LIST_TYPE)p_in)); }
const bool operator!=(const ptr_linked_list<USER_DATA>& p_in) { return(!operator==(p_in)); }
};
#undef LIST_NODE_TYPE
#undef LIST_TYPE
///////////////////////////////////////
//
// Sorted linked list object
//
// Derived simple type class object
#define LIST_NODE_TYPE object_list_node<USER_DATA>
#define LIST_TYPE _linked_list<USER_DATA, LIST_NODE_TYPE>
#define SORTED_LIST_TYPE _sorted_linked_list<USER_DATA, linked_list<USER_DATA>, LIST_NODE_TYPE>
//#define SORTED_LIST_TYPE _sorted_linked_list<USER_DATA, LIST_TYPE, LIST_NODE_TYPE>
template <typename USER_DATA>
struct sorted_linked_list : public SORTED_LIST_TYPE
{
// Constructors / Destructor
sorted_linked_list() : SORTED_LIST_TYPE() { };
sorted_linked_list(const sorted_linked_list<USER_DATA>& p_in) : SORTED_LIST_TYPE(p_in) { };
~sorted_linked_list() { };
// Assignment operator
void operator=(const sorted_linked_list<USER_DATA>& p_in) { LIST_TYPE::operator=((LIST_TYPE)p_in); };
// Element functions
const bool add(USER_DATA& p_in) { return(SORTED_LIST_TYPE::add(p_in)); };
const bool remove(USER_DATA& p_in) { return(SORTED_LIST_TYPE::remove(p_in, false)); };
// Comparison operators
const bool operator==(const sorted_linked_list<USER_DATA>& p_in) { return(LIST_TYPE::operator==((LIST_TYPE)p_in)); }
const bool operator!=(const sorted_linked_list<USER_DATA>& p_in) { return(!operator==(p_in)); }
};
#undef SORTED_LIST_TYPE
#undef LIST_TYPE
#undef LIST_NODE_TYPE
// Derived complex type class object for heap objects
#define LIST_NODE_TYPE ptr_object_list_node<USER_DATA>
#define LIST_TYPE _linked_list<USER_DATA*, LIST_NODE_TYPE>
#define SORTED_LIST_TYPE _sorted_linked_list<USER_DATA*, ptr_linked_list<USER_DATA>, LIST_NODE_TYPE>
//#define SORTED_LIST_TYPE _sorted_linked_list<USER_DATA*, LIST_TYPE, LIST_NODE_TYPE>
template <typename USER_DATA>
struct ptr_sorted_linked_list : public SORTED_LIST_TYPE
{
// Constructors / Destructor
ptr_sorted_linked_list() : SORTED_LIST_TYPE() { };
ptr_sorted_linked_list(const ptr_sorted_linked_list<USER_DATA>& p_in) : SORTED_LIST_TYPE(p_in) { };
ptr_sorted_linked_list(const bool _do_not_destroy) : _sorted_linked_list() { p_params.do_not_destroy = _do_not_destroy; };
~ptr_sorted_linked_list() { };
// Assignment operator
void operator=(const ptr_sorted_linked_list<USER_DATA>& p_in) { LIST_TYPE::operator=((LIST_TYPE)p_in); };
// Buffer functions
const bool dnd(const bool _do_not_destroy = true) { p_params.do_not_destroy |= _do_not_destroy; return(p_params.do_not_destroy); };
// Element functions
const bool add(USER_DATA& p_in, const bool unique = false) { USER_DATA* new_ptr = new USER_DATA(p_in); if(!SORTED_LIST_TYPE::add(new_ptr, unique)) { delete(new_ptr); return(false); } return(true); };
const bool remove(USER_DATA& p_in, const bool _do_not_destroy = false) { USER_DATA* p_tmp = ::GetPointer(p_in); return(SORTED_LIST_TYPE::remove(p_tmp, p_params.do_not_destroy || _do_not_destroy)); };
const bool add(USER_DATA* p_in, const bool unique = false) { return(SORTED_LIST_TYPE::add(p_in, unique)); };
const bool remove(USER_DATA* p_in, const bool _do_not_destroy = false) { return(SORTED_LIST_TYPE::remove(p_in, p_params.do_not_destroy || _do_not_destroy)); };
// Comparison operators
const bool operator==(const ptr_sorted_linked_list<USER_DATA>& p_in) { return(LIST_TYPE::operator==((LIST_TYPE)p_in)); }
const bool operator!=(const ptr_sorted_linked_list<USER_DATA>& p_in) { return(!operator==(p_in)); }
};
#undef SORTED_LIST_TYPE
#undef LIST_TYPE
#undef LIST_NODE_TYPE
//
// END MQL data structures */
//*********************************************************************************************************************************************************/
#endif // LIB_MQLPLUS_OBJ_LIST_TEMPLATES_MQH_INCLUDED