summaryrefslogtreecommitdiff
path: root/lib/sbin/Orderer.pl
blob: 54a8277b5edd2a66c484f9349fb2ce2ba991a188 (plain)
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
#! /usr/bin/perl -s
#/*@@
#  @file      Orderer.pl
#  @date      Wed Feb 10 08:55:51 1999
#  @author    Tom Goodale
#  @desc 
#  Routines to order a set of things which have specified before and after.
#  @enddesc 
#@@*/

#/*@@
#  @routine    TestOrderList
#  @date       Sun Feb 21 08:22:42 1999
#  @author     Tom Goodale
#  @desc 
#  Routine to test the OrderList function
#  @enddesc 
#  @calls     
#  @calledby   
#  @history 
#
#  @endhistory 
#
#@@*/

sub TestOrderList
{
  local(%data);
  local($sorted_things);

  # Create a list of data and rules to sort them.
  %data = ("thorns", "c b a d e f",
	   "A BEFORE", "f",
	   "B BEFORE", "c",
	   "B AFTER",  "a",
	   "C BEFORE", "d",
	   "D BEFORE",  "e",
	   "E BEFORE", "f",
	   "E AFTER",  "a",
	   "F AFTER",  "b",
	  );

  # Find the sorted list
  @sorted_things = &OrderList("Error", "thorns", %data);

  # Report
  print join(",", @sorted_things);
  print "\n";

}


#/*@@
#  @routine    OrderList
#  @date       Sun Feb 21 07:52:43 1999
#  @author     Tom Goodale
#  @desc 
#  Orders a set of strings given info in a database
#  There should be records with names like \U<thing> BEFORE and
#  \U<thing> AFTER specifying any ordering between the strings.
#  @enddesc 
#  @calls     
#  @calledby   
#  @var     error_string
#  @vdesc   The string to be printed before any error messages.
#  @vtype   string
#  @vcomment 
#
#  @endvar
#  @var     fieldname
#  @vdesc   The name of the field containing the names of the strings to be sorted.
#  @vtype   string
#  @vcomment 
#
#  @endvar 
#  @var     database
#  @vdesc   The database.
#  @vtype   hash table
#  @vcomment 
#
#  @endvar 

#  @history 
#
#  @endhistory 
#@@*/

sub OrderList
{
  local($error_string, $fieldname, %database) = @_;
  local(@things);
  local($thing, $other_thing);
  local($nerrors);
  local(@thing_list); 

  $nerrors = 0;
  @things = split(" ", $database{$fieldname});

  # Make complete first level lists of before and after.
  foreach $thing (@things)
  {
    if($database{"\U$thing BEFORE"})
    {
      foreach $other_thing (split(" ", $database{"\U$thing BEFORE"}))
      {
	$database{"\U$other_thing ALLAFTER"} .= " $thing";
	$database{"\U$thing ALLBEFORE"}      .= " $other_thing";
      }
    }
    if($database{"\U$thing AFTER"})
    {
      foreach $other_thing (split(" ", $database{"\U$thing AFTER"}))
      {
	$database{"\U$other_thing ALLBEFORE"} .= " $thing";
	$database{"\U$thing ALLAFTER"}        .= " $other_thing";
      }
    }
  }

  # Now go through the list and find the complete before and after lists.
  foreach $thing (@things)
  {
    %complete = &RecurseThings($thing, "ALLBEFORE", 0, %database);

    $database{"\U$thing ALLBEFORE"} = join(" ", keys %complete);

    %complete = &RecurseThings($thing, "ALLAFTER", 0, %database);

    $database{"\U$thing ALLAFTER"} = join(" ", keys %complete);

  }

  # Check that something doesn't appear on its own lists !
  foreach $thing (@things)
  {
    $nerrors += &CheckThings($error_string, $thing, "ALLBEFORE", %database);

    $nerrors += &CheckThings($error_string, $thing, "ALLAFTER", %database);
  }

  # Stop if there have been any errors.
#  if($nerrors)
#  {
#    print "$error_string: $nerrors errors detected\n";
#    exit;
#  }

#  foreach $field ( sort keys %database )
#  {
#    print "$field has value $database{$field}\n";
#  }

  # Finally, sort the strings.
  @thing_list = &SortThings("ALLBEFORE", "ALLAFTER", scalar(@things), @things, %database);

#  foreach $thing (@thing_list)
#  {
#    print "thing is $thing\n";
#  }

  return @thing_list;
}


#/*@@
#  @routine    RecurseThings
#  @date       Sun Feb 21 08:01:55 1999
#  @author     Tom Goodale
#  @desc 
#  Recurses through a database, constructing the full list of before
#  and after properties for a particular string.
#  @enddesc 
#  @calls     
#  @calledby 
#  @var     thing
#  @vdesc   The string to construct data for
#  @vtype   string
#  @vcomment 
#
#  @endvar 
#  @var     keyword
#  @vdesc   The keyword in the database used to find the relationship
#  @vtype   string
#  @vcomment 
#
#  @endvar 
#  @var     nthings
#  @vdesc   The number of things which have been found so far.
#  @vtype   integer
#  @vcomment 
#
#  @endvar 
#  @var     indata
#  @vdesc   The rest of the arguments
#  @vtype   array
#  @vcomment 
#  This consists of two hash tables - one containing the things found so
#  far, and one containing the database with the relations between the 
#  strings.
#  @endvar 

