📄 elst2.h
字号:
/********************************************************************** * File: elst2.h (Formerly elist2.h) * Description: Double linked embedded list module include file. * Author: Phil Cheatle * Created: Wed Jan 23 11:04:47 GMT 1991 * * (C) Copyright 1991, Hewlett-Packard Ltd. ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** http://www.apache.org/licenses/LICENSE-2.0 ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. * **********************************************************************/#ifndef ELST2_H#define ELST2_H#include <stdio.h>#include "host.h"#include "serialis.h"#include "lsterr.h"class ELIST2_ITERATOR;/**********************************************************************DESIGN NOTE===========It would probably be possible to implement the ELIST2 classes as derivedclasses from ELIST. I haven't done this because:a) I think it would be harder to understand the code(Though the problem with not inheriting is that changes to ELIST must be reflected in ELIST2 and vice versa)b) Most of the code is inline so:i) The duplication in source does not affect the run time code size - the code is copied inline anyway! ii) The compiler should have a bit less work to do!**********************************************************************//********************************************************************** * CLASS - ELIST2_LINK * * Generic link class for doubly linked lists with embedded links * * Note: No destructor - elements are assumed to be destroyed EITHER after * they have been extracted from a list OR by the ELIST2 destructor which * walks the list. **********************************************************************/class DLLSYM ELIST2_LINK{ friend class ELIST2_ITERATOR; friend class ELIST2; ELIST2_LINK *prev; ELIST2_LINK *next; public: ELIST2_LINK() { //constructor prev = next = NULL; } ELIST2_LINK( //copy constructor const ELIST2_LINK &) { //dont copy link prev = next = NULL; } void operator= ( //dont copy links const ELIST2_LINK &) { prev = next = NULL; } /* NOTE that none of the serialise member functions are required for ELIST2_LINKs as they are never serialised. (We demand that the derived class terminates recursion - just to make sure that it defines the member functions anyway.) */};/********************************************************************** * CLASS - ELIST2 * * Generic list class for doubly linked lists with embedded links **********************************************************************/class DLLSYM ELIST2{ friend class ELIST2_ITERATOR; ELIST2_LINK *last; //End of list //(Points to head) ELIST2_LINK *First() { // return first return last ? last->next : NULL; } public: ELIST2() { //constructor last = NULL; } void internal_clear ( //destroy all links void (*zapper) (ELIST2_LINK *)); //ptr to zapper functn BOOL8 empty() { //is list empty? return !last; } BOOL8 singleton() { return last ? (last == last->next) : FALSE; } void shallow_copy( //dangerous!! ELIST2 *from_list) { //beware destructors!! last = from_list->last; } //ptr to copier functn void internal_deep_copy (ELIST2_LINK * (*copier) (ELIST2_LINK *), const ELIST2 * list); //list being copied void assign_to_sublist( //to this list ELIST2_ITERATOR *start_it, //from list start ELIST2_ITERATOR *end_it); //from list end INT32 length(); //# elements in list void sort ( //sort elements int comparator ( //comparison routine const void *, const void *)); void internal_dump ( //serialise each elem FILE * f, //to this file void element_serialiser ( //using this function FILE *, ELIST2_LINK *)); void internal_de_dump ( //de_serial each elem FILE * f, //from this file //using this function ELIST2_LINK * element_de_serialiser ( FILE *)); void prep_serialise(); //change last to count /* Note that dump() and de_dump() are not required as calls to dump/de_dump a list class should be handled by a class derived from this. make_serialise is not required for a similar reason. */};/*********************************************************************** * CLASS - ELIST2_ITERATOR * * Generic iterator class for doubly linked lists with embedded links **********************************************************************/class DLLSYM ELIST2_ITERATOR{ friend void ELIST2::assign_to_sublist(ELIST2_ITERATOR *, ELIST2_ITERATOR *); ELIST2 *list; //List being iterated ELIST2_LINK *prev; //prev element ELIST2_LINK *current; //current element ELIST2_LINK *next; //next element BOOL8 ex_current_was_last; //current extracted //was end of list BOOL8 ex_current_was_cycle_pt; //current extracted //was cycle point ELIST2_LINK *cycle_pt; //point we are cycling //the list to. BOOL8 started_cycling; //Have we moved off //the start? ELIST2_LINK *extract_sublist( //from this current... ELIST2_ITERATOR *other_it); //to other current public: ELIST2_ITERATOR() { //constructor list = NULL; } //unassigned list ELIST2_ITERATOR( //constructor ELIST2 *list_to_iterate); void set_to_list( //change list ELIST2 *list_to_iterate); void add_after_then_move( //add after current & ELIST2_LINK *new_link); //move to new void add_after_stay_put( //add after current & ELIST2_LINK *new_link); //stay at current void add_before_then_move( //add before current & ELIST2_LINK *new_link); //move to new void add_before_stay_put( //add before current & ELIST2_LINK *new_link); //stay at current void add_list_after( //add a list & ELIST2 *list_to_add); //stay at current void add_list_before( //add a list & ELIST2 *list_to_add); //move to it 1st item ELIST2_LINK *data() { //get current data #ifdef _DEBUG if (!current) NULL_DATA.error ("ELIST2_ITERATOR::data", ABORT, NULL); if (!list) NO_LIST.error ("ELIST2_ITERATOR::data", ABORT, NULL); #endif return current; } ELIST2_LINK *data_relative( //get data + or - ... INT8 offset); //offset from current ELIST2_LINK *forward(); //move to next element ELIST2_LINK *backward(); //move to prev element ELIST2_LINK *extract(); //remove from list //go to start of list ELIST2_LINK *move_to_first(); ELIST2_LINK *move_to_last(); //go to end of list void mark_cycle_pt(); //remember current BOOL8 empty() { //is list empty? #ifdef _DEBUG if (!list) NO_LIST.error ("ELIST2_ITERATOR::empty", ABORT, NULL); #endif return list->empty (); } BOOL8 current_extracted() { //current extracted? return !current; } BOOL8 at_first(); //Current is first? BOOL8 at_last(); //Current is last? BOOL8 cycled_list(); //Completed a cycle? void add_to_end( //add at end & ELIST2_LINK *new_link); //dont move void exchange( //positions of 2 links ELIST2_ITERATOR *other_it); //other iterator INT32 length(); //# elements in list void sort ( //sort elements int comparator ( //comparison routine const void *, const void *));};/*********************************************************************** * ELIST2_ITERATOR::set_to_list * * (Re-)initialise the iterator to point to the start of the list_to_iterate * over. **********************************************************************/inline void ELIST2_ITERATOR::set_to_list( //change list ELIST2 *list_to_iterate) { #ifdef _DEBUG if (!this) NULL_OBJECT.error ("ELIST2_ITERATOR::set_to_list", ABORT, NULL); if (!list_to_iterate) BAD_PARAMETER.error ("ELIST2_ITERATOR::set_to_list", ABORT, "list_to_iterate is NULL"); #endif list = list_to_iterate; prev = list->last; current = list->First (); next = current ? current->next : NULL; cycle_pt = NULL; //await explicit set started_cycling = FALSE; ex_current_was_last = FALSE; ex_current_was_cycle_pt = FALSE;}/*********************************************************************** * ELIST2_ITERATOR::ELIST2_ITERATOR * * CONSTRUCTOR - set iterator to specified list; **********************************************************************/inline ELIST2_ITERATOR::ELIST2_ITERATOR(ELIST2 *list_to_iterate) { set_to_list(list_to_iterate);}/*********************************************************************** * ELIST2_ITERATOR::add_after_then_move * * Add a new element to the list after the current element and move the * iterator to the new element. **********************************************************************/inline void ELIST2_ITERATOR::add_after_then_move( // element to add ELIST2_LINK *new_element) { #ifdef _DEBUG if (!this) NULL_OBJECT.error ("ELIST2_ITERATOR::add_after_then_move", ABORT, NULL); if (!list) NO_LIST.error ("ELIST2_ITERATOR::add_after_then_move", ABORT, NULL); if (!new_element) BAD_PARAMETER.error ("ELIST2_ITERATOR::add_after_then_move", ABORT, "new_element is NULL"); if (new_element->next) STILL_LINKED.error ("ELIST2_ITERATOR::add_after_then_move", ABORT, NULL); #endif if (list->empty ()) { new_element->next = new_element; new_element->prev = new_element; list->last = new_element; prev = next = new_element; } else { new_element->next = next; next->prev = new_element; if (current) { //not extracted new_element->prev = current; current->next = new_element; prev = current; if (current == list->last) list->last = new_element; } else { //current extracted new_element->prev = prev; prev->next = new_element; if (ex_current_was_last) list->last = new_element; if (ex_current_was_cycle_pt) cycle_pt = new_element; } } current = new_element;}/*********************************************************************** * ELIST2_ITERATOR::add_after_stay_put * * Add a new element to the list after the current element but do not move * the iterator to the new element. **********************************************************************/inline void ELIST2_ITERATOR::add_after_stay_put( // element to add ELIST2_LINK *new_element) { #ifdef _DEBUG if (!this) NULL_OBJECT.error ("ELIST2_ITERATOR::add_after_stay_put", ABORT, NULL); if (!list) NO_LIST.error ("ELIST2_ITERATOR::add_after_stay_put", ABORT, NULL); if (!new_element) BAD_PARAMETER.error ("ELIST2_ITERATOR::add_after_stay_put", ABORT, "new_element is NULL"); if (new_element->next)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -