View difference between Paste ID: ea3s6XFy and jM70Yibj
SHOW: | | - or go back to the newest paste.
1
//  /*----------------------------------------------------------------*\
2
//  |   Main Program: Simple tester for Partial_Map
3
//  |*----------------------------------------------------------------*|
4
//  |   Date:    17 November 1997, revised 20 January 2008     
5
//  |   Author:  Bruce W. Weide 
6
//  |   
7
//  |   Brief User's Manual:
8
//  |   Allows user to test a Partial_Map implementation, interactively
9
//  |   or in batch mode.
10
//  |   
11
//  \*----------------------------------------------------------------*/
12
13
///---------------------------------------------------------------------
14
/// Global Context -----------------------------------------------------
15
///---------------------------------------------------------------------
16
17
#include "RESOLVE_Foundation.h"
18
19
#include "AT/General/Are_In_Order.h"
20
#include "CI/Natural/Number_1_C.h"
21
#include "CT/Partial_Map/Kernel_7_C.h"
22
#include "CT/Partial_Map/Put_To_1.h"
23
#include "CT/Array/Kernel_1_C.h"
24
25
///---------------------------------------------------------------------
26
/// Interface ----------------------------------------------------------
27
///---------------------------------------------------------------------
28
29
// Concrete instances
30
31
concrete_instance
32
utility_class Natural_Are_In_Order_1 :
33
    implements
34
        abstract_instance General_Are_In_Order <Natural_Number_1_C>
35
{
36
public:
37
    
38
    /*!
39
	math definition ARE_IN_ORDER (
40
		x: NATURAL_MODEL,
41
		y: NATURAL_MODEL,
42
	    ): boolean is
43
	    x <= y
44
    !*/
45
46
    utility_function_body Boolean Are_In_Order (
47
	    preserves Natural_Number_1_C& n1,
48
	    preserves Natural_Number_1_C& n2
49
	)
50
    {
51
	return n1.Compare (n2) <= 0;
52
    }	
53
};
54
55
//----------------------------------------------------------------------
56
57
concrete_instance
58
class Partial_Map_Of_Natural_To_Text_Base :
59
    instantiates Partial_Map_Kernel_7_C <
60
	    Natural_Number_1_C,
61
            Text,
62
            Natural_Are_In_Order_1
63
	>
64
{};
65
66
//----------------------------------------------------------------------
67
68
concrete_instance
69
class Partial_Map_Of_Natural_To_Text :
70
    instantiates Partial_Map_Put_To_1 <
71
	    Natural_Number_1_C,
72
	    Text,
73
	    Partial_Map_Of_Natural_To_Text_Base
74
	>
75
{};
76
77
//----------------------------------------------------------------------
78
79
concrete_instance
80
class Array_Of_Partial_Map_Of_Natural_To_Text :
81
    instantiates Array_Kernel_1_C <Partial_Map_Of_Natural_To_Text>
