Make the tag (for items in an idlist) a member of the templated

data structure, and thus get rid of a level of struct.

[git-p4: depot-paths = "//depot/solvespace/": change = 1671]
solver
Jonathan Westhues 2008-04-17 23:21:17 -08:00
parent 2f4a3917c5
commit c097fea4f3
5 changed files with 55 additions and 49 deletions

20
dsc.h
View File

@ -43,12 +43,7 @@ public:
template <class T, class H> template <class T, class H>
class IdList { class IdList {
public: public:
typedef struct { T *elem;
T t;
int tag;
} Elem;
Elem *elem;
int elems; int elems;
int elemsAllocated; int elemsAllocated;
@ -57,7 +52,7 @@ public:
DWORD id = 0; DWORD id = 0;
for(i = 0; i < elems; i++) { for(i = 0; i < elems; i++) {
id = max(id, elem[i].t.h.v); id = max(id, elem[i].h.v);
} }
t->h.v = (id + 1); t->h.v = (id + 1);
@ -69,12 +64,11 @@ public:
void Add(T *t) { void Add(T *t) {
if(elems >= elemsAllocated) { if(elems >= elemsAllocated) {
elemsAllocated = (elemsAllocated + 32)*2; elemsAllocated = (elemsAllocated + 32)*2;
elem = (Elem *)MemRealloc(elem, elemsAllocated*sizeof(elem[0])); elem = (T *)MemRealloc(elem, elemsAllocated*sizeof(elem[0]));
if(!elem) oops(); if(!elem) oops();
} }
elem[elems].t = *t; elem[elems] = *t;
elem[elems].tag = 0;
elems++; elems++;
} }
@ -90,8 +84,8 @@ public:
T *FindByIdNoOops(H h) { T *FindByIdNoOops(H h) {
int i; int i;
for(i = 0; i < elems; i++) { for(i = 0; i < elems; i++) {
if(elem[i].t.h.v == h.v) { if(elem[i].h.v == h.v) {
return &(elem[i].t); return &(elem[i]);
} }
} }
return NULL; return NULL;
@ -107,7 +101,7 @@ public:
void Tag(H h, int tag) { void Tag(H h, int tag) {
int i; int i;
for(i = 0; i < elems; i++) { for(i = 0; i < elems; i++) {
if(elem[i].t.h.v == h.v) { if(elem[i].h.v == h.v) {
elem[i].tag = tag; elem[i].tag = tag;
} }
} }

View File

@ -157,12 +157,12 @@ void GraphicsWindow::EnsureValidActives(void) {
if((!g) || (g->h.v == Group::HGROUP_REFERENCES.v)) { if((!g) || (g->h.v == Group::HGROUP_REFERENCES.v)) {
int i; int i;
for(i = 0; i < SS.group.elems; i++) { for(i = 0; i < SS.group.elems; i++) {
if(SS.group.elem[i].t.h.v != Group::HGROUP_REFERENCES.v) { if(SS.group.elem[i].h.v != Group::HGROUP_REFERENCES.v) {
break; break;
} }
} }
if(i >= SS.group.elems) oops(); if(i >= SS.group.elems) oops();
activeGroup = SS.group.elem[i].t.h; activeGroup = SS.group.elem[i].h;
change = true; change = true;
} }
@ -363,28 +363,28 @@ void GraphicsWindow::HitTestMakeSelection(Point2d mp, Selection *dest) {
// Do the points // Do the points
for(i = 0; i < SS.entity.elems; i++) { for(i = 0; i < SS.entity.elems; i++) {
d = SS.entity.elem[i].t.GetDistance(mp); d = SS.entity.elem[i].GetDistance(mp);
if(d < 10 && d < dmin) { if(d < 10 && d < dmin) {
memset(dest, 0, sizeof(*dest)); memset(dest, 0, sizeof(*dest));
dest->entity = SS.entity.elem[i].t.h; dest->entity = SS.entity.elem[i].h;
} }
} }
// Entities // Entities
for(i = 0; i < SS.point.elems; i++) { for(i = 0; i < SS.point.elems; i++) {
d = SS.point.elem[i].t.GetDistance(mp); d = SS.point.elem[i].GetDistance(mp);
if(d < 10 && d < dmin) { if(d < 10 && d < dmin) {
memset(dest, 0, sizeof(*dest)); memset(dest, 0, sizeof(*dest));
dest->point = SS.point.elem[i].t.h; dest->point = SS.point.elem[i].h;
} }
} }
// Constraints // Constraints
for(i = 0; i < SS.constraint.elems; i++) { for(i = 0; i < SS.constraint.elems; i++) {
d = SS.constraint.elem[i].t.GetDistance(mp); d = SS.constraint.elem[i].GetDistance(mp);
if(d < 10 && d < dmin) { if(d < 10 && d < dmin) {
memset(dest, 0, sizeof(*dest)); memset(dest, 0, sizeof(*dest));
dest->constraint = SS.constraint.elem[i].t.h; dest->constraint = SS.constraint.elem[i].h;
} }
} }
} }
@ -582,15 +582,15 @@ void GraphicsWindow::Paint(int w, int h) {
// First, draw the entire scene. // First, draw the entire scene.
glColor3f(1, 1, 1); glColor3f(1, 1, 1);
for(i = 0; i < SS.entity.elems; i++) { for(i = 0; i < SS.entity.elems; i++) {
SS.entity.elem[i].t.Draw(); SS.entity.elem[i].Draw();
} }
glColor3f(0, 0.8f, 0); glColor3f(0, 0.8f, 0);
for(i = 0; i < SS.point.elems; i++) { for(i = 0; i < SS.point.elems; i++) {
SS.point.elem[i].t.Draw(); SS.point.elem[i].Draw();
} }
glColor3f(1.0f, 0, 1.0f); glColor3f(1.0f, 0, 1.0f);
for(i = 0; i < SS.constraint.elems; i++) { for(i = 0; i < SS.constraint.elems; i++) {
SS.constraint.elem[i].t.Draw(); SS.constraint.elem[i].Draw();
} }
// Then redraw whatever the mouse is hovering over, highlighted. Have // Then redraw whatever the mouse is hovering over, highlighted. Have

View File

@ -61,6 +61,7 @@ class Group {
public: public:
static const hGroup HGROUP_REFERENCES; static const hGroup HGROUP_REFERENCES;
int tag;
hGroup h; hGroup h;
NameStr name; NameStr name;
@ -77,6 +78,7 @@ public:
static const hRequest HREQUEST_REFERENCE_YZ; static const hRequest HREQUEST_REFERENCE_YZ;
static const hRequest HREQUEST_REFERENCE_ZX; static const hRequest HREQUEST_REFERENCE_ZX;
int tag;
hRequest h; hRequest h;
// Types of requests // Types of requests
@ -112,6 +114,7 @@ public:
static const int LINE_SEGMENT = 1010; static const int LINE_SEGMENT = 1010;
int type; int type;
int tag;
hEntity h; hEntity h;
Expr *expr[16]; Expr *expr[16];
@ -140,7 +143,9 @@ public:
class Param { class Param {
public: public:
int tag;
hParam h; hParam h;
double val; double val;
bool known; bool known;
@ -149,6 +154,7 @@ public:
class Point { class Point {
public: public:
int tag;
// The point ID is equal to the initial param ID. // The point ID is equal to the initial param ID.
hPoint h; hPoint h;
@ -176,6 +182,26 @@ public:
double GetDistance(Point2d mp); double GetDistance(Point2d mp);
}; };
inline hEntity hRequest::entity(int i)
{ hEntity r; r.v = (v << 10) | i; return r; }
inline hRequest hEntity::request(void)
{ hRequest r; r.v = (v >> 10); return r; }
inline hParam hEntity::param(int i)
{ hParam r; r.v = (v << 7) | i; return r; }
inline hPoint hEntity::point(int i)
{ hPoint r; r.v = (v << 7) | i; return r; }
inline bool hPoint::isFromReferences(void) {
DWORD d = v >> 17;
if(d == Request::HREQUEST_REFERENCE_XY.v) return true;
if(d == Request::HREQUEST_REFERENCE_YZ.v) return true;
if(d == Request::HREQUEST_REFERENCE_ZX.v) return true;
return false;
}
class hConstraint { class hConstraint {
public: public:
DWORD v; DWORD v;
@ -191,7 +217,9 @@ public:
static const int HORIZONTAL = 40; static const int HORIZONTAL = 40;
static const int VERTICAL = 41; static const int VERTICAL = 41;
int tag;
hConstraint h; hConstraint h;
int type; int type;
hGroup group; hGroup group;
@ -235,27 +263,11 @@ public:
class Equation { class Equation {
public: public:
int tag;
hEquation h; hEquation h;
Expr *e; Expr *e;
}; };
inline hEntity hRequest::entity(int i)
{ hEntity r; r.v = (v << 10) | i; return r; }
inline hRequest hEntity::request(void)
{ hRequest r; r.v = (v >> 10); return r; }
inline hParam hEntity::param(int i)
{ hParam r; r.v = (v << 7) | i; return r; }
inline hPoint hEntity::point(int i)
{ hPoint r; r.v = (v << 7) | i; return r; }
inline bool hPoint::isFromReferences(void) {
DWORD d = v >> 17;
if(d == Request::HREQUEST_REFERENCE_XY.v) return true;
if(d == Request::HREQUEST_REFERENCE_YZ.v) return true;
if(d == Request::HREQUEST_REFERENCE_ZX.v) return true;
return false;
}
#endif #endif

View File

@ -59,13 +59,13 @@ void SolveSpace::GenerateAll(void) {
entity.Clear(); entity.Clear();
point.Clear(); point.Clear();
for(i = 0; i < request.elems; i++) { for(i = 0; i < request.elems; i++) {
request.elem[i].t.Generate(&entity, &point, &param); request.elem[i].Generate(&entity, &point, &param);
} }
for(i = 0; i < param.elems; i++) { for(i = 0; i < param.elems; i++) {
Param *p = prev.FindByIdNoOops(param.elem[i].t.h); Param *p = prev.FindByIdNoOops(param.elem[i].h);
if(p) { if(p) {
param.elem[i].t.val = p->val; param.elem[i].val = p->val;
} }
} }

View File

@ -261,7 +261,7 @@ void TextWindow::ShowAllGroups(void) {
s = "all requests from all groups"; s = "all requests from all groups";
v = 0; v = 0;
} else { } else {
Group *g = &(SS.group.elem[i].t); Group *g = &(SS.group.elem[i]);
s = g->DescriptionString(); s = g->DescriptionString();
v = g->h.v; v = g->h.v;
} }
@ -294,7 +294,7 @@ void TextWindow::ShowRequestsInGroup(void) {
int i; int i;
for(i = 0; i < SS.request.elems; i++) { for(i = 0; i < SS.request.elems; i++) {
Request *r = &(SS.request.elem[i].t); Request *r = &(SS.request.elem[i]);
if(r->group.v == shown->group.v || shown->group.v == 0) { if(r->group.v == shown->group.v || shown->group.v == 0) {
char *s = r->DescriptionString(); char *s = r->DescriptionString();