#  @history 
#
#  @endhistory 
#
#@@*/
sub RecurseThings
{
  local($thing, $keyword, $nthings, @indata) = @_;
  local(%things);
  local(%database);

  # Extract the hash tables
  if($nthings > 0)
  {
    %things = @indata[0..2*$nthings-1];
    %database = @indata[2*$nthings..$#indata];
  }
  else
  {
    %things = ();
    %database = @indata;
  }

  # Recurse
  if($database{"\U$thing $keyword"})
  {
    foreach $other_thing (split(" ", $database{"\U$thing $keyword"}))
    {
      if(! $things{"\U$other_thing\E"})
      {
	$things{"\U$other_thing\E"} = 1;
	%things = &RecurseThings($other_thing, $keyword, scalar(keys %things), %things,%database);
      }
    }
  }

  return %things;

}


#/*@@
#  @routine    CheckThings
#  @date       Sun Feb 21 08:08:28 1999
#  @author     Tom Goodale
#  @desc 
#  Checks that something doesn't appear on its own ordering list.
#  @enddesc 
#  @calls     
#  @calledby   
#  @history 
#
#  @endhistory 
#  @var     error_string
#  @vdesc   The string to be printed before any error messages.
#  @vtype   string
#  @vcomment 
#
#  @endvar
#  @var     thing
#  @vdesc   The string to check the data for
#  @vtype   string
#  @vcomment 
#
#  @endvar 
#  @var     keyword
#  @vdesc   The keyword in the database to be checked
#  @vtype   string
#  @vcomment 
#
#  @endvar 
#  @var     database
#  @vdesc   The database of relation data
#  @vtype   hash table
#  @vcomment 
#
#  @endvar 
#
#@@*/
sub CheckThings
{
  local($error_string, $thing, $keyword, %database) = @_;
  local($other_thing);
  local($nerrors);

  if($database{"\U$thing $keyword"})
  {
    foreach $other_thing (split(" ", $database{"\U$thing $keyword"}))
    {
      
      if( $thing =~ m:^$other_thing$:i)
      {
	$message = "$error_string:  $thing appears in its own $keyword list";
	&CST_error(0,$message,__LINE__,__FILE__);
	$nerrors++;
      }
    }
  }

  return $nerrors;
}

#/*@@
#  @routine    SortThings
#  @date       Sun Feb 21 08:11:49 1999
#  @author     Tom Goodale
#  @desc 
#  Sorts a set of strings given data in a database
#  @enddesc 
#  @calls     
#  @calledby   
#  @history 
#
#  @endhistory 
#  @var     before
#  @vdesc   The keyword for finding strings before which a particular string should appear.
#  @vtype   string
#  @vcomment 
#
#  @endvar 
#  @var     before
#  @vdesc   The keyword for finding strings after which a particular string should appear.
#  @vtype   string
#  @vcomment 
#
#  @endvar 
#  @var     n_things
#  @vdesc   The number of things in the list to be sorted.
#  @vtype   integer
#  @vcomment 
#
#  @endvar 
#  @var     rest
#  @vdesc   The rest of the arguments.
#  @vtype   hash
#  @vcomment 
#  This consists of an array containing the things to be sorted and
#  a hash table containing the relation data.
#  @endvar 
#
#@@*/
sub SortThings
{
  local($before, $after, $n_things, @rest) = @_;
  local(@things);
  local(%database);
  local(@sorted_things);

  # Extract the list of things and the database
  if($n_things)
  {
    @things   = @rest[0..$n_things-1];
    %database = @rest[$n_things..$#rest];
  }
  else
  {
    return;
  }

  # Remove anything which doesn't need sorting
  $sortcount=0;
  $returncount=0;
  for ($i=0;$i<scalar(@things);$i++)
  {
      if ($database{"\U$things[$i] AFTER"} || $database{"\U$things[$i] ALLAFTER"} || 
	  $database{"\U$things[$i] BEFORE"} || $database{"\U$things[$i] ALLBEFORE"})
      {
	  $sortthings[$sortcount] = $things[$i];
	  $sortcount++;
      }
      else
      {
	  $returnthings[$returncount] = $things[$i];
	  $returncount++
      }
  }


  # Sort the things
  @sorted_things = sort ThingSorter @sortthings;
  return (@returnthings,@sorted_things);

}


#/*@@
#  @routine    ThingSorter
#  @date       Sun Feb 21 08:17:08 1999
#  @author     Tom Goodale
#  @desc 
#  A customised sort routine to sort strings based upon details
#  stored in a database.
#
#  The database should be called %database, and should contain
#  entries of the form 
#     \U<string> $before   and
#     \U<string> $after
#  which are lists of strings before or after which <string> should
#  appear.
#  $before, $after and %database need to be provided, and be in scope.
#  @enddesc 
#  @calls     
#  @calledby   
#  @history 
#
#  @endhistory 
#
#@@*/
sub ThingSorter
{
  local($retval);
  if($database{"\U$a $before"} =~ m:\b$b\b:i)
  {
#    print "$b in $a $before list - " . $database{"\U$a $before"} . "\n";
    $retval = -1;
  }
  elsif($database{"\U$b $after"} =~ m:\b$a\b:i)
  {
#    print "$a in $b $after list - " . $database{"\U$b $after"} . "\n";
    $retval = 1;
  }
  elsif($database{"\U$a $after"} =~ m:\b$b\b:i)
  {
#    print "$b in $a $after list - " . $database{"\U$a $after"} . "\n";
    $retval = 1;
  }
  elsif($database{"\U$b $before"} =~ m:\b$a\b:i)
  {
#    print "$a in $b $before list - `" . $database{"\U$b $before"} . "'\n";
    $retval = -1;
  }
  else
  {
    $retval = 0;
  }

#  print "Sorting $a and $b, return val is $retval\n";
#  print "cmp would give " . ($a cmp $b) . "\n";

  return $retval;
}

1;