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

Last change on this file since 519 was 519, checked in by Sam Hocevar, 11 years ago

core: fix a few useless casts.

  • Property svn:keywords set to Id
File size: 3.9 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]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
[481]11// linked.h  - linked list and linked list node classes
[2]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)
[494]18//        { return ((classname *) n1)->data > data); }
[2]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
[124]24#ifndef linkman
[2]25#define linkman
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
[519]30#define loop(controll,first,inside) { controll=first; \
31  if (first) do { inside controll=controll->next(); } \
32  while (controll!=first); }
[2]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
[519]39#define loop_rev(controll,last,inside) { controll=last; \
40  if (first) do { inside controll=controll->last(); } \
41  while (controll!=last); }
[2]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); \
[494]51          (!x || (controll)!=(type *)(first)); \
[2]52          controll=(type *)(controll->next()),x++)
53
54#define loop_forct(type,controll,first,cond,x) int x=0; if (first) for \
[494]55  (controll=(type *)(first); cond && (!x || controll!=(type *)(first)); \
[2]56  controll=(type *)(controll->next()),x++)
57
58class linked_node
59{
60  class linked_node *nextp, *lastp;
61public:
[494]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; }
[2]67  virtual ~linked_node() { ; }
68  linked_node() { nextp=NULL; lastp=NULL; }
69};
70
71// this is the basic class for all linked_list
[112]72// its features should be self-explanatory.
[2]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
[519]75// for example shape is an class derived from linked_node. to add a shape to
76// linked list I have to say mylist.add_end(myshape_pointer);
[2]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);
[494]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(); }
[2]96  class linked_node *get_node(int x);
[494]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; }
[2]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.