82
{};
83
84
//----------------------------------------------------------------------
85
//----------------------------------------------------------------------
86
87
// Program control objects
88
89
object Boolean interactive;
90
object Text command_line;
91
object Character command_code;
92
object Character_IStream input;
93
object Character_OStream output;
94
95
// Test objects
96
97
object Array_Of_Partial_Map_Of_Natural_To_Text m;
98
99
//----------------------------------------------------------------------
100
//----------------------------------------------------------------------
101
102
// Global operations (omitted for brevity)
103
104
//----------------------------------------------------------------------
105
//----------------------------------------------------------------------
106
107
// Global operation bodies
108
109
global_procedure_body Determine_Interactive_Mode ()
110
{
111
    output << "Run in interactive mode (y/n)? ";
112
    input >> command_line;
113
    command_code = command_line[0];
114
    interactive = (command_code == 'y');
115
}
116
117
//----------------------------------------------------------------------
118
119
global_procedure_body Get_Command ()
120
{
121
    if (interactive)
122
    {
123
	// Show menu of commands:
124
	// Concept-specific operations
125
	
126
	output << "\n\nCommand: s [Swap]\n"
127
	       << "         c [Clear]\n"
128
	       << "         p [Put_To]\n"
129
	       << "         d [Define]\n"
130
	       << "         u [Undefine]\n"
131
	       << "         a [Undefine_Any]\n"
132
	       << "         i [Is_Defined]\n"
133
	       << "         [ [Swap Using Accessor]\n"
134
	       << "         z [Size]\n"
135
136
	// Tester control operations
137
	
138
	       << "         q [Quit]: ";
139
    }
140
141
    // Get next command line and code
142
	     
143
    input >> command_line;
144
    command_code = command_line[0];
145
}
146
147
//----------------------------------------------------------------------
148
149
global_procedure_body Do_Swap ()
150
{
151
    object Integer id1, id2;
152
    object Partial_Map_Of_Natural_To_Text temp;  // to avoid repeated arg
153
154
    if (interactive)
155
    {
156
	output << "Swap which object? ";
157
    }
158
    input >> id1;
159
    if (interactive)
160
    {
161
	output << "  ... with which object? ";
162
    }
163
    input >> id2;
164
    output << "\n--------------------------------------------"
165
	   << "\n                   |";
166
    output << "\n--------------------------------------------"
167
	   << "\nm" << id1 << " &= m" << id2 << ";          |";
168
    temp &= m[id1];
169
    temp &= m[id2];
170
    temp &= m[id1];
171
    output << "\n--------------------------------------------"
172
	   << "\n                   |";
173
    output << "\n--------------------------------------------";
174
}
175
176
//----------------------------------------------------------------------
177
178
global_procedure_body Do_Clear ()
179
{
180
    object Integer id;
181
182
    if (interactive)
183
    {
184
	output << "Clear which object? ";
185
    }
186
    input >> id;
187
    output << "\n--------------------------------------------"
188
	   << "\n                   |";
189
    output << "\n--------------------------------------------"
190
	   << "\nm" << id << ".Clear ();       |";
191
    m[id].Clear ();
192
    output << "\n--------------------------------------------"
193
	   << "\n                   |";
194
    output << "\n--------------------------------------------";
195
}
196
197
//----------------------------------------------------------------------
198
199
global_procedure_body Do_Put_To ()
200
{
201
    object Integer id;
202
203
    if (interactive)
204
    {
205
	output << "Put which object? ";
206
    }
207
    input >> id;
208
    output << "\n--------------------------------------------"
209
	   << "\nm" << id << " = ";
210
    output << m[id];
211
    output << "\n--------------------------------------------";
212
}
213
214
//----------------------------------------------------------------------
215
216
global_procedure_body Do_Define ()
217
{
218
    object Integer id;
219
    object Natural_Number_1_C d;
220
    object Text r;
221
222
    if (interactive)
223
    {
224
	output << "Define into which object? ";
225
    }
226
    input >> id;
227
    if (interactive)
228
    {
229
	output << "  ... d value? ";
230
    }
231
    input >> d;
232
    if (interactive)
233
    {
234
	output << "  ... r value? ";
235
    }
236
    input >> r;
237
    output << "\n--------------------------------------------"
238
	   << "\n                   |  d = " << d
239
	   << "\n                   |  r = " << r;
240
    output << "\n--------------------------------------------"
241
	 << "\nm" << id << ".Define (d, r);  |";
242
    m[id].Define (d, r);
243
    output << "\n--------------------------------------------"
244
	   << "\n                   |  d = " << d
245
	   << "\n                   |  r = " << r;
246
    output << "\n--------------------------------------------";
247
}
248
249
//----------------------------------------------------------------------
250
251
global_procedure_body Do_Undefine ()
252
{
253
    object Integer id;
254
    object Natural_Number_1_C d, d_copy;
255
    object Text r;
256
257
    if (interactive)
258
    {
259
	output << "Undefine from which object? ";
260
    }
261
    input >> id;
262
    if (interactive)
263
    {
264
	output << "  ... d value? ";
265
    }
266
    input >> d;
267
    if (interactive)
268
    {
269
	output << "  ... d_copy value? ";
270
    }
271
    input >> d_copy;
272
    if (interactive)
273
    {
274
	output << "  ... r value? ";
275
    }
276
    input >> r;
277
    output << "\n--------------------------------------------"
278
	   << "\n                   |  d = " << d
279
	   << "\n                   |  d_copy = " << d_copy
280
	   << "\n                   |  r = " << r;
281
    output << "\n--------------------------------------------"
282
	   << "\nm" << id << ".Undefine (d, d_copy, r);  |";
283
    m[id].Undefine (d, d_copy, r);
284
    output << "\n--------------------------------------------"
285
	   << "\n                   |  d = " << d
286
	   << "\n                   |  d_copy = " << d_copy
287
	   << "\n                   |  r = " << r;
288
    output << "\n--------------------------------------------";
289
}
290
291
//----------------------------------------------------------------------
292
293
global_procedure_body Do_Undefine_Any ()
294
{
295
    object Integer id;
296
    object Natural_Number_1_C d;
297
    object Text r;
298
299
    if (interactive)
300
    {
301
	output << "Undefine_Any from which object? ";
302
    }
303
    input >> id;
304
    if (interactive)
305
    {
306
	output << "  ... d value? ";
307
    }
308
    input >> d;
309
    if (interactive)
310
    {
311
	output << "  ... r value? ";
312
    }
313
    input >> r;
314
    output << "\n--------------------------------------------"
315
	   << "\n                   |  d = " << d
316
	   << "\n                   |  r = " << r;
317
    output << "\n--------------------------------------------"
318
	   << "\nm" << id << ".Undefine_Any (d, r);  |";
319
    m[id].Undefine_Any (d, r);
320
    output << "\n--------------------------------------------"
321
	   << "\n                   |  d = " << d
322
	   << "\n                   |  r = " << r;
323
    output << "\n--------------------------------------------";
324
}
325
326
//----------------------------------------------------------------------
327
328
global_procedure_body Do_Is_Defined ()
329
{
330
    object Integer id;
331
    object Natural_Number_1_C d;
332
    object Boolean b;
333
334
    if (interactive)
335
    {
336
	output << "Is_Defined in which object? ";
337
    }
338
    input >> id;
339
    if (interactive)
340
    {
341
	output << "  ... d value? ";
342
    }
343
    input >> d;
344
    output << "\n--------------------------------------------"
345
	   << "\n                   |  d = " << d
346
	   << "\n                   |  b = ?";
347
    output << "\n--------------------------------------------"
348
	   << "\nb = m" << id << ".Is_Defined (d);  |";
349
    b = m[id].Is_Defined (d);
350
    output << "\n--------------------------------------------"
351
	   << "\n                   |  d = " << d
352
	   << "\n                   |  b = " << b;
353
    output << "\n--------------------------------------------";
354
}
355
356
//----------------------------------------------------------------------
357
358
global_procedure_body Do_Swap_Using_Accessor ()
359
{
360
    object Integer id;
361
    object Natural_Number_1_C d;
362
    object Text r;
363
364
    if (interactive)
365
    {
366
	output << "Swap using accessor in which object? ";
367
    }
368
    input >> id;
369
    if (interactive)
370
    {
371
	output << "  ... d value? ";
372
    }
373
    input >> d;
374
    if (interactive)
375
    {
376
	output << "  ... r value to swap with? ";
377
    }
378
    input >> r;
379
    output << "\n--------------------------------------------"
380
	   << "\n                   |  d = " << d
381
	   << "\n                   |  r = " << r;
382
    output << "\n--------------------------------------------"
383
	   << "\nr &= m" << id << "[d];        |";
384
    r &= m[id][d];
385
    output << "\n--------------------------------------------"
386
	   << "\n                   |  d = " << d
387
	   << "\n                   |  r = " << r;
388
    output << "\n--------------------------------------------";
389
}
390
391
//----------------------------------------------------------------------
392
393
global_procedure_body Do_Size ()
394
{
395
    object Integer id;
396
    object Integer i;
397
398
    if (interactive)
399
    {
400
	output << "Get size of which object? ";
401
    }
402
    input >> id;
403
    output << "\n--------------------------------------------"
404
	   << "\n                   |  i = ?";
405
    output << "\n--------------------------------------------"
406
	   << "\ni = m" << id << ".Size ();    |";
407
    i = m[id].Size ();
408
    output << "\n--------------------------------------------"
409
	   << "\n                   |  i = " << i;
410
    output << "\n--------------------------------------------";
411
}
412
413
//----------------------------------------------------------------------
414
//----------------------------------------------------------------------
415
416
program_body main ()
417
{
418
    object Boolean finished;
419
    
420
    // Open input and output streams
421
    
422
    input.Open_External ("");
423
    output.Open_External ("");
424
425
    // Set number of test objects (= 8; this number is absolutely NOT
426
    // related to the number of buckets in the hash table, when the
427
    // Partial_Map_Kernel implementation uses hashing)
428
429
    m.Set_Bounds (0, 7);
430
431
    // Ask user about interactive mode
432
    
433
    Determine_Interactive_Mode ();
434
435
    // Execute interactive testing loop until finished
436
    
437
    while (not finished)
438
    {
439
	// Ask user for next command
440
	
441
	Get_Command ();
442
	
443
	// Perform next command
444
	
445
	case_select (command_code)
446
	    {
447
		// Concept-specific operations
448
		
449
	        case 's':
450
		{
451
		    Do_Swap ();
452
		}
453
		break;
454
	        case 'c':
455
		{
456
		    Do_Clear ();
457
		}
458
		break;
459
	        case 'p':
460
		{
461
		    Do_Put_To ();
462
		}
463
		break;
464
	        case 'd':
465
		{
466
		    Do_Define ();
467
		}
468
		break;
469
	        case 'u':
470
		{
471
		    Do_Undefine ();
472
		}
473
		break;
474
	        case 'a':
475
		{
476
		    Do_Undefine_Any ();
477
		}
478
		break;
479
	        case 'i':
480
		{
481
		    Do_Is_Defined ();
482
		}
483
		break;
484
	        case '[':
485
		{
486
		    Do_Swap_Using_Accessor ();
487
		}
488
		break;
489
	        case 'z':
490
		{
491
		    Do_Size ();
492
		}
493
		break;
494
	    
495
		// Tester control operations
496
	    
497
	        case 'q':
498
		{
499
		    finished = true;
500
		}
501
		break;
502
	    
503
	        default:
504
		{
505
		    output << "\n" << command_line;
506
		}
507
		break;
508
	    }
509
    }
510
    
511
    // Close input and output streams
512
    input.Close_External ();
513
    output.Close_External ();
514
}