Some style cleanups by Fredrik Ekdahl. Thanks!

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22013 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini
2007-08-19 20:14:48 +00:00
parent 0e5fe45f13
commit be98a6028d
12 changed files with 426 additions and 355 deletions
+13 -13
View File
@@ -18,20 +18,20 @@ void
FPS::drawChar(GLfloat x, GLfloat y, GLint number) FPS::drawChar(GLfloat x, GLfloat y, GLint number)
{ {
static bool numbers[13][7] = { static bool numbers[13][7] = {
{ true, true, true, true, true, true, false }, /* 0 */ {true, true, true, true, true, true, false}, /* 0 */
{ false, true, true, false, false, false, false }, /* 1 */ {false, true, true, false, false, false, false},/* 1 */
{ true, true, false, true, true, false, true }, /* 2 */ {true, true, false, true, true, false, true}, /* 2 */
{ true, true, true, true, false, false, true }, /* 3 */ {true, true, true, true, false, false, true}, /* 3 */
{ false, true, true, false, false, true, true }, /* 4 */ {false, true, true, false, false, true, true}, /* 4 */
{ true, false, true, true, false, true, true }, /* 5 */ {true, false, true, true, false, true, true}, /* 5 */
{ true, false, true, true, true, true, true }, /* 6 */ {true, false, true, true, true, true, true}, /* 6 */
{ true, true, true, false, false, false, false }, /* 7 */ {true, true, true, false, false, false, false}, /* 7 */
{ true, true, true, true, true, true, true }, /* 8 */ {true, true, true, true, true, true, true}, /* 8 */
{ true, true, true, false, false, true, true }, /* 9 */ {true, true, true, false, false, true, true}, /* 9 */
{ true, false, false, false, true, true, true }, /* F */ {true, false, false, false, true, true, true}, /* F */
{ true, true, false, false, true, true, true }, /* P */ {true, true, false, false, true, true, true}, /* P */
{ true, false, true, true, false, true, true }, /* S */ {true, false, true, true, false, true, true}, /* S */
}; };
static GLfloat gap = 0.03; static GLfloat gap = 0.03;
+8 -10
View File
@@ -5,16 +5,14 @@
#include <GL/gl.h> #include <GL/gl.h>
class FPS class FPS {
{ public:
public: FPS();
FPS(); ~FPS();
~FPS();
static void drawCounter(GLfloat frameRate);
static void drawCounter( GLfloat frameRate ); private:
static void drawChar(GLfloat x, GLfloat y, GLint number);
private:
static void drawChar( GLfloat x, GLfloat y, GLint number );
}; };
+35 -23
View File
@@ -3,22 +3,17 @@
This file may be used under the terms of the Be Sample Code License. This file may be used under the terms of the Be Sample Code License.
*/ */
#include <stdlib.h>
#include <GL/gl.h>
#include <InterfaceKit.h>
#include "GLObject.h" #include "GLObject.h"
#include <GL/gl.h>
#include <stdlib.h>
#include <InterfaceKit.h>
#include "glob.h" #include "glob.h"
struct material { struct material {
float ambient[3],diffuse[3],specular[3]; float ambient[3], diffuse[3], specular[3];
}; };
float *colors[] = float *colors[] = {NULL, white, yellow, blue, red, green};
{
NULL,white,yellow,blue,red,green
};
material materials[] = { material materials[] = {
// Null // Null
@@ -61,25 +56,34 @@ material materials[] = {
#define USE_QUAD_STRIPS 1 #define USE_QUAD_STRIPS 1
extern long setEvent(sem_id event); extern long setEvent(sem_id event);
GLObject::GLObject(ObjectView *ov) GLObject::GLObject(ObjectView *ov)
: rotX(0),
rotY(0),
lastRotX(0),
lastRotY(0),
spinX(2),
spinY(2),
x(0),
y(0),
z(-2.0),
color(4),
solidity(0),
changed(false),
objView(ov)
{ {
rotX = rotY = lastRotX = lastRotY = 0;
spinX = spinY = 2;
x = y = 0;
z = -2.0;
color = 4;
solidity = 0;
changed = false;
objView = ov;
}; };
GLObject::~GLObject() GLObject::~GLObject()
{ {
}; };
void GLObject::MenuInvoked(BPoint point)
void
GLObject::MenuInvoked(BPoint point)
{ {
BPopUpMenu *m = new BPopUpMenu("Object",false,false); BPopUpMenu *m = new BPopUpMenu("Object",false,false);
BMenuItem *i; BMenuItem *i;
@@ -126,7 +130,9 @@ void GLObject::MenuInvoked(BPoint point)
setEvent(objView->drawEvent); setEvent(objView->drawEvent);
}; };
bool GLObject::SpinIt()
bool
GLObject::SpinIt()
{ {
rotX += spinX; rotX += spinX;
rotY += spinY; rotY += spinY;
@@ -138,7 +144,9 @@ bool GLObject::SpinIt()
return c; return c;
}; };
void GLObject::Draw(bool forID, float IDcolor[])
void
GLObject::Draw(bool forID, float IDcolor[])
{ {
glPushMatrix(); glPushMatrix();
glTranslatef(x, y, z); glTranslatef(x, y, z);
@@ -156,6 +164,7 @@ void GLObject::Draw(bool forID, float IDcolor[])
changed = false; changed = false;
}; };
TriangleObject::TriangleObject(ObjectView *ov, char *filename) TriangleObject::TriangleObject(ObjectView *ov, char *filename)
: GLObject(ov), : GLObject(ov),
points(100,100), points(100,100),
@@ -247,6 +256,7 @@ TriangleObject::TriangleObject(ObjectView *ov, char *filename)
fclose(f); fclose(f);
}; };
TriangleObject::~TriangleObject() TriangleObject::~TriangleObject()
{ {
for (int i=0;i<qs.num_items;i++) { for (int i=0;i<qs.num_items;i++) {
@@ -254,7 +264,9 @@ TriangleObject::~TriangleObject()
}; };
}; };
void TriangleObject::DoDrawing(bool forID)
void
TriangleObject::DoDrawing(bool forID)
{ {
if (!forID) { if (!forID) {
float c[3][4]; float c[3][4];
+26 -23
View File
@@ -2,8 +2,11 @@
Copyright 1999, Be Incorporated. All Rights Reserved. Copyright 1999, Be Incorporated. All Rights Reserved.
This file may be used under the terms of the Be Sample Code License. This file may be used under the terms of the Be Sample Code License.
*/ */
#ifndef GL_OBJECT_H
#define GL_OBJECT_H
#include "ObjectView.h" #include "ObjectView.h"
#include "util.h"
struct point { struct point {
float x,y,z; float x,y,z;
@@ -20,35 +23,35 @@ struct quadStrip {
int *pts; int *pts;
}; };
#include "util.h"
class GLObject { class GLObject {
public: public:
float rotX,rotY,spinX,spinY,lastRotX,lastRotY; GLObject(ObjectView *ov);
float x,y,z; virtual ~GLObject();
int color;
int solidity;
bool changed;
ObjectView * objView;
GLObject(ObjectView *ov); virtual void Draw(bool forID, float IDcolor[]);
virtual ~GLObject(); virtual bool SpinIt();
virtual void MenuInvoked(BPoint point);
virtual void DoDrawing(bool forID) {};
virtual void Draw(bool forID, float IDcolor[]); float rotX,rotY,spinX,spinY,lastRotX,lastRotY;
virtual bool SpinIt(); float x,y,z;
virtual void MenuInvoked(BPoint point); int color;
int solidity;
virtual void DoDrawing(bool forID) {}; bool changed;
ObjectView * objView;
}; };
class TriangleObject : public GLObject { class TriangleObject : public GLObject {
public: public:
BufferArray<point> points; TriangleObject(ObjectView *ov, char *filename);
BufferArray<tri> triangles; virtual ~TriangleObject();
BufferArray<quadStrip> qs;
virtual void DoDrawing(bool forID);
TriangleObject(ObjectView *ov, char *filename); BufferArray<point> points;
virtual ~TriangleObject(); BufferArray<tri> triangles;
BufferArray<quadStrip> qs;
virtual void DoDrawing(bool forID);
}; };
#endif // GL_OBJECT_H
+58 -24
View File
@@ -3,16 +3,17 @@
This file may be used under the terms of the Be Sample Code License. This file may be used under the terms of the Be Sample Code License.
*/ */
#include "ObjectView.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <InterfaceKit.h> #include <InterfaceKit.h>
#include <FindDirectory.h> #include <FindDirectory.h>
#include "ObjectView.h"
#include "ResScroll.h"
#include "GLObject.h"
#include "FPS.h" #include "FPS.h"
#include "GLObject.h"
#include "ResScroll.h"
#define teapotData "teapot.data" #define teapotData "teapot.data"
char teapotPath[PATH_MAX]; char teapotPath[PATH_MAX];
@@ -35,22 +36,26 @@ float red[3] = {1.0,0.0,0.0};
float *bgColor = black; float *bgColor = black;
struct light { struct light {
float *ambient; float *ambient;
float *diffuse; float *diffuse;
float *specular; float *specular;
}; };
light lights[] = { light lights[] = {
{NULL,NULL,NULL}, {NULL, NULL, NULL},
{dimWhite,white,white}, {dimWhite, white, white},
{dimWhite,yellow,yellow}, {dimWhite, yellow, yellow},
{dimWhite,red,red}, {dimWhite, red, red},
{dimWhite,blue,blue}, {dimWhite, blue, blue},
{dimWhite,green,green} {dimWhite, green, green}
}; };
long signalEvent(sem_id event)
long
signalEvent(sem_id event)
{ {
long c; long c;
get_sem_count(event,&c); get_sem_count(event,&c);
@@ -60,7 +65,9 @@ long signalEvent(sem_id event)
return 0; return 0;
}; };
long setEvent(sem_id event)
long
setEvent(sem_id event)
{ {
long c; long c;
get_sem_count(event,&c); get_sem_count(event,&c);
@@ -71,7 +78,8 @@ long setEvent(sem_id event)
}; };
long waitEvent(sem_id event) long
waitEvent(sem_id event)
{ {
acquire_sem(event); acquire_sem(event);
@@ -83,7 +91,9 @@ long waitEvent(sem_id event)
return 0; return 0;
}; };
long simonThread(ObjectView *ov)
long
simonThread(ObjectView *ov)
{ {
int noPause=0; int noPause=0;
while (acquire_sem_etc(ov->quittingSem,1,B_TIMEOUT,0) == B_NO_ERROR) { while (acquire_sem_etc(ov->quittingSem,1,B_TIMEOUT,0) == B_NO_ERROR) {
@@ -101,6 +111,7 @@ long simonThread(ObjectView *ov)
return 0; return 0;
}; };
ObjectView::ObjectView(BRect r, char *name, ulong resizingMode, ulong options) ObjectView::ObjectView(BRect r, char *name, ulong resizingMode, ulong options)
: BGLView(r,name,resizingMode,0,options) : BGLView(r,name,resizingMode,0,options)
{ {
@@ -130,13 +141,16 @@ ObjectView::ObjectView(BRect r, char *name, ulong resizingMode, ulong options)
objListLock.Unlock(); objListLock.Unlock();
}; };
ObjectView::~ObjectView() ObjectView::~ObjectView()
{ {
delete_sem(quittingSem); delete_sem(quittingSem);
delete_sem(drawEvent); delete_sem(drawEvent);
}; };
void ObjectView::AttachedToWindow()
void
ObjectView::AttachedToWindow()
{ {
float position[] = {0.0, 3.0, 3.0, 0.0}; float position[] = {0.0, 3.0, 3.0, 0.0};
float position1[] = {-3.0, -3.0, 3.0, 0.0}; float position1[] = {-3.0, -3.0, 3.0, 0.0};
@@ -204,7 +218,9 @@ void ObjectView::AttachedToWindow()
setEvent(drawEvent); setEvent(drawEvent);
}; };
void ObjectView::DetachedFromWindow()
void
ObjectView::DetachedFromWindow()
{ {
BGLView::DetachedFromWindow(); BGLView::DetachedFromWindow();
@@ -224,7 +240,9 @@ void ObjectView::DetachedFromWindow()
while (locks--) Window()->Lock(); while (locks--) Window()->Lock();
}; };
void ObjectView::Pulse()
void
ObjectView::Pulse()
{ {
Window()->Lock(); Window()->Lock();
BRect p = Parent()->Bounds(); BRect p = Parent()->Bounds();
@@ -237,7 +255,9 @@ void ObjectView::Pulse()
Window()->Unlock(); Window()->Unlock();
}; };
void ObjectView::MessageReceived(BMessage *msg)
void
ObjectView::MessageReceived(BMessage *msg)
{ {
BMenuItem *i; BMenuItem *i;
bool *b; bool *b;
@@ -320,7 +340,9 @@ void ObjectView::MessageReceived(BMessage *msg)
}; };
}; };
int ObjectView::ObjectAtPoint(BPoint p)
int
ObjectView::ObjectAtPoint(BPoint p)
{ {
LockGL(); LockGL();
glShadeModel(GL_FLAT); glShadeModel(GL_FLAT);
@@ -347,7 +369,9 @@ int ObjectView::ObjectAtPoint(BPoint p)
return objNum; return objNum;
}; };
void ObjectView::MouseDown(BPoint p)
void
ObjectView::MouseDown(BPoint p)
{ {
BPoint op=p,np=p; BPoint op=p,np=p;
BRect bounds = Bounds(); BRect bounds = Bounds();
@@ -441,7 +465,9 @@ void ObjectView::MouseDown(BPoint p)
setEvent(drawEvent); setEvent(drawEvent);
}; };
void ObjectView::FrameResized(float w, float h)
void
ObjectView::FrameResized(float w, float h)
{ {
LockGL(); LockGL();
@@ -481,7 +507,9 @@ void ObjectView::FrameResized(float w, float h)
setEvent(drawEvent); setEvent(drawEvent);
} }
bool ObjectView::RepositionView()
bool
ObjectView::RepositionView()
{ {
if (!(persp != lastPersp) && if (!(persp != lastPersp) &&
!(lastObjectDistance != objectDistance) && !(lastObjectDistance != objectDistance) &&
@@ -516,7 +544,9 @@ bool ObjectView::RepositionView()
return true; return true;
}; };
void ObjectView::EnforceState()
void
ObjectView::EnforceState()
{ {
glShadeModel(gouraud?GL_SMOOTH:GL_FLAT); glShadeModel(gouraud?GL_SMOOTH:GL_FLAT);
if (zbuf) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); if (zbuf) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
@@ -542,7 +572,9 @@ void ObjectView::EnforceState()
}; };
}; };
bool ObjectView::SpinIt()
bool
ObjectView::SpinIt()
{ {
bool changed = false; bool changed = false;
@@ -615,7 +647,9 @@ bool ObjectView::SpinIt()
return changed; return changed;
}; };
void ObjectView::DrawFrame(bool noPause)
void
ObjectView::DrawFrame(bool noPause)
{ {
LockGL(); LockGL();
glClear(GL_COLOR_BUFFER_BIT|(zbuf?GL_DEPTH_BUFFER_BIT:0)); glClear(GL_COLOR_BUFFER_BIT|(zbuf?GL_DEPTH_BUFFER_BIT:0));
+42 -40
View File
@@ -3,8 +3,8 @@
This file may be used under the terms of the Be Sample Code License. This file may be used under the terms of the Be Sample Code License.
*/ */
#ifndef ObjectView_h #ifndef OBJECT_VIEW_H
#define ObjectView_h #define OBJECT_VIEW_H
#include <GL/glu.h> #include <GL/glu.h>
#include <GLView.h> #include <GLView.h>
@@ -33,47 +33,49 @@ enum lights {
#define HISTSIZE 10 #define HISTSIZE 10
class ResScroll; class ResScroll;
class ObjectView : public BGLView { class ObjectView : public BGLView {
public: public:
sem_id drawEvent; ObjectView(BRect r, char *name,
sem_id quittingSem; ulong resizingMode, ulong options);
thread_id drawThread; ~ObjectView();
ResScroll * resScroll;
BList objects; virtual void MouseDown(BPoint p);
BLocker objListLock; virtual void MessageReceived(BMessage *msg);
virtual void AttachedToWindow();
virtual void DetachedFromWindow();
virtual void FrameResized(float width, float height);
bool SpinIt();
int ObjectAtPoint(BPoint p);
virtual void DrawFrame(bool noPause);
virtual void Pulse();
void EnforceState();
bool RepositionView();
float yxRatio,lastYXRatio; sem_id drawEvent;
uint64 lastFrame; sem_id quittingSem;
float fpsHistory[HISTSIZE]; thread_id drawThread;
int32 histEntries,oldestEntry; ResScroll * resScroll;
bool fps;
bool gouraud, lastGouraud; BList objects;
bool zbuf,lastZbuf; BLocker objListLock;
bool culling,lastCulling;
bool lighting,lastLighting; float yxRatio,lastYXRatio;
bool filled,lastFilled; uint64 lastFrame;
bool persp,lastPersp; float fpsHistory[HISTSIZE];
bool lastTextured,textured; int32 histEntries,oldestEntry;
bool lastFog,fog; bool fps;
bool forceRedraw; bool gouraud, lastGouraud;
float objectDistance,lastObjectDistance; bool zbuf,lastZbuf;
bool culling,lastCulling;
bool lighting,lastLighting;
bool filled,lastFilled;
bool persp,lastPersp;
bool lastTextured,textured;
bool lastFog,fog;
bool forceRedraw;
float objectDistance,lastObjectDistance;
ObjectView(BRect r, char *name,
ulong resizingMode, ulong options);
~ObjectView();
virtual void MouseDown(BPoint p);
virtual void MessageReceived(BMessage *msg);
virtual void AttachedToWindow();
virtual void DetachedFromWindow();
virtual void FrameResized(float width, float height);
bool SpinIt();
int ObjectAtPoint(BPoint p);
virtual void DrawFrame(bool noPause);
virtual void Pulse();
void EnforceState();
bool RepositionView();
}; };
#endif #endif // OBJECT_VIEW_H
+8 -9
View File
@@ -3,21 +3,20 @@
This file may be used under the terms of the Be Sample Code License. This file may be used under the terms of the Be Sample Code License.
*/ */
#ifndef ResScroll_h #ifndef RESSCROLL_H
#define ResScroll_h #define RESSCROLL_H
#include <interface/ScrollBar.h> #include <interface/ScrollBar.h>
class ObjectView; class ObjectView;
class ResScroll : public BScrollBar { class ResScroll : public BScrollBar {
public: public:
ObjectView * objectView; ObjectView * objectView;
ResScroll(BRect r, const char *name, ResScroll(BRect r, const char *name,
ObjectView *target, orientation posture); ObjectView *target, orientation posture);
virtual void ValueChanged(float value); virtual void ValueChanged(float value);
}; };
#endif #endif
+2 -1
View File
@@ -5,7 +5,8 @@
#include "error.h" #include "error.h"
void fatalerror(char *s) void
fatalerror(char *s)
{ {
printf("FATAL ERROR: %s\n",s); printf("FATAL ERROR: %s\n",s);
}; };
+3 -3
View File
@@ -3,8 +3,8 @@
This file may be used under the terms of the Be Sample Code License. This file may be used under the terms of the Be Sample Code License.
*/ */
#ifndef error_h #ifndef ERROR_H
#define error_h #define ERROR_H
#include <stdio.h> #include <stdio.h>
@@ -28,4 +28,4 @@ extern void fatalerror(char *);
#endif //DEBUGGING #endif //DEBUGGING
#endif #endif // ERROR_H
+1 -1
View File
@@ -3,7 +3,7 @@
This file may be used under the terms of the Be Sample Code License. This file may be used under the terms of the Be Sample Code License.
*/ */
#include "GL/gl.h" #include <GL/gl.h>
void auxSolidTeapot(GLdouble scale); void auxSolidTeapot(GLdouble scale);
void auxWireTeapot(GLdouble scale); void auxWireTeapot(GLdouble scale);
+20 -14
View File
@@ -3,42 +3,48 @@
This file may be used under the terms of the Be Sample Code License. This file may be used under the terms of the Be Sample Code License.
*/ */
#include <InterfaceKit.h>
#include <stdio.h> #include <stdio.h>
#include <InterfaceKit.h>
#include "ObjectView.h" #include "ObjectView.h"
class QuitWindow : public BDirectWindow class QuitWindow : public BDirectWindow {
{ public:
public: QuitWindow(BRect r, char *name, window_type wt, ulong something);
QuitWindow(BRect r, char *name, window_type wt, ulong something);
virtual bool QuitRequested();
virtual bool QuitRequested(); virtual void DirectConnected( direct_buffer_info *info );
virtual void DirectConnected( direct_buffer_info *info );
ObjectView *bgl;
ObjectView *bgl;
}; };
QuitWindow::QuitWindow(BRect r, char *name, window_type wt, ulong something) QuitWindow::QuitWindow(BRect r, char *name, window_type wt, ulong something)
: BDirectWindow(r,name,wt,something) : BDirectWindow(r,name,wt,something)
{ {
} }
bool QuitWindow::QuitRequested()
bool
QuitWindow::QuitRequested()
{ {
printf( "closing \n" ); printf("closing \n");
bgl->EnableDirectMode( false ); bgl->EnableDirectMode( false );
be_app->PostMessage(B_QUIT_REQUESTED); be_app->PostMessage(B_QUIT_REQUESTED);
return true; return true;
}; };
void QuitWindow::DirectConnected( direct_buffer_info *info )
void
QuitWindow::DirectConnected(direct_buffer_info *info)
{ {
if( bgl ) if (bgl)
bgl->DirectConnected( info ); bgl->DirectConnected( info );
bgl->EnableDirectMode( true ); bgl->EnableDirectMode( true );
} }
int main(int argc, char **argv)
int
main(int argc, char **argv)
{ {
GLenum type = BGL_RGB | BGL_DEPTH | BGL_DOUBLE; GLenum type = BGL_RGB | BGL_DEPTH | BGL_DOUBLE;
//GLenum type = BGL_RGB | BGL_DEPTH | BGL_SINGLE; //GLenum type = BGL_RGB | BGL_DEPTH | BGL_SINGLE;
+210 -194
View File
@@ -3,15 +3,14 @@
This file may be used under the terms of the Be Sample Code License. This file may be used under the terms of the Be Sample Code License.
*/ */
#ifndef list_h #ifndef UTIL_H
#define list_h #define UTIL_H
#include <memory.h> #include <memory.h>
#include "error.h" #include "error.h"
template <class contents> template <class contents>
struct LispNode struct LispNode {
{
contents *car; contents *car;
LispNode *cdr; LispNode *cdr;
@@ -31,89 +30,100 @@ struct LispNode
template <class contents> template <class contents>
struct LispList struct LispList {
{ LispNode<contents> *first;
LispNode<contents> *first;
/* -------- List creation --------------- */ /* -------- List creation --------------- */
/* Create an empty list */ /* Create an empty list */
inline LispList() inline LispList()
{ first = 0; } {
first = 0;
}
/* Create a list pointing to the specified node */ /* Create a list pointing to the specified node */
inline LispList(LispNode<contents> *_first) inline LispList(LispNode<contents> *_first)
{ first = _first; } {
first = _first;
}
/* ?? */ /* ?? */
inline LispList(LispList &init) inline LispList(LispList &init)
{ first = init.first; } {
first = init.first;
}
/* ---------- List queries ------------- */ /* ---------- List queries ------------- */
inline int is_empty() inline int is_empty()
{ return first == 0; } {
return first == 0;
}
/* Determines if a thing is on the list */ /* Determines if a thing is on the list */
inline int is_present(contents *element) inline int is_present(contents *element)
{ {
for (LispNode<contents> *node = first; node; node = node->cdr) for (LispNode<contents> *node = first; node; node = node->cdr)
if (node->car == element) if (node->car == element)
return 1; return 1;
return 0; return 0;
} }
/* Returns the length of the list */ /* Returns the length of the list */
inline int count() inline int count()
{ {
int n = 0; int n = 0;
for (LispNode<contents> *node = first; node; node = node->cdr) for (LispNode<contents> *node = first; node; node = node->cdr)
n++; n++;
return n; return n;
} }
/* ----------- Adding "nodes" to the list ------------ */ /* ----------- Adding "nodes" to the list ------------ */
/* Add a specified node to the head of the list. */ /* Add a specified node to the head of the list. */
inline void add_head(LispNode<contents> *new_element) inline void add_head(LispNode<contents> *new_element)
{ {
new_element->cdr = first; new_element->cdr = first;
first = new_element; first = new_element;
} }
/* Add a specified node anywhere on the list */ /* Add a specified node anywhere on the list */
inline void add(LispNode<contents> *new_element) inline void add(LispNode<contents> *new_element)
{ {
add_head (new_element); add_head (new_element);
} }
inline void add_tail(LispNode<contents> *new_element) inline void add_tail(LispNode<contents> *new_element)
{ {
LispNode<contents>** pred = &first; LispNode<contents>** pred = &first;
while( *pred ) while(*pred)
pred = &(*pred)->cdr; pred = &(*pred)->cdr;
*pred = new_element; *pred = new_element;
new_element->cdr = 0; new_element->cdr = 0;
} }
/* ----------- Adding "contents" to the list ------------ */ /* ----------- Adding "contents" to the list ------------ */
/* ----- (Which in my opinion is far more useful) ------ */ /* ----- (Which in my opinion is far more useful) ------ */
/* Create new node pointing to thing, & add to head of list. */ /* Create new node pointing to thing, & add to head of list. */
inline void add_head_new(contents *new_element) inline void add_head_new(contents *new_element)
{ {
first = new LispNode<contents>(new_element, first); first = new LispNode<contents>(new_element, first);
} }
inline void add_head(contents *new_element) inline void add_head(contents *new_element)
{ add_head_new(new_element); } {
add_head_new(new_element);
}
/* Create new node pointing to thing, & add to end of list. */ /* Create new node pointing to thing, & add to end of list. */
inline void add_tail_new(contents *new_element) inline void add_tail_new(contents *new_element)
{ {
LispNode< contents > **pred = &first; LispNode< contents > **pred = &first;
while (*pred) while (*pred)
pred = &(*pred)->cdr; pred = &(*pred)->cdr;
*pred = new LispNode< contents >(new_element); *pred = new LispNode< contents >(new_element);
} }
inline void add_tail(contents *new_element) inline void add_tail(contents *new_element)
{ add_tail_new(new_element); } {
add_tail_new(new_element);
}
/* Create new node pointing to thing, & add anywhere on list */ /* Create new node pointing to thing, & add anywhere on list */
inline void add_new(contents *new_element) inline void add_new(contents *new_element)
@@ -226,156 +236,162 @@ struct LispList
} }
#endif #endif
/* Take the first thing off the list */ /* Take the first thing off the list */
inline contents *get_head() inline contents *get_head()
{ {
contents *c = 0; contents *c = 0;
if(first) if(first) {
{ c = first->car;
c = first->car; delete rem_head();
delete rem_head(); }
} return c;
return c; }
}
/* Take something off the list */
/* Take something off the list */ inline contents *get()
inline contents *get() {
{ return(get_head()); } return(get_head());
}
/* XXX inline contents *get_tail() { } */ /* XXX inline contents *get_tail() { } */
/* -------- Stack simulation (by j.h.) ------------ */ /* -------- Stack simulation (by j.h.) ------------ */
/* Put a thing onto the head of the list */ /* Put a thing onto the head of the list */
inline void push( contents *c ) inline void push(contents *c)
{ put_head( c ); } {
put_head(c);
}
/* Remove a thing from the head of the list */ /* Remove a thing from the head of the list */
inline contents *pop() inline contents *pop()
{ return(get_head()); } {
return(get_head());
}
/* Pop everything off the stack. Empty the stack/list. */ /* Pop everything off the stack. Empty the stack/list. */
inline void pop_all() inline void pop_all()
{ rem_del_all(); } {
rem_del_all();
}
/* ----------- list/list manipulations ------------ */ /* ----------- list/list manipulations ------------ */
/* Add all elements present on another list to this list also. */ /* Add all elements present on another list to this list also. */
inline void add_new(LispList other) inline void add_new(LispList other)
{ {
for (LispNode<contents> *n = other.first; n; n = n->cdr) for (LispNode<contents> *n = other.first; n; n = n->cdr)
add_new(n->car); add_new(n->car);
} }
inline void add_new_once(LispList other) inline void add_new_once(LispList other)
{ {
for (LispNode<contents> *n = other.first; n; n = n->cdr) for (LispNode<contents> *n = other.first; n; n = n->cdr)
add_new_once(n->car); add_new_once(n->car);
} }
/* Remove and delete all nodes whose contents are also present /* Remove and delete all nodes whose contents are also present
in a different list (set disjunction). */ in a different list (set disjunction). */
inline void rem_del(LispList other) inline void rem_del(LispList other)
{ {
for (LispNode<contents> *n = other.first; n; n = n->cdr) for (LispNode<contents> *n = other.first; n; n = n->cdr)
rem_del(n->car); rem_del(n->car);
} }
}; };
template <class thetype> template <class thetype>
struct DoubleLinkedNode struct DoubleLinkedNode
{ {
thetype *next,*prev; thetype *next,*prev;
DoubleLinkedNode() { next = prev = NULL; }; DoubleLinkedNode() { next = prev = NULL; };
void insert_after(thetype *n) void insert_after(thetype *n)
{ {
if (next != NULL) if (next != NULL)
next->prev = n; next->prev = n;
n->next = next; n->next = next;
n->prev = (thetype*)this; n->prev = (thetype*)this;
next = n; next = n;
}; };
void insert_before(thetype *n) void insert_before(thetype *n)
{ {
prev->next = n; prev->next = n;
n->next = (thetype*)this; n->next = (thetype*)this;
n->prev = prev; n->prev = prev;
prev = n; prev = n;
}; };
void remove() void remove()
{ {
assert(prev != NULL); assert(prev != NULL);
prev->next = next; prev->next = next;
if (next != NULL) if (next != NULL)
next->prev = prev; next->prev = prev;
}; };
}; };
template <class thetype> template <class thetype>
struct DoubleLinkedList : public DoubleLinkedNode<thetype> struct DoubleLinkedList : public DoubleLinkedNode<thetype>
{ {
DoubleLinkedList() : DoubleLinkedNode<thetype>() {}; DoubleLinkedList() : DoubleLinkedNode<thetype>() {};
void insert(thetype *n) void insert(thetype *n)
{ {
insert_after(n); insert_after(n);
}; };
void add(thetype *n) void add(thetype *n)
{ {
insert_after(n); insert_after(n);
}; };
}; };
template <class T> template <class T>
struct BufferArray struct BufferArray {
{ T * items;
T * items; int num_items;
int num_items; int num_slots;
int num_slots; int slot_inc;
int slot_inc;
void resize(int i)
{
items = (T*)realloc(items,sizeof(T)*i);
num_slots = i;
};
T & operator [](int index)
{
assert(index < num_items);
return items[index];
};
T & get(int index) void resize(int i)
{ {
assert(index < num_items); items = (T*)realloc(items,sizeof(T)*i);
return items[index]; num_slots = i;
}; };
void add(T &item)
{
if (num_items == num_slots)
resize(num_slots+slot_inc);
memcpy(items+num_items,&item,sizeof(item));
num_items++;
};
BufferArray(int start_slots, int _slot_inc)
{
num_slots = start_slots;
slot_inc = _slot_inc;
assert(slot_inc > 0);
num_items = 0;
items = (T*)malloc(sizeof(T)*num_slots);
};
~BufferArray() T & operator [](int index)
{ {
free(items); assert(index < num_items);
}; return items[index];
};
T & get(int index)
{
assert(index < num_items);
return items[index];
};
void add(T &item)
{
if (num_items == num_slots)
resize(num_slots+slot_inc);
memcpy(items+num_items,&item,sizeof(item));
num_items++;
};
BufferArray(int start_slots, int _slot_inc)
{
num_slots = start_slots;
slot_inc = _slot_inc;
assert(slot_inc > 0);
num_items = 0;
items = (T*)malloc(sizeof(T)*num_slots);
};
~BufferArray()
{
free(items);
};
}; };
#endif #endif // UTIL_H