source: abuse/trunk/src/imlib/linked.h @ 494

Last change on this file since 494 was 494, checked in by Sam Hocevar, 12 years ago

style: remove trailing spaces, fix copyright statements.

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