-
Notifications
You must be signed in to change notification settings - Fork 9
/
deltablue.c
1126 lines (948 loc) · 24.8 KB
/
deltablue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This is an implementation of the DeltaBlue incremental dataflow
constraint solver written in portable C.
The original version was by John Maloney.
This version was modified for portability and benchmarking
by Mario Wolczko, Sun Microsystems Labs, 2 Oct 96.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef enum {false, true} Boolean;
typedef void (*Proc)();
typedef void * Element;
/*
List: Supports variable sized, ordered lists of elements. */
typedef struct {
Element *slots; /* variable-sized array of element slots */
int slotCount; /* number of slots currently allocated */
int first; /* index of first element */
int last; /* index of last element (first-1, if empty) */
} *List, ListStruct;
/*
Constraint, variable, and strength data definitions for DeltaBlue.
*/
/* Strength Constants */
typedef enum {
S_required= 0,
S_strongPreferred= 1,
S_preferred= 2,
S_strongDefault= 3,
S_default= 4,
S_weakDefault= 5,
S_weakest= 6
} Strength;
struct constraint;
typedef struct {
long value;
List constraints;
struct constraint* determinedBy;
long mark;
Strength walkStrength;
Boolean stay;
char name[10];
} *Variable, VariableStruct;
typedef struct constraint {
Proc execute;
Boolean inputFlag;
Strength strength;
char whichMethod;
char methodCount;
char varCount;
char methodOuts[7];
Variable variables[1];
} *Constraint, ConstraintStruct;
/* Other Constants and Macros */
#define NO_METHOD (-1)
#define SATISFIED(c) ((c)->whichMethod != NO_METHOD)
#define Weaker(a,b) (a > b)
/*
Implementation of List
Invariants and relationships:
slots != NULL
slotCount > 0
sizeof(*slots) == slotCount * sizeof(Element)
0 <= first < slotCount
-1 <= last < slotCount
last >= first (if not empty)
last == first - 1 (if empty)
NumberOfItems == (last - first) + 1
*/
/* Private Prototypes */
void Error(char*);
void Grow(List);
void MakeRoom(List);
char* StrengthString(Strength strength);
/* Variables */
Variable Variable_Create(char *, long);
Variable Variable_CreateConstant(char *, long);
void Variable_Destroy(Variable);
void Variable_Print(Variable);
/* Constraints */
Constraint Constraint_Create(int, Strength);
void Constraint_Destroy(Constraint);
void Constraint_Print(Constraint);
/* Miscellaneous */
void ExecutePlan(List);
Constraint StayC(Variable v, Strength); /* keep v constant */
Constraint EditC(Variable v, Strength); /* change v */
Constraint EqualsC(Variable a, Variable b, Strength); /* a = b */
/* (src * scale) + offset = dest*/
Constraint ScaleOffsetC(Variable src, Variable scale, Variable offset,
Variable dest, Strength);
void InitDeltaBlue(void);
void AddVariable(Variable);
void DestroyVariable(Variable);
void AddConstraint(Constraint);
void DestroyConstraint(Constraint);
List ExtractPlan(void);
List ExtractPlanFromConstraint(Constraint);
List ExtractPlanFromConstraints(List);
/****** Create and Destruction ******/
List List_Create(int initialCount)
{
List newList;
newList = (List) malloc(sizeof(ListStruct));
if (newList == NULL) Error("out of memory");
newList->slots = (Element *) malloc(initialCount * sizeof(Element));
if (newList->slots == NULL) Error("out of memory");
newList->slotCount = initialCount;
newList->first = 0;
newList->last = -1;
return newList;
}
void List_Destroy(List list)
{
if (list->slots == NULL) Error("bad ListStruct; already freed?");
free(list->slots);
list->slots = NULL;
list->slotCount = 0;
list->first = 0;
list->last = -1;
free(list);
}
/****** Enumeration and Queries ******/
void List_Do(List list, Proc proc)
{
Element *nextPtr = &(list->slots[list->first]);
Element *lastPtr = &(list->slots[list->last]);
while (nextPtr <= lastPtr) {
(*proc)(*nextPtr++);
}
}
int List_Size(List list)
{
return (list->last - list->first) + 1;
}
void* List_At(List list, int index)
{
if (index < 0 || index > list->last - list->first + 1)
Error("List access out of bounds");
return list->slots[index + list->first];
}
/****** Adding ******/
void List_Add(List list, Element element)
{
if (list->last >= (list->slotCount - 1)) MakeRoom(list);
list->slots[++list->last] = element;
}
void List_Append(List list1, List list2)
{
Element *nextPtr = &(list2->slots[list2->first]);
Element *lastPtr = &(list2->slots[list2->last]);
while (nextPtr <= lastPtr) {
List_Add(list1, *nextPtr++);
}
}
/****** Removing ******/
void List_Remove(List list, Element element)
{
Element *srcPtr = &list->slots[list->first];
Element *destPtr = &list->slots[0];
Element *lastPtr = &list->slots[list->last];
list->last = list->last - list->first;
list->first = 0;
while (srcPtr <= lastPtr) {
if (*srcPtr == element) {
list->last--;
} else {
*destPtr++ = *srcPtr;
}
srcPtr++;
}
}
Element List_RemoveFirst(List list)
{
Element element;
if (list->last < list->first) return NULL;
element = list->slots[list->first++];
return element;
}
void List_RemoveAll(List list)
{
list->first = 0;
list->last = -1;
}
/****** Private ******/
#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))
void Error(char *errorString)
{
printf("error: %s.\n", errorString);
exit(-1);
}
void Grow(List list)
{
list->slotCount += min(max(list->slotCount, 2), 512);
list->slots = realloc(list->slots, (list->slotCount * sizeof(Element)));
if (list->slots == NULL) Error("out of memory");
}
void MakeRoom(List list)
{
Element *srcPtr = &list->slots[list->first];
Element *destPtr = &list->slots[0];
Element *lastPtr = &list->slots[list->last];
if (((list->last - list->first) + 1) >= list->slotCount) Grow(list);
if (list->first == 0) return;
while (srcPtr <= lastPtr) {
*destPtr++ = *srcPtr++;
}
list->last = list->last - list->first;
list->first = 0;
}
/*
Constraint, variable, and other operations for DeltaBlue.
*/
/******* Private *******/
void Execute(Constraint c)
{
c->execute(c);
}
void Noop(Constraint c)
{
/* default execute procedure; does nothing */
};
/******* Variables *******/
Variable Variable_Create(char *name, long initialValue)
{
Variable new;
new = (Variable) malloc(sizeof(VariableStruct));
if (new == NULL) Error("out of memory");
new->value = initialValue;
new->constraints = List_Create(2);
new->determinedBy = NULL;
new->mark = 0;
new->walkStrength = S_weakest;
new->stay = true;
strncpy(new->name, name, 10);
new->name[9] = 0;
AddVariable(new);
return new;
}
Variable Variable_CreateConstant(char *name, long value)
{
Variable new;
new = (Variable) malloc(sizeof(VariableStruct));
if (new == NULL) Error("out of memory");
new->value = value;
new->constraints = List_Create(0);
new->determinedBy = NULL;
new->mark = 0;
new->walkStrength = S_required;
new->stay = true;
strncpy(new->name, name, 10);
new->name[9] = 0;
AddVariable(new);
return new;
}
void Variable_Destroy(Variable v)
{
if (v->constraints == NULL) {
Error("bad VariableStruct; already freed?");
}
List_Destroy(v->constraints);
v->constraints = NULL;
free(v);
}
void Variable_Print(Variable v)
{
printf(
"%s(%s,%ld)",
v->name, StrengthString(v->walkStrength), v->value);
}
/******* Constraints *******/
Constraint Constraint_Create(int variableCount, Strength strength)
{
Constraint new;
int i;
new = (Constraint) malloc(sizeof(ConstraintStruct)
+ ((variableCount - 1) * sizeof(Variable)));
if (new == NULL) Error("out of memory");
new->execute = Noop;
new->inputFlag = false;
new->strength = strength;
new->whichMethod = NO_METHOD;
new->methodCount = 0;
for (i = 0; i < 7; i++) {
new->methodOuts[i] = 0;
}
new->varCount = variableCount;
for (i = 0; i < new->varCount; i++) {
new->variables[i] = NULL;
}
return new;
}
void Constraint_Destroy(Constraint c)
{
if (c->execute == NULL) {
Error("bad ConstraintStruct; already freed?");
}
c->execute = NULL;
free(c);
}
void Constraint_Print(Constraint c)
{
int i, outIndex;
if (!SATISFIED(c)) {
printf("Unsatisfied(");
for (i = 0; i < c->varCount; i++) {
Variable_Print(c->variables[i]);
printf(" ");
}
printf(")");
} else {
outIndex = c->methodOuts[c->whichMethod];
printf("Satisfied(");
for (i = 0; i < c->varCount; i++) {
if (i != outIndex) {
Variable_Print(c->variables[i]);
printf(" ");
}
}
printf("-> ");
Variable_Print(c->variables[outIndex]);
printf(")");
}
printf("\n");
}
/******* Miscellaneous Functions *******/
char* StrengthString(Strength strength)
{
static char temp[20];
switch (strength) {
case S_required: return "required";
case S_strongPreferred: return "strongPreferred";
case S_preferred: return "preferred";
case S_strongDefault: return "strongDefault";
case S_default: return "default";
case S_weakDefault: return "weakDefault";
case S_weakest: return "weakest";
default:
sprintf(temp, "strength[%d]", strength);
return temp;
}
}
void ExecutePlan(List list)
{
List_Do(list, Execute);
}
/*
DeltaBlue, an incremental dataflow constraint solver.
*/
/******* Private Macros and Prototypes *******/
#define OUT_VAR(c) (c->variables[c->methodOuts[c->whichMethod]])
void FreeVariable(Variable);
void AddIfSatisfiedInput(Constraint);
void CollectSatisfiedInputs(Variable);
List MakePlan(void);
void IncrementalAdd(Constraint);
void AddAtStrength(Constraint);
void IncrementalRemove(Constraint);
Boolean AddPropagate(Constraint);
void CollectUnsatisfied(Constraint);
void RemovePropagateFrom(Variable);
Constraint Satisfy(Constraint);
int ChooseMethod(Constraint);
void Recalculate(Constraint);
int OutputWalkStrength(Constraint);
Boolean ConstantOutput(Constraint);
Boolean InputsKnown(Constraint);
void NewMark(void);
void Error(char *);
Constraint NextDownstreamConstraint(List, Variable);
/******* DeltaBlue Globals *******/
List allVariables = NULL;
long currentMark = 0;
/******** Public: Initialization *******/
void InitDeltaBlue(void)
{
Variable v;
if (allVariables == NULL) allVariables = List_Create(128);
v = (Variable) List_RemoveFirst(allVariables);
while (v != NULL) {
FreeVariable(v);
v = (Variable) List_RemoveFirst(allVariables);
}
List_RemoveAll(allVariables);
currentMark = 0;
}
/* this is used when we know we are going to throw away all variables */
void FreeVariable(Variable v)
{
Constraint c;
int i;
c = (Constraint) List_RemoveFirst(v->constraints);
while (c != NULL) {
for (i = c->varCount - 1; i >= 0; i--) {
List_Remove((c->variables[i])->constraints, (Element) c);
}
Constraint_Destroy(c);
c = (Constraint) List_RemoveFirst(v->constraints);
}
Variable_Destroy(v);
}
/******** Public: Variables and Constraints *******/
void AddVariable(Variable v)
{
List_Add(allVariables, v);
}
void DestroyConstraint(Constraint c);
void DestroyVariable(Variable v)
{
Constraint c;
c = (Constraint) List_RemoveFirst(v->constraints);
while (c != NULL) {
DestroyConstraint(c);
c = (Constraint) List_RemoveFirst(v->constraints);
}
List_Remove(allVariables, v);
Variable_Destroy(v);
}
void AddConstraint(Constraint c)
{
int i;
for (i = c->varCount - 1; i >= 0; i--) {
List_Add((c->variables[i])->constraints, (Element) c);
}
c->whichMethod = NO_METHOD;
IncrementalAdd(c);
}
void DestroyConstraint(Constraint c)
{
int i;
if (SATISFIED(c)) IncrementalRemove(c);
for (i = c->varCount - 1; i >= 0; i--) {
List_Remove((c->variables[i])->constraints, (Element) c);
}
Constraint_Destroy(c);
}
/******** Public: Plan Extraction *******/
List hot = NULL; /* used to collect "hot" constraints */
void AddIfSatisfiedInput(Constraint c)
{
if (c->inputFlag && SATISFIED(c)) {
List_Add(hot, c);
}
}
void CollectSatisfiedInputs(Variable v)
{
List_Do(v->constraints, AddIfSatisfiedInput);
}
List ExtractPlan(void)
{
if (hot == NULL) hot = List_Create(128);
List_RemoveAll(hot);
List_Do(allVariables, CollectSatisfiedInputs);
return MakePlan();
}
List ExtractPlanFromConstraint(Constraint c)
{
if (hot == NULL) hot = List_Create(128);
List_RemoveAll(hot);
AddIfSatisfiedInput(c);
return MakePlan();
}
List ExtractPlanFromConstraints(List constraints)
{
if (hot == NULL) hot = List_Create(128);
List_RemoveAll(hot);
List_Do(constraints, AddIfSatisfiedInput);
return MakePlan();
}
/******* Private: Plan Extraction *******/
List MakePlan()
{
List plan;
Constraint nextC;
Variable out;
NewMark();
plan = List_Create(128);
nextC = (Constraint) List_RemoveFirst(hot);
while (nextC != NULL) {
out = OUT_VAR(nextC);
if ((out->mark != currentMark) && InputsKnown(nextC)) {
List_Add(plan, nextC);
out->mark = currentMark;
nextC = NextDownstreamConstraint(hot, out);
} else {
nextC = (Constraint) List_RemoveFirst(hot);
}
}
return plan;
}
Boolean InputsKnown(Constraint c)
{
int outIndex, i;
Variable in;
outIndex = c->methodOuts[c->whichMethod];
for (i = c->varCount - 1; i >= 0; i--) {
if (i != outIndex) {
in = c->variables[i];
if ((in->mark != currentMark) &&
(!in->stay) &&
(in->determinedBy != NULL)) {
return false;
}
}
}
return true;
}
/******* Private: Adding *******/
void IncrementalAdd(Constraint c)
{
Constraint overridden;
NewMark();
overridden = Satisfy(c);
while (overridden != NULL) {
overridden = Satisfy(overridden);
}
}
Constraint Satisfy(Constraint c)
{
int outIndex, i;
Constraint overridden;
Variable out;
c->whichMethod = ChooseMethod(c);
if (SATISFIED(c)) {
/* mark inputs to allow cycle detection in AddPropagate */
outIndex = c->methodOuts[c->whichMethod];
for (i = c->varCount - 1; i >= 0; i--) {
if (i != outIndex) {
c->variables[i]->mark = currentMark;
}
}
out = c->variables[outIndex];
overridden = (Constraint) out->determinedBy;
if (overridden != NULL) overridden->whichMethod = NO_METHOD;
out->determinedBy = c;
if (!AddPropagate(c)) {
Error("Cycle encountered");
return NULL;
}
out->mark = currentMark;
return overridden;
} else {
if (c->strength == S_required) {
Error("Could not satisfy a required constraint");
}
return NULL;
}
}
int ChooseMethod(Constraint c)
{
int best, m;
Strength bestOutStrength;
Variable mOut;
best = NO_METHOD;
bestOutStrength = c->strength;
for (m = c->methodCount - 1; m >= 0; m--) {
mOut = c->variables[c->methodOuts[m]];
if ((mOut->mark != currentMark) &&
(Weaker(mOut->walkStrength, bestOutStrength))) {
best = m;
bestOutStrength = mOut->walkStrength;
}
}
return best;
}
Boolean AddPropagate(Constraint c)
{
List todo;
Constraint nextC;
Variable out;
todo = List_Create(8); /* unprocessed constraints */
nextC = c;
while (nextC != NULL) {
out = OUT_VAR(nextC);
if (out->mark == currentMark) {
/* remove the cycle-causing constraint */
IncrementalRemove(c);
return false;
}
Recalculate(nextC);
nextC = NextDownstreamConstraint(todo, out);
}
List_Destroy(todo);
return true;
}
/******* Private: Removing *******/
List unsatisfied; /* used to collect unsatisfied downstream constraints */
Strength strength; /* used to add unsatisfied constraints in strength order */
void AddAtStrength(Constraint c)
{
if (c->strength == strength) IncrementalAdd(c);
}
void CollectUnsatisfied(Constraint c)
{
if (!SATISFIED(c)) List_Add(unsatisfied, c);
}
void IncrementalRemove(Constraint c)
{
Variable out;
int i;
out = OUT_VAR(c);
c->whichMethod = NO_METHOD;
for (i = c->varCount - 1; i >= 0; i--) {
List_Remove((c->variables[i])->constraints, (Element) c);
}
unsatisfied = List_Create(8);
RemovePropagateFrom(out);
for (strength = S_required; strength <= S_weakest; strength++) {
List_Do(unsatisfied, AddAtStrength);
}
List_Destroy(unsatisfied);
}
void RemovePropagateFrom(Variable v)
{
Constraint nextC;
List todo;
v->determinedBy = NULL;
v->walkStrength = S_weakest;
v->stay = true;
todo = List_Create(8);
while (true) {
List_Do(v->constraints, CollectUnsatisfied);
nextC = NextDownstreamConstraint(todo, v);
if (nextC == NULL) {
break;
} else {
Recalculate(nextC);
v = OUT_VAR(nextC);
}
}
List_Destroy(todo);
}
/******* Private: Recalculation *******/
void Recalculate(Constraint c)
{
Variable out;
out = OUT_VAR(c);
out->walkStrength = OutputWalkStrength(c);
out->stay = ConstantOutput(c);
if (out->stay) c->execute(c);
}
int OutputWalkStrength(Constraint c)
{
int outIndex, m, mOutIndex;
Strength minStrength;
minStrength = c->strength;
outIndex = c->methodOuts[c->whichMethod];
for (m = c->methodCount - 1; m >= 0; m--) {
mOutIndex = c->methodOuts[m];
if ((mOutIndex != outIndex) &&
(Weaker(c->variables[mOutIndex]->walkStrength, minStrength))) {
minStrength = c->variables[mOutIndex]->walkStrength;
}
}
return minStrength;
}
Boolean ConstantOutput(Constraint c)
{
int outIndex, i;
if (c->inputFlag) return false;
outIndex = c->methodOuts[c->whichMethod];
for (i = c->varCount - 1; i >= 0; i--) {
if (i != outIndex) {
if (!c->variables[i]->stay) return false;
}
}
return true;
}
/******* Private: Miscellaneous *******/
void NewMark(void)
{
currentMark++;
}
Constraint NextDownstreamConstraint(List todo, Variable variable)
{
List allC = variable->constraints;
Constraint *nextPtr = (Constraint *) &(allC->slots[allC->first]);
Constraint *lastPtr = (Constraint *) &(allC->slots[allC->last]);
Constraint determiningC = variable->determinedBy;
Constraint first = NULL;
for ( ; nextPtr <= lastPtr; nextPtr++) {
if ((*nextPtr != determiningC) && SATISFIED(*nextPtr)) {
if (first == NULL) {
first = *nextPtr;
} else {
List_Add(todo, *nextPtr);
}
}
}
if (first == NULL) {
first = (Constraint) List_RemoveFirst(todo);
}
return first;
}
/*
Some useful constraints. Each function instantiates and installs
a constraint on the argument variables.
*/
/* macro to reference a constraint variable value */
#define var(i) ((c->variables[i])->value)
/******* Stay Constraint *******/
Constraint StayC(Variable v, Strength strength)
{
Constraint new = Constraint_Create(1, strength);
new->variables[0] = v;
new->methodCount = 1;
new->methodOuts[0] = 0;
AddConstraint(new);
return new;
};
/******* Edit Constraint *******/
Constraint EditC(Variable v, Strength strength)
{
Constraint new = Constraint_Create(1, strength);
new->inputFlag = true;
new->variables[0] = v;
new->methodCount = 1;
new->methodOuts[0] = 0;
AddConstraint(new);
return new;
};
/****** Equals Constraint ******/
void EqualsC_Execute(Constraint c)
{
/* a = b */
switch (c->whichMethod) {
case 0:
var(0) = var(1);
break;
case 1:
var(1) = var(0);
break;
}
}
Constraint EqualsC(Variable a, Variable b, Strength strength)
{
Constraint new = Constraint_Create(2, strength);
new->execute = EqualsC_Execute;
new->variables[0] = a;
new->variables[1] = b;
new->methodCount = 2;
new->methodOuts[0] = 0;
new->methodOuts[1] = 1;
AddConstraint(new);
return new;
};
/******** Add Constraint *******/
void AddC_Execute(Constraint c)
{
/* a + b = sum */
switch (c->whichMethod) {
case 0:
var(2) = var(0) + var(1);
break;
case 1:
var(1) = var(2) - var(0);
break;
case 2:
var(0) = var(2) - var(1);
break;
}
}
Constraint AddC(Variable a, Variable b, Variable sum, Strength strength)
{
Constraint new = Constraint_Create(3, strength);
new->execute = AddC_Execute;
new->variables[0] = a;
new->variables[1] = b;
new->variables[2] = sum;
new->methodCount = 3;
new->methodOuts[0] = 2;
new->methodOuts[1] = 1;
new->methodOuts[2] = 0;
AddConstraint(new);
return new;
};
/******** ScaleOffset Constraint *******/
void ScaleOffsetC_Execute(Constraint c)
{
/* (src * scale) + offset = dest */
switch (c->whichMethod) {
case 0:
var(3) = (var(0) * var(1)) + var(2);
break;
case 1:
var(0) = (var(3) - var(2)) / var(1);
break;
}
}
Constraint ScaleOffsetC(Variable src, Variable scale, Variable offset,
Variable dest, Strength strength)
{
Constraint new = Constraint_Create(4, strength);
new->execute = ScaleOffsetC_Execute;
new->variables[0] = src;
new->variables[1] = scale;
new->variables[2] = offset;
new->variables[3] = dest;
new->methodCount = 2;
new->methodOuts[0] = 3;
new->methodOuts[1] = 0;
AddConstraint(new);
return new;
};
/***************************************************************************
Timing Functions
****************************************************************************/
long startTime;
long Milliseconds()
{
int millisecondsPerClock = CLOCKS_PER_SEC / 1000;
return (clock() / millisecondsPerClock);
}
void Start()
{
startTime = Milliseconds();
}
void Finish(long *milliseconds)
{
*milliseconds = Milliseconds() - startTime;
}
/***************************************************************************
*
* This is the standard DeltaBlue benchmark. A long chain of equality
* constraints is constructed with a stay constraint on one end. An edit
* constraint is then added to the opposite end and the time is measured for
* adding and removing this constraint, and extracting and executing a
* constraint satisfaction plan. There are two cases. In case 1, the added
* constraint is stronger than the stay constraint and values must propagate
* down the entire length of the chain. In case 2, the added constraint is
* weaker than the stay constraint so it cannot be accomodated. The cost in
* this case is, of course, very low. Typical situations lie somewhere between
* these two extremes.
*
****************************************************************************/
void ChainTest(int n)
{
long msecs, i;
char name[20];
Variable prev, v, first, last;
Constraint editC;
List plan;
InitDeltaBlue();