source: abuse/trunk/src/imlib/linked.hpp @ 57

Last change on this file since 57 was 57, checked in by Sam Hocevar, 11 years ago
  • Move each header to the same directory as its corresponding source, to get a better idea of which files are likely to export symbols.
File size: 3.9 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10// linked.hpp  - linked list and linked list node classes
11// written June 2, 1992 by Jonathan Clark  (at home)
12// these classes provide the basic groundwork for any future linked list
13// please derive your own linked_node subclass and define the virtual
14// function compare.
15// example compare function
16//   virtual int compare(void *n1, int field)
17//        {return ((classname *) n1)->data > data);}
18//  should return (1 if n1 is greater than (self)) else return 0
19//  field is the value determined by linked_list::set_sort_field
20//  this defaults to 1
21
22
23#ifndef linkman         
24#define linkman
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#define loop(controll,first,inside) { (linked_node *)controll=first; \
30  if (first) do { inside (linked_node *) controll=controll->next(); } \
31  while ((linked_node *) controll!=first); }
32
33#define loopt(type,controll,first,inside) { controll=(type *)(first); \
34  if (first) do { inside controll=(type *)(controll->next()); } \
35  while (controll!=(type *)(first)); }
36
37
38#define loop_rev(controll,last,inside) { (linked_node *)controll=last; \
39  if (first) do { inside (linked_node *) controll=controll->last(); } \
40  while ((linked_node *) controll!=last); }
41
42#define loopct(type,controll,first,cond,inside) { controll=(type *)first; \
43  if (first && (cond)) do { inside controll=(type *)controll->next(); } \
44  while (controll!=(type *)first && (cond)); }
45
46#define loop_fort(type,controll,first,x) \
47  int x=0; \
48  if (first) \
49     for (controll=(type *)(first); \
50          (!x || (controll)!=(type *)(first));\
51          controll=(type *)(controll->next()),x++)
52
53#define loop_forct(type,controll,first,cond,x) int x=0; if (first) for \
54  (controll=(type *)(first);cond && (!x || controll!=(type *)(first));\
55  controll=(type *)(controll->next()),x++)
56
57class linked_node
58{
59  class linked_node *nextp, *lastp;
60public:
61  virtual int compare(void *n1, int field)     {return(0);}  // default is = (equal)
62  class linked_node *next()              {return nextp;}
63  class linked_node *last()              {return lastp;}
64  void set_next(class linked_node *p)    {nextp=p;}
65  void set_last(class linked_node *p)    {lastp=p;}
66  virtual ~linked_node() { ; }
67  linked_node() { nextp=NULL; lastp=NULL; }
68};
69
70// this is the basic class for all linked_list
71// it's features should beself explanitory
72// openly use the functions listed after the keyword PUBLIC
73// type conversions may be nessary if you derive a class of nodes of your own
74// for example shape is an class derived from linked_node.
75// to add a shape to linked lis I have to say
76// mylist.add_end( (linked_node *) myshape_pointer);
77// unlink removes a node from the list via pointers but does not deallocate
78// it from the heap
79// the destructor for linked_list will get dispose of all the nodes as
80// well, so if you don't want something deleted then you must unlink
81// it from the list before the destructor is called
82
83class linked_list
84{
85  class linked_node *fn, *cn;     // first and current nodes
86  int nn; char sortby;
87public :
88  linked_list(linked_node *first=NULL);
89  void add_front(class linked_node *p);
90  void add_end(class linked_node *p);
91  void insert(class linked_node *p);
92  void set_sort_field(int x) {sortby=x;}   // this is passed to compare
93  class linked_node *current() {return cn;}
94  class linked_node *first() {return fn;}
95  class linked_node *last() {return fn->last();}
96  class linked_node *get_node(int x);
97  void set_current(class linked_node *p) {cn=p;}
98  void go_first() {cn=fn;}
99  void go_end() {cn=fn->last();}
100  void go_next() {cn=cn->next();}
101  void go_last() {cn=cn->last();}
102  int number_nodes()  {return nn;}
103  int node_number(linked_node *p);
104  int unlink(linked_node *p);
105  ~linked_list();
106};
107
108#endif
109
110
111
Note: See TracBrowser for help on using the repository browser.