Newer
Older
#pragma warning(disable:4511)
#pragma warning(disable:4512)
#pragma warning(disable:4239)
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
#include <vector>
#include <string>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cassert>
//
// Logging facility
//
// #define _DOUT // output verbose info
// #define _FILE // output to generator.log, otherwise to console
#if defined(_DOUT)
#if defined(_FILE)
#include <fstream>
wofstream logfile;
#define DOUT(arg) logfile << arg
#else
#define DOUT(arg) wcerr << arg
#endif
#else
#define DOUT(arg)
#endif
//
// due to discrepancies between different implementations of STL, the following
// functions are defined explicitly and used throughout
//
//
// Insert a range into a collection
//
template<class _Col>
void __insert
(
_Col& collection,
typename _Col::iterator first,
typename _Col::iterator last
)
{
for ( ; first != last; ++first )
{
collection.insert( *first );
}
}
//
// Push_back a range into a collection
//
template<class _Col>
void __push_back
(
_Col& collection,
typename _Col::iterator first,
typename _Col::iterator last
)
{
for ( ; first != last; ++first )
{
collection.push_back( *first );
}
}
//
// Remove a node from a tree. Return the current node
//
template<class _Col>
typename _Col::iterator __map_erase
(
_Col& collection,
typename _Col::iterator where
)
{
if ( where == collection.end() ) return( collection.end() );
typename _Col::iterator next = where;
++next;
collection.erase( where );
return( next );
}
#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
namespace pictcore
{
//
// forward references
//
class Task;
class Model;
class Parameter;
class Combination;
class Exclusion;
class WorkList;
//
// callback function cancelling the generation
//
typedef bool( *AbortCallbackFunc ) ( );
//
// internal constants and enums
//
const long MaxRowsToGenerate = 1000 * 1000; // could make this bigger
typedef unsigned char TrackType;
const TrackType OPEN = 0x00;
const TrackType COVERED = 0x01;
const TrackType EXCLUDED = 0xff;
enum ComboStatus
{
Open,
CoveredMatch,
Excluded
};
{
MixedOrder,
FixedOrder,
Full,
Flat,
Random
};
{
Regular, // regular, no shortcuts, all pairs covered, can be slow
Preview, // only a few test cases, most pairs *not* covered, very fast
Approximate // most pairs covered, can return no results
};
{
GenerationCancelled,
TooManyRows,
GenerationFailure,
OutOfMemory,
Unknown
};
class GenerationError
{
public:
GenerationError( std::string file, int line, ErrorType err = ErrorType::Unknown ) :
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
_file( file ), _line( line ), _err( err ){}
ErrorType GetErrorType() { return (ErrorType) _err; }
private:
ErrorType _err;
std::string _file;
int _line;
};
//
//
//
typedef std::vector<Combination *> ComboCollection;
typedef std::vector<Parameter *> ParamCollection;
typedef std::list<size_t> ParamResult;
typedef std::pair<Parameter *, std::wstring> Value;
//
// seeding structures
//
typedef std::pair<Parameter *,int> RowSeedTerm;
typedef std::set<RowSeedTerm> RowSeed;
typedef std::list<RowSeed> RowSeedCollection;
void printRowSeed( RowSeed &seed );
//
// results
//
typedef std::vector<size_t> ResultRow;
typedef std::vector<ResultRow> ResultCollection;
//
// submodels
//
typedef std::list<Model *> SubmodelCollection;
//
// exclusions
//
typedef std::pair<Parameter *, int> ExclusionTerm;
int compareExclusionTerms( const ExclusionTerm& op1, const ExclusionTerm& op2 );
int compareExclusions( const Exclusion& op1, const Exclusion& op2 );
extern bool contained( Exclusion &a, Exclusion &b );
// exclusions have to be sorted by size when they enter ProcessExclusions()
// otherwise correct combinations will not be created and generation will fail
class ExclusionSizeLess
{
public:
bool operator() ( const Exclusion &excl1, const Exclusion &excl2 ) const;
};
typedef std::set<Exclusion, ExclusionSizeLess> ExclusionCollection;
class ExclIterCollectionPred
{
public:
bool operator() ( const ExclusionCollection::iterator excl1, const ExclusionCollection::iterator excl2 ) const;
};
typedef std::set<ExclusionCollection::iterator, ExclIterCollectionPred> ExclIterCollection;
class ExclusionTermCompare
{
public:
bool operator()( const ExclusionTerm& op1, const ExclusionTerm& op2 ) const;
};
//
//
//
class Exclusion
{
public:
typedef std::set<ExclusionTerm, ExclusionTermCompare> _Exclusion;
typedef std::vector<ExclusionTerm> _ExclusionVec;
typedef _Exclusion::iterator iterator;
typedef _Exclusion::const_iterator const_iterator;
typedef _Exclusion::reference reference;
typedef _Exclusion::const_reference const_reference;
typedef _Exclusion::value_type value_type;
Exclusion() : m_deleted( false ) {}
iterator begin() { return( col.begin() ); }
iterator end() { return( col.end() ); }
const_iterator begin() const { return( col.begin() ); }
const_iterator end() const { return( col.end() ); }
size_t size() const { return( col.size() ); }
bool empty() const { return( col.empty() ); }
// exclusion represented as a list, for next_permutation to work
_ExclusionVec::iterator lbegin() { return( vec.begin() ); }
_ExclusionVec::iterator lend() { return( vec.end() ); }
_ExclusionVec &GetList() { return( vec ); }
std::pair<iterator, bool> insert( const ExclusionTerm& Term )
{
std::pair<iterator, bool> ret = col.insert( Term );
if( ret.second ) vec.push_back( Term );
assert( col.size() == vec.size() );
return ret;
}
// for inserter() to work we need two-param insert
iterator insert( iterator pos, const ExclusionTerm& Term )
{
return insert( Term ).first;
}
bool operator <( const Exclusion& Excl ) const {
return( size() != Excl.size() ? size() < Excl.size() : -1 == compareExclusions( *this, Excl ) );
}
void markDeleted() { m_deleted = true; }
bool isDeleted() const { return( m_deleted ); }
size_t ResultParamCount() const;
void Print() const;
private:
_Exclusion col;
_ExclusionVec vec;
bool m_deleted;
};
//
// combination
//
const unsigned int UNDEFINED_ID = 0;
class Combination
{
public:
Combination( Model* model );
~Combination();
static void ResetId() { m_lastUsedId = UNDEFINED_ID; }
unsigned int GetId() { return m_id; }
void WireModel( Model* model ) { m_model = model; }
void PushParameter( Parameter* param ) { m_params.push_back( param ); }
void PopParameter() { m_params.pop_back(); }
Parameter& operator[]( int N ) const { return *( m_params[ N ] ); }
void InitBinding() { m_boundCount = 0; }
int Bind( int val, WorkList& worklist );
int AddBinding();
int GetBoundCount() const { return m_boundCount; }
bool IsFullyBound() const { return m_boundCount == m_params.size(); }
void SetOpen ( int n );
bool IsOpen ( int n ) const { return OPEN == m_bitvec[ n ]; }
bool IsExcluded( int n ) const { return EXCLUDED == m_bitvec[ n ]; }
ComboStatus Feasible ( int n );
int Feasible();
void ApplyExclusion( Exclusion& excl );
bool ViolatesExclusion();
ParamCollection& GetParameters() { return m_params; }
int GetParameterCount() const { return static_cast<int>( m_params.size() ); }
int GetOpenCount() const { return m_openCount; }
int GetRange() const { return m_range; }
void SetMapSize( int n, TrackType val = 0 );
int Weight( int vak );
void Print();
void Assign( Combination &Combo )
{
m_params = Combo.GetParameters();
m_range = Combo.GetRange();
m_openCount = Combo.GetOpenCount();
m_boundCount = Combo.GetBoundCount();
}
private:
static unsigned int m_lastUsedId; // static source of identifiers
unsigned int m_id; // unique identifier of this instance
ParamCollection m_params;
unsigned char* m_bitvec;
int m_range;
int m_openCount;
int m_boundCount;
Model* m_model;
void applyExclusion( Exclusion& excl, int index, ParamCollection::iterator pos );
};
// Combinations pointers in ComboCollection should be sorted by id and not by memory location
// We want to avoid any indeterminism stemming from sorting random pointers
class CombinationPtrSortPred {
public:
bool operator() (Combination *c1, Combination *c2)
{
return(c1->GetId() < c2->GetId());
}
};
//
// parameter
//
class Parameter
{
public:
Parameter( int order, int sequence, int valueCount, std::wstring name, bool expectedResultParam ) :
m_order( order ), m_sequence( sequence ), m_valueCount( valueCount ),
m_name( name ), m_expResultParam( expectedResultParam ), m_valueWeights( 0 )
{
// result params must have order = 1
if ( m_expResultParam ) m_order = 1;
}
virtual ~Parameter() {}
static const unsigned int UndefinedValue = 0xFFFFFFFF;
void SetOrder( int Order ) { m_order = Order; }
void SetWeights( std::vector<int> Weights );
const std::wstring& GetName() const { return m_name; }
ExclIterCollection& GetExclusions() { return m_exclusions; }
int GetOrder() const { return m_order; }
int GetWeight( int n ) const
{
if( 0 <= n && n < static_cast<int>( m_valueWeights.size() ) )
return m_valueWeights[ n ];
else
return 1;
}
int GetSequence() const { return m_sequence; }
int GetValueCount() const { return static_cast<int>( m_valueCount ); }
int GetTempResultCount() const { return static_cast<int>( m_result.size() ); }
int GetExclusionCount() const { return static_cast<int>( m_exclusions.size() ); }
void ClearExclusions() { m_avgExclusionSize = 0; m_exclusions.clear(); }
float GetAverageExclusionSize() const { return m_avgExclusionSize; }
bool IsExpectedResultParam() { return( m_expResultParam ); }
void WireTask( Task* task ) { m_task = task; }
void InitBinding()
{
m_bound = false;
m_pending = false;
}
bool Bind( int value, WorkList& worklist );
bool GetBoundCount() const { return m_bound; }
void MarkPending() { m_pending = true; }
bool IsPending() const { return m_pending; }
int PickValue();
void LinkCombination( Combination* combo ) { m_combinations.push_back( combo ); }
// remember to sort by Id, not with default sorting pred (by memory address)
void SortCombinations() { sort( m_combinations.begin(), m_combinations.end(), CombinationPtrSortPred() ); }
ComboCollection::const_iterator GetCombinationBegin() { return m_combinations.begin(); }
ComboCollection::const_iterator GetCombinationEnd() { return m_combinations.end(); }
void LinkExclusion( ExclusionCollection::iterator iter )
{
m_avgExclusionSize = ( m_avgExclusionSize * (float) m_exclusions.size() + (float) iter->size() ) / (float) ( m_exclusions.size() + 1 );
std::pair<ExclIterCollection::iterator, bool> ret = m_exclusions.insert( iter );
assert( ret.second );
}
void UnlinkExclusion( ExclusionCollection::iterator Iter )
{
size_t erased = m_exclusions.erase( Iter );
assert( 1 == erased );
}
void GetFirst() { m_resultIter = m_result.begin(); }
size_t GetNext() { return *m_resultIter++; }
size_t GetLast() { return m_currentValue; }
ParamResult& GetTempResults() { return m_result; }
virtual Model* GetModel() { return nullptr; }
virtual ParamCollection* GetComponents() { return nullptr; }
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
void CleanUp();
protected:
std::wstring m_name;
private:
int m_order; // how many ways must this parameter combine?
int m_sequence; // input sequence number, so we can reorder but output in original order
size_t m_currentValue; // current iteration's value cache
int m_valueCount; // how many values parameter has
bool m_expResultParam; // is this a special, result inducing param
bool m_bound; // variable to use in iteration - is this bound yet?
bool m_pending; // have we put it on work list yet this iteration?
ComboCollection m_combinations; // combinations this parameter participates in
ExclIterCollection m_exclusions; // exclusions referring to this parameter
ParamResult::iterator m_resultIter;
ParamResult m_result;
std::vector<int> m_valueWeights;
Task* m_task;
float m_avgExclusionSize;
};
//
// pseudoParameter is a parameter constructed out of results generated by a submodel
//
class PseudoParameter : public Parameter
{
public:
PseudoParameter(int order, unsigned int sequence, Model *submodel);
virtual Model* GetModel() {return m_model;}
virtual ParamCollection* GetComponents();
private:
Model* m_model;
};
//
//
//
struct MatchParameterPointer : public std::binary_function<ExclusionTerm, Parameter*, bool>
{
bool operator()( const ExclusionTerm excl, Parameter* pParam ) const
{
return excl.first == pParam;
}
};
//
//
//
class Model
{
public:
Model( const std::wstring& id, GenerationType type, int order, long seed = 0 ) :
m_id( id ), m_generationType( type ), m_order( order ), m_maxRows( 0 ), m_lastParamId( UNDEFINED_ID + 1000 * 1000 ) {
SetRandomSeed( seed );
}
~Model();
void GenerateVirtualRoot(); // generates a root of a hierarchical cluster
void Generate(); // entry point of the generation
GenerationType GetGenerationType() { return( m_generationType ); }
bool AddExclusion( Exclusion& e )
{
std::pair<ExclusionCollection::iterator, bool> ret = m_exclusions.insert( e );
return( ret.second );
}
void AddSubmodel( Model* model ) { m_submodels.push_back( model ); }
void AddParameter( Parameter* param )
{
// parameter names should be unique but we can't assert it here
// as there are cases where we add same name params to the collection
// internally and delete the duplicate almost immediately
param->WireTask( GetTask() );
m_parameters.push_back( param );
}
void AddRowSeed( RowSeed& seed )
{
m_rowSeeds.push_back( seed );
for( auto & submodel : m_submodels ) submodel->AddRowSeed( seed );
}
void AddRowSeeds( RowSeedCollection::iterator begin, RowSeedCollection::iterator end )
{
while( begin != end ) AddRowSeed( *begin++ );
}
void SetOrder ( int order ) { m_order = order; }
void SetMaxRows ( long maxRows ) { m_maxRows = maxRows; }
void SetRandomSeed( long seed )
{
m_randomSeed = seed;
srand( m_randomSeed );
for( auto & submodel : m_submodels ) submodel->SetRandomSeed( m_randomSeed );
}
void WireTask( Task* task )
{
m_task = task;
for( auto & param : m_parameters ) param->WireTask( task );
for( auto & submodel : m_submodels ) submodel->WireTask( task );
}
Task* GetTask() { return( m_task ); }
std::wstring& GetId() { return m_id; }
SubmodelCollection& GetSubmodels() { return m_submodels; }
ExclusionCollection& GetExclusions() { return m_exclusions; }
RowSeedCollection& GetRowSeeds() { return( m_rowSeeds ); }
ResultCollection& GetResults() { return m_results; }
ParamCollection& GetParameters() { return m_parameters; }
void GetAllParameters( ParamCollection& params )
{
__push_back( params, m_parameters.begin(), m_parameters.end() );
for( auto & submodel : m_submodels ) submodel->GetAllParameters( params );
}
size_t GetAllParametersCount()
{
size_t count = m_parameters.size();
return( count );
}
size_t GetResultParameterCount()
{
size_t count = 0;
for( auto & param : m_parameters )
if( param->IsExpectedResultParam() )
++count;
return count;
}
int GetOrder() { return m_order; }
long GetRandomSeed() { return( m_randomSeed ); }
long GetTotalCombinationsCount() { return static_cast<long>( m_totalCombinations ); }
long GetRemainingCombinationsCount() { return static_cast<long>( m_remainingCombinations ); }
int GetSubmodelCount() { return static_cast<int> ( m_submodels.size() ); }
int GetResultCount() { return static_cast<int> ( m_results.size() ); }
int GlobalZerosCount;
private:
ParamCollection m_parameters;
ExclusionCollection m_exclusions;
SubmodelCollection m_submodels;
RowSeedCollection m_rowSeeds;
std::deque<Parameter*> m_worklist;
ResultCollection m_results;
std::wstring m_id;
int m_order;
long m_randomSeed;
long m_maxRows;
GenerationType m_generationType;
Parameter* m_currentParam;
unsigned int m_lastParamId;
long m_totalCombinations;
long m_remainingCombinations;
Task* m_task;
void generateMixedOrder(); // generates mixed-order (the most generic)
void generateFixedOrder(); // generates fixed, n-order
void generateFull(); // generates exhaustive
void generateFlat(); // generates order of 1 with extra assumptions
void generateRandom(); // generates randomized suites
void resolvePseudoParams(); // helper that handles submodels
void fixRowSeeds(); // helper that cleans up seeding rows
void gcd( ComboCollection& ComboCol ); // generation entry point
void choose( ParamCollection::iterator first,
ParamCollection::iterator last,
int order, int realOrder,
Combination& baseCombo, ComboCollection& vecCombo );
void processExclusions( ComboCollection& comboCol );
bool excludeConflictingParamValues();
bool mapExclusionsToPseudoParameters();
void mapRowSeedsToPseudoParameters();
void deriveSubmodelExclusions();
bool rowViolatesExclusion( ResultRow& row );
bool rowViolatesExclusion( Exclusion& row );
void markUndefinedValuesInResultParams();
Exclusion generateRandomRow();
};
//
// task governs the entire generation
//
class Task
{
public:
Task();
~Task();
// this has to be called right before generation starts
void PrepareForGeneration();
void AllocWorkbuf( int Size );
int* GetWorkbuf() { return( m_workbuf ); }
void DeallocWorkbuf();
void SetRootModel( Model* model )
{
m_rootModel = model;
model->WireTask( this );
}
size_t GetTotalParameterCount() { return( m_rootModel->GetAllParametersCount() ); }
void SetAbortCallback( AbortCallbackFunc func ) { m_abortCallback = func; }
bool AddExclusion( Exclusion& excl )
{
std::pair<ExclusionCollection::iterator, bool> ret = m_exclusions.insert( excl );
return( ret.second );
}
void AddRowSeed( RowSeed& seed ) { m_rowSeeds.push_back( seed ); }
Model* GetRootModel() { return( m_rootModel ); }
ExclusionCollection& GetExclusions() { return( m_exclusions ); }
RowSeedCollection& GetRowSeeds() { return( m_rowSeeds ); }
bool AbortGeneration() { return( ( nullptr == m_abortCallback ) ? false : m_abortCallback() ); }
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
void SetGenerationMode( GenerationMode mode ) { m_generationMode = mode; }
GenerationMode GetGenerationMode() { return( m_generationMode ); }
// approximate mode
void SetMaxRandomTries( size_t max ) { m_maxRandomTries = max; }
size_t GetMaxRandomTries() { return( m_maxRandomTries ); }
ResultCollection& GetResults() { return m_rootModel->GetResults(); }
// these two functions are used by C-style API which only returns one row at a time
void ResetResultFetching();
ResultCollection::iterator GetNextResultRow();
private:
Model* m_rootModel;
ExclusionCollection m_exclusions;
RowSeedCollection m_rowSeeds;
AbortCallbackFunc m_abortCallback;
GenerationMode m_generationMode;
// in Approximate and Randomized mode, how many times we will try to
// generate a random row before giving up
size_t m_maxRandomTries;
Model* findMatchingNode( Exclusion& exclusion, Model* root );
bool findParamInSubtree( Parameter* param, Model* root );
void deriveExclusions();
// a global workspace shared by multiple objects
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
// result row pointer allows C-style API to implement GetNextResultRow function
// i.e. get one result row at a time
ResultCollection::iterator m_currentResultRow;
};
//
//
//
class WorkList
{
public:
WorkList() {}
~WorkList() {}
void AddItem( Parameter* pParam );
Parameter *GetItem();
bool IsEmpty() const { return m_rep.empty(); }
void Print();
private:
std::deque<Parameter*> m_rep;
};
}
#ifdef __cplusplus
}
#endif // __cplusplus