| 1 | /* |
| 2 | * Copyright (C) 1996-2011 Daniel Waggoner |
| 3 | * |
| 4 | * This free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * It is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * If you did not received a copy of the GNU General Public License |
| 15 | * with this software, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "matrix_test.hpp" |
| 19 | |
| 20 | #include "dw_matrix_rand.h" |
| 21 | #include "dw_std.h" |
| 22 | |
| 23 | #include <string.h> |
| 24 | #include <iostream> |
| 25 | #include <ostream> |
| 26 | |
| 27 | int CheckArgumentString(char*); |
| 28 | int CreateArguments(char*,TArg**,int); |
| 29 | |
| 30 | |
| 31 | //=== TArgInc ========================================================= begin === |
| 32 | TArgInc::TArgInc(TArgList* p_args) |
| 33 | { |
| 34 | args=p_args; |
| 35 | args->DefaultTestSizes(); |
| 36 | } |
| 37 | |
| 38 | TArgInc::TArgInc(TArgList* p_args, int** ts) |
| 39 | { |
| 40 | args=p_args; |
| 41 | args->SetTestSizes(ts); |
| 42 | } |
| 43 | |
| 44 | TArgInc::TArgInc(TArgList* p_args, int nts, int* ts) |
| 45 | { |
| 46 | args=p_args; |
| 47 | args->SetTestSizes(nts,ts); |
| 48 | } |
| 49 | |
| 50 | TArgInc::TArgInc(TArgList* p_args, int min, int max) |
| 51 | { |
| 52 | args=p_args; |
| 53 | args->SetTestSizes(min,max); |
| 54 | } |
| 55 | |
| 56 | void TArgInc::ComputeActualSizes(void) |
| 57 | { |
| 58 | args->ComputeCorrectSizes(); |
| 59 | memcpy(args->GetActualSizes(),args->GetCorrectSizes(),args->GetNumberSizes()*sizeof(int)); |
| 60 | } |
| 61 | //=== TArgInc =========================================================== end === |
| 62 | |
| 63 | |
| 64 | //=== TArgInc_Basic =================================================== begin === |
| 65 | TArgInc_Basic::~TArgInc_Basic() |
| 66 | { |
| 67 | if (valid_null) dw_free(valid_null); |
| 68 | } |
| 69 | |
| 70 | int TArgInc_Basic::First(void) |
| 71 | { |
| 72 | for (int i=args->number_args()-1; i >= 0; i--) valid_null[i]=0; |
| 73 | args->ResetMinimalIndexes(); |
| 74 | ComputeActualSizes(); |
| 75 | args->Allocate(); |
| 76 | args->Initialize(); |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | int TArgInc_Basic::Next(void) |
| 81 | { |
| 82 | TArg_pointer *p; |
| 83 | if (!IncrementValidNull()) |
| 84 | if (!args->IncrementMinimalIndexes()) |
| 85 | return 0; |
| 86 | |
| 87 | ComputeActualSizes(); |
| 88 | args->Allocate(); |
| 89 | for (int i=args->number_args()-1; i >= 0; i--) |
| 90 | if (valid_null[i] && (p=dynamic_cast<TArg_pointer*>(args->arg(i)))) |
| 91 | p->Free(); |
| 92 | args->Initialize(); |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | int TArgInc_Basic::IncrementValidNull(void) |
| 97 | { |
| 98 | TArg_pointer *p; |
| 99 | for (int i=args->number_args()-1; i >= 0; i--) |
| 100 | if (valid_null[i] == 1) |
| 101 | valid_null[i]=0; |
| 102 | else |
| 103 | if ((p=dynamic_cast<TArg_pointer*>(args->arg(i))) && p->IsNullOK()) |
| 104 | { |
| 105 | valid_null[i]=1; |
| 106 | return 1; |
| 107 | } |
| 108 | return 0; |
| 109 | } |
| 110 | //=== TArgInc_Basic ===================================================== end === |
| 111 | |
| 112 | |
| 113 | //=== TArgInc_InvalidSizes ============================================ begin === |
| 114 | void TArgInc_InvalidSizes::ComputeActualSizes(void) |
| 115 | { |
| 116 | TArgInc::ComputeActualSizes(); |
| 117 | args->MakeSizeInvalid(i); |
| 118 | } |
| 119 | |
| 120 | int TArgInc_InvalidSizes::First(void) |
| 121 | { |
| 122 | i=0; |
| 123 | args->ResetMinimalIndexes(); |
| 124 | ComputeActualSizes(); |
| 125 | args->Allocate(); |
| 126 | args->Initialize(); |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | int TArgInc_InvalidSizes::Next(void) |
| 131 | { |
| 132 | if (i < args->GetNumberSizes()-1) |
| 133 | i++; |
| 134 | else |
| 135 | if (args->IncrementMinimalIndexes()) |
| 136 | i=0; |
| 137 | else |
| 138 | return 0; |
| 139 | |
| 140 | ComputeActualSizes(); |
| 141 | args->Allocate(); |
| 142 | args->Initialize(); |
| 143 | return 1; |
| 144 | } |
| 145 | //=== TArgInc_InvalidSizes ============================================== end === |
| 146 | |
| 147 | |
| 148 | //=== TArgInc_InvalidNulls ============================================ begin === |
| 149 | int TArgInc_InvalidNulls::First(void) |
| 150 | { |
| 151 | i=-1; |
| 152 | args->ResetMinimalIndexes(); |
| 153 | return Next(); |
| 154 | } |
| 155 | |
| 156 | int TArgInc_InvalidNulls::Next(void) |
| 157 | { |
| 158 | TArg_pointer *p; |
| 159 | while (++i < args->number_args()) |
| 160 | if ((p=dynamic_cast<TArg_pointer*>(args->arg(i))) && !p->IsNullOK()) break; |
| 161 | |
| 162 | while (i == args->number_args()) |
| 163 | if (!args->IncrementMinimalIndexes()) |
| 164 | return 0; |
| 165 | else |
| 166 | for (i=0; i < args->number_args(); i++) |
| 167 | if ((p=dynamic_cast<TArg_pointer*>(args->arg(i))) && !p->IsNullOK()) break; |
| 168 | |
| 169 | ComputeActualSizes(); |
| 170 | args->Allocate(); |
| 171 | p->Free(); |
| 172 | args->Initialize(); |
| 173 | return 1; |
| 174 | } |
| 175 | //=== TArgInc_InvalidNulls ============================================== end === |
| 176 | |
| 177 | |
| 178 | //=== TArgInc_EqualPointers =========================================== begin === |
| 179 | void TArgInc_EqualPointers::SetupEqualPointers(void) |
| 180 | { |
| 181 | actual=(int*)NULL; |
| 182 | master=(int**)NULL; |
| 183 | if (args->number_args() > 0) |
| 184 | { |
| 185 | if (!(actual=(int*)dw_malloc(args->number_args()*sizeof(int)))) throw ERR_MEM_FAILURE; |
| 186 | if (!(master=(int**)dw_malloc(args->number_args()*sizeof(int*)))) throw ERR_MEM_FAILURE; |
| 187 | for (int i=0; i < args->number_args(); i++) master[i]=(int*)NULL; |
| 188 | for (int i=0; i < args->number_args(); i++) |
| 189 | if (!(master[i]=(int*)dw_malloc((args->number_args()+1)*sizeof(int)))) throw ERR_MEM_FAILURE; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | TArgInc_EqualPointers::~TArgInc_EqualPointers() |
| 194 | { |
| 195 | if (actual) dw_free(actual); |
| 196 | if (master) |
| 197 | { |
| 198 | for (int i=0; i < args->number_args(); i++) |
| 199 | if (master[i]) dw_free(master[i]); |
| 200 | dw_free(master); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | int TArgInc_EqualPointers::First(void) |
| 205 | { |
| 206 | args->ResetMinimalIndexes(); |
| 207 | while (!SetMaster()) |
| 208 | if (!args->IncrementMinimalIndexes()) |
| 209 | return 0; |
| 210 | return Next(); |
| 211 | } |
| 212 | |
| 213 | int TArgInc_EqualPointers::Next(void) |
| 214 | { |
| 215 | if (IncrementActual()) |
| 216 | { |
| 217 | Initialize(); |
| 218 | return 1; |
| 219 | } |
| 220 | else |
| 221 | while (args->IncrementMinimalIndexes()) |
| 222 | if (SetMaster()) |
| 223 | if (IncrementActual()) |
| 224 | { |
| 225 | Initialize(); |
| 226 | return 1; |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | int TArgInc_EqualPointers::SetMaster(void) |
| 232 | { |
| 233 | TArg_sized *sp, *s; |
| 234 | int i, j, k, primary, found, n=args->number_args(), *row, *actual_sizes=args->GetActualSizes(); |
| 235 | |
| 236 | ComputeActualSizes(); |
| 237 | for (i=n-1; i >= 0; i--) |
| 238 | { |
| 239 | actual[i]=-1; |
| 240 | master[i][n]=-1; |
| 241 | for (j=n-1; j >= 0; j--) |
| 242 | master[i][j]=0; |
| 243 | } |
| 244 | |
| 245 | for (threads=0, primary=0; primary < n; primary++) |
| 246 | if (dynamic_cast<TArg_pointer*>(args->arg(primary))) |
| 247 | { |
| 248 | for (i=0; i < n; i++) |
| 249 | if ((i != primary) && (dynamic_cast<TArg_pointer*>(args->arg(i)))) |
| 250 | { |
| 251 | found=0; |
| 252 | switch (args->arg(i)->type()) |
| 253 | { |
| 254 | case dw_TVector_type: |
| 255 | if (dynamic_cast<TArg_TVector*>(args->arg(primary))) found=1; |
| 256 | break; |
| 257 | case dw_TMatrix_S_type: |
| 258 | if (dynamic_cast<TArg_TMatrix_S*>(args->arg(primary))) found=1; |
| 259 | break; |
| 260 | case dw_TMatrix_SPD_type: |
| 261 | if (dynamic_cast<TArg_TMatrix_SPD*>(args->arg(primary))) found=1; |
| 262 | break; |
| 263 | case dw_TMatrix_U_type: |
| 264 | if (dynamic_cast<TArg_TMatrix_U*>(args->arg(primary))) found=1; |
| 265 | break; |
| 266 | case dw_TMatrix_L_type: |
| 267 | if (dynamic_cast<TArg_TMatrix_L*>(args->arg(primary))) found=1; |
| 268 | break; |
| 269 | case dw_TMatrix_type: |
| 270 | if (dynamic_cast<TArg_TMatrix*>(args->arg(primary))) found=1; |
| 271 | break; |
| 272 | case dw_TPermutation_type: |
| 273 | if (dynamic_cast<TArg_TPermutation*>(args->arg(primary))) found=1; |
| 274 | break; |
| 275 | case dw_pInteger_type: |
| 276 | if (dynamic_cast<TArg_pInteger*>(args->arg(primary))) found=1; |
| 277 | break; |
| 278 | default: |
| 279 | throw ERR_ARG_TYPE; |
| 280 | } |
| 281 | if (found) |
| 282 | if ((sp=dynamic_cast<TArg_sized*>(args->arg(primary))) && (s=dynamic_cast<TArg_sized*>(args->arg(i)))) |
| 283 | { |
| 284 | for (j=sp->GetNumberSizes()-1; j >= 0; j--) |
| 285 | if (sp->GetSizeReference(j) != s->GetSizeReference(j)) |
| 286 | { |
| 287 | found=0; |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | if (found) |
| 292 | { |
| 293 | master[threads][i]=1; |
| 294 | master[threads][n]=primary; |
| 295 | } |
| 296 | } |
| 297 | if (master[threads][n] != -1) |
| 298 | { |
| 299 | master[threads][primary]=1; |
| 300 | threads++; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | for (i=0; i < threads; i++) |
| 305 | for (j=i+1; j < threads; j++) |
| 306 | { |
| 307 | for (k=0; k < n; k++) |
| 308 | if (master[i][k] != master[j][k]) break; |
| 309 | if (k == n) master[j][master[i][n]]=0; |
| 310 | } |
| 311 | |
| 312 | for (i=0; i < threads; i++) |
| 313 | master[i][master[i][n]]=0; |
| 314 | |
| 315 | /** |
| 316 | printf("\n"); |
| 317 | for (i=0; i < n; i++) |
| 318 | { |
| 319 | for (j=0; j <= n; j++) printf("%d ",master[i][j]); |
| 320 | printf("\n"); |
| 321 | } |
| 322 | printf("\n"); |
| 323 | /**/ |
| 324 | |
| 325 | return threads ? 1 : 0; |
| 326 | } |
| 327 | |
| 328 | int TArgInc_EqualPointers::IncrementActual(void) |
| 329 | { |
| 330 | int i, k, primary, n=args->number_args(); |
| 331 | for (k=0; k < threads; k++) |
| 332 | { |
| 333 | primary=master[k][n]; |
| 334 | if ((actual[primary] == -1) || (actual[primary] == primary)) |
| 335 | { |
| 336 | actual[primary]=-1; |
| 337 | for (i=0; i < n; i++) |
| 338 | if ((i != primary) && master[k][i]) |
| 339 | if (actual[i] == primary) |
| 340 | actual[i]=-1; |
| 341 | else |
| 342 | if (actual[i] == -1) |
| 343 | { |
| 344 | actual[i]=actual[primary]=primary; |
| 345 | return 1; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | void TArgInc_EqualPointers::Initialize(void) |
| 353 | { |
| 354 | int i, k, primary, n=args->number_args(); |
| 355 | args->Allocate(); |
| 356 | for (k=0; k < threads; k++) |
| 357 | for (primary=master[k][n], i=0; i < n; i++) |
| 358 | if ((i != primary) && (actual[i] == primary)) |
| 359 | (dynamic_cast<TArg_pointer*>(args->arg(i)))->EquatePointers(dynamic_cast<TArg_pointer*>(args->arg(primary))); |
| 360 | args->Initialize(); |
| 361 | //for (i=0; i < args->number_args(); i++) printf("%d ",actual[i]); printf("\n"); args->PrintArgList(cout);printf("\n"); getchar(); |
| 362 | } |
| 363 | //=== TArgInc_EqualPointers ============================================= end === |
| 364 | |
| 365 | |
| 366 | //=== TArgList ======================================================== begin === |
| 367 | /* |
| 368 | arg_string syntax |
| 369 | |
| 370 | pI(m) - integer array |
| 371 | I - integer |
| 372 | I(m) - integer as a size parameter |
| 373 | F - PRECISION |
| 374 | V(m) - TVector : general |
| 375 | M(m,n) - TMatrix : general |
| 376 | U(m,n) - TMatrix : upper triangular |
| 377 | L(m,n) - TMatrix : lower triangular |
| 378 | S(m,n) - TMatrix : symmetric |
| 379 | SPD(m,n) - TMatrix : symmetric and positive definite |
| 380 | P(m) - TPermutation : general |
| 381 | |
| 382 | Arguments must be separated by a semicolon and the last argument is not |
| 383 | followed by a semicolon. |
| 384 | |
| 385 | If any of the pointer argments is followed by an '!', then that argument is |
| 386 | allowed to be null. |
| 387 | |
| 388 | If any of the pointer arguments is followd by an '+', then that argument is |
| 389 | allowed to change. If an argument is allowed to be null and change, then |
| 390 | the '!' must preceed the '+'. |
| 391 | |
| 392 | The sizes can be any lower case letter. A '?' signifies that the size is not |
| 393 | determined. |
| 394 | |
| 395 | If any of these argument is preceeded by a 'p', then it is a pointer to an |
| 396 | argument of that type. (not yet implemented) |
| 397 | |
| 398 | Returns the number of arguments or -1 upon error. |
| 399 | */ |
| 400 | int CheckArgString(char *s) |
| 401 | { |
| 402 | int nargs=0, j=0; |
| 403 | |
| 404 | if (!s || !s[0]) return nargs; |
| 405 | |
| 406 | while (1) |
| 407 | { |
| 408 | switch (s[j]) |
| 409 | { |
| 410 | case 'p': |
| 411 | switch (s[++j]) |
| 412 | { |
| 413 | case 'I': |
| 414 | if ((s[j+1] != '(') || ((s[j+2] != '?') && ((s[j+2] < 'a') || ('z' < s[j+2]))) || (s[j+3] != ')')) |
| 415 | return -1; |
| 416 | if (s[j+=4] == '!') j+=1; |
| 417 | if (s[j] == '+') j+=1; |
| 418 | break; |
| 419 | default: |
| 420 | cerr << "Invalid argument string\n"; |
| 421 | return -1; |
| 422 | } |
| 423 | break; |
| 424 | case 'I': |
| 425 | if (s[j+1] != '(') |
| 426 | j+=1; |
| 427 | else |
| 428 | { |
| 429 | if (((s[j+2] != '?') && ((s[j+2] < 'a') || ('z' < s[j+2]))) || (s[j+3] != ')')) |
| 430 | return -1; |
| 431 | j+=4; |
| 432 | } |
| 433 | break; |
| 434 | case 'F': |
| 435 | j+=1; |
| 436 | break; |
| 437 | case 'V': |
| 438 | if ((s[j+1] != '(') || ((s[j+2] != '?') && ((s[j+2] < 'a') || ('z' < s[j+2]))) || (s[j+3] != ')')) |
| 439 | return -1; |
| 440 | if (s[j+=4] == '!') j+=1; |
| 441 | if (s[j] == '+') j+=1; |
| 442 | break; |
| 443 | case 'S': |
| 444 | if ((s[j+1] == 'P') && (s[j+2] == 'D')) j+=2; |
| 445 | case 'M': |
| 446 | case 'U': |
| 447 | case 'L': |
| 448 | if ((s[j+1] != '(') || ((s[j+2] != '?') && ((s[j+2] < 'a') || ('z' < s[j+2]))) || |
| 449 | (s[j+3] != ',') || ((s[j+4] != '?') && ((s[j+4] < 'a') || ('z' < s[j+4]))) || (s[j+5] != ')')) |
| 450 | return -1; |
| 451 | if (s[j+=6] == '!') j+=1; |
| 452 | if (s[j] == '+') j+=1; |
| 453 | break; |
| 454 | case 'P': |
| 455 | if ((s[j+1] != '(') || ((s[j+2] != '?') && ((s[j+2] < 'a') || ('z' < s[j+2]))) || (s[j+3] != ')')) |
| 456 | return -1; |
| 457 | if (s[j+=4] == '!') j+=1; |
| 458 | if (s[j] == '+') j+=1; |
| 459 | break; |
| 460 | default: |
| 461 | cerr << "Invalid argument string\n"; |
| 462 | return -1; |
| 463 | } |
| 464 | |
| 465 | nargs++; |
| 466 | |
| 467 | if (!s[j]) return nargs; |
| 468 | |
| 469 | if (s[j] == ';') |
| 470 | j+=1; |
| 471 | else |
| 472 | return -1; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | void SetSizeCharacter(char *s, int *j, TArg_sized *arg) |
| 477 | { |
| 478 | int i, n=arg->GetNumberSizes(); |
| 479 | for (i=0; i < n; i++) |
| 480 | arg->SetSizeChar(s[(*j)+=2],i); |
| 481 | (*j)+=2; |
| 482 | } |
| 483 | |
| 484 | void SetPointerInfo(char *s, int *j, TArg_pointer *arg) |
| 485 | { |
| 486 | if (s[*j] == '!') |
| 487 | { |
| 488 | arg->SetNullOK(1); |
| 489 | (*j)++; |
| 490 | } |
| 491 | if (s[*j] == '+') |
| 492 | { |
| 493 | arg->SetChangeOK(1); |
| 494 | (*j)++; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | int CreateArgs(char *s, TArg **args, int nargs) |
| 499 | { |
| 500 | int i, j; |
| 501 | |
| 502 | TArg_TVector *pV; |
| 503 | TArg_TMatrix *pM; |
| 504 | TArg_TMatrix_U *pU; |
| 505 | TArg_TMatrix_L *pL; |
| 506 | TArg_TMatrix_S *pS; |
| 507 | TArg_TMatrix_SPD *pSPD; |
| 508 | TArg_TPermutation *pP; |
| 509 | TArg_int_size *pIS; |
| 510 | TArg_pInteger *ppI; |
| 511 | |
| 512 | // Create each argument |
| 513 | for (j=i=0; i < nargs; i++) |
| 514 | { |
| 515 | switch (s[j]) |
| 516 | { |
| 517 | case 'p': |
| 518 | switch (s[++j]) |
| 519 | { |
| 520 | case 'I': |
| 521 | ppI=new TArg_pInteger(); |
| 522 | args[i]=static_cast<TArg*>(ppI); |
| 523 | SetSizeCharacter(s,&j,static_cast<TArg_sized*>(ppI)); |
| 524 | SetPointerInfo(s,&j,static_cast<TArg_pointer*>(ppI)); |
| 525 | break; |
| 526 | default: |
| 527 | cerr << "Invalid argument string\n"; |
| 528 | return 0; |
| 529 | } |
| 530 | break; |
| 531 | case 'I': |
| 532 | if (s[j+1] != '(') |
| 533 | { |
| 534 | args[i]=static_cast<TArg*>(new TArg_int()); |
| 535 | j++; |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | pIS=new TArg_int_size(); |
| 540 | args[i]=static_cast<TArg*>(pIS); |
| 541 | SetSizeCharacter(s,&j,static_cast<TArg_sized*>(pIS)); |
| 542 | } |
| 543 | break; |
| 544 | case 'F': |
| 545 | args[i]=static_cast<TArg*>(new TArg_PRECISION()); |
| 546 | j++; |
| 547 | break; |
| 548 | case 'V': |
| 549 | pV=new TArg_TVector(); |
| 550 | args[i]=static_cast<TArg*>(pV); |
| 551 | SetSizeCharacter(s,&j,static_cast<TArg_sized*>(pV)); |
| 552 | SetPointerInfo(s,&j,static_cast<TArg_pointer*>(pV)); |
| 553 | break; |
| 554 | case 'M': |
| 555 | pM=new TArg_TMatrix(); |
| 556 | args[i]=static_cast<TArg*>(pM); |
| 557 | SetSizeCharacter(s,&j,static_cast<TArg_sized*>(pM)); |
| 558 | SetPointerInfo(s,&j,static_cast<TArg_pointer*>(pM)); |
| 559 | break; |
| 560 | case 'U': |
| 561 | pU=new TArg_TMatrix_U(); |
| 562 | args[i]=static_cast<TArg*>(pU); |
| 563 | SetSizeCharacter(s,&j,static_cast<TArg_sized*>(pU)); |
| 564 | SetPointerInfo(s,&j,static_cast<TArg_pointer*>(pU)); |
| 565 | break; |
| 566 | case 'L': |
| 567 | pL=new TArg_TMatrix_L(); |
| 568 | args[i]=static_cast<TArg*>(pL); |
| 569 | SetSizeCharacter(s,&j,static_cast<TArg_sized*>(pL)); |
| 570 | SetPointerInfo(s,&j,static_cast<TArg_pointer*>(pL)); |
| 571 | break; |
| 572 | case 'S': |
| 573 | if ((s[j+1] == 'P') && (s[j+2] == 'D')) |
| 574 | { |
| 575 | j+=2; |
| 576 | pSPD=new TArg_TMatrix_SPD(); |
| 577 | args[i]=static_cast<TArg*>(pSPD); |
| 578 | SetSizeCharacter(s,&j,static_cast<TArg_sized*>(pSPD)); |
| 579 | SetPointerInfo(s,&j,static_cast<TArg_pointer*>(pSPD)); |
| 580 | } |
| 581 | else |
| 582 | { |
| 583 | pS=new TArg_TMatrix_S(); |
| 584 | args[i]=static_cast<TArg*>(pS); |
| 585 | SetSizeCharacter(s,&j,static_cast<TArg_sized*>(pS)); |
| 586 | SetPointerInfo(s,&j,static_cast<TArg_pointer*>(pS)); |
| 587 | } |
| 588 | break;; |
| 589 | case 'P': |
| 590 | pP=new TArg_TPermutation(); |
| 591 | args[i]=static_cast<TArg*>(pP); |
| 592 | SetSizeCharacter(s,&j,static_cast<TArg_sized*>(pP)); |
| 593 | SetPointerInfo(s,&j,static_cast<TArg_pointer*>(pP)); |
| 594 | break; |
| 595 | default: |
| 596 | cerr << "Invalid argument string\n"; |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | for (j++; s[j] && (s[j-1] != ';'); j++); |
| 601 | } |
| 602 | |
| 603 | return 1; |
| 604 | } |
| 605 | |
| 606 | TArgList::TArgList(char *string) |
| 607 | { |
| 608 | int ch[256], i, j, k; |
| 609 | TArg_sized *p; |
| 610 | |
| 611 | // default values |
| 612 | nargs=0; |
| 613 | args=(TArg**)NULL; |
| 614 | arg_string=(char*)NULL; |
| 615 | n_minimal_idx=0; |
| 616 | minimal_idx=(int*)NULL; |
| 617 | n_sizes=0; |
| 618 | sizes_idx=(int*)NULL; |
| 619 | correct_sizes=(int*)NULL; |
| 620 | actual_sizes=(int*)NULL; |
| 621 | |
| 622 | // No arguments |
| 623 | if (!string || !string[0]) return; |
| 624 | |
| 625 | // Check and copy argument string |
| 626 | if ((nargs=CheckArgString(string)) == -1) throw ERR_STRING_SYNTAX; |
| 627 | if (!(arg_string=(char*)dw_malloc((strlen(string)+1)*sizeof(char)))) throw ERR_MEM_FAILURE; |
| 628 | strcpy(arg_string,string); |
| 629 | |
| 630 | // Setup Arguments |
| 631 | if (!(args=(TArg**)dw_malloc(nargs*sizeof(TArg*)))) throw ERR_MEM_FAILURE; |
| 632 | for (i=0; i < nargs; i++) args[i]=(TArg*)NULL; |
| 633 | if (!CreateArgs(arg_string,args,nargs)) throw ERR_STRING_SYNTAX; |
| 634 | |
| 635 | // Set n_sizes, sizes_idx, correct_sizes, and actual_sizes |
| 636 | for (n_sizes=i=0; i < nargs; i++) |
| 637 | if ((p=dynamic_cast<TArg_sized*>(args[i]))) n_sizes+=p->GetNumberSizes(); |
| 638 | if (!(sizes_idx=(int*)dw_malloc(n_sizes*sizeof(int)))) throw ERR_MEM_FAILURE; |
| 639 | if (!(correct_sizes=(int*)dw_malloc(n_sizes*sizeof(int)))) throw ERR_MEM_FAILURE; |
| 640 | if (!(actual_sizes=(int*)dw_malloc(n_sizes*sizeof(int)))) throw ERR_MEM_FAILURE; |
| 641 | |
| 642 | // Set n_mininal_idx, minimal_idx |
| 643 | for (i=0; i < 256; i++) ch[i]=0; |
| 644 | |
| 645 | for (i=nargs-1; i >= 0; i--) |
| 646 | if (p=dynamic_cast<TArg_sized*>(args[i])) |
| 647 | for (j=p->GetNumberSizes()-1; j >= 0; j--) |
| 648 | if (p->GetSizeChar(j) != '?') ch[p->GetSizeChar(j)]++; |
| 649 | |
| 650 | for (n_minimal_idx=i=0; i < 256; i++) |
| 651 | ch[i]=(ch[i] > 0) ? n_minimal_idx++ : -1; |
| 652 | |
| 653 | if (!(minimal_idx=(int*)dw_malloc(n_minimal_idx*sizeof(int)))) throw ERR_MEM_FAILURE; |
| 654 | for (i=0; i < n_minimal_idx; i++) minimal_idx[i]=0; |
| 655 | |
| 656 | // Set size_idx values and size reference |
| 657 | for (k=i=0; i < nargs; i++) |
| 658 | if (p=dynamic_cast<TArg_sized*>(args[i])) |
| 659 | for (j=0; j < p->GetNumberSizes(); j++) |
| 660 | { |
| 661 | p->SetSizeReference(actual_sizes+k,j); |
| 662 | sizes_idx[k++]=ch[p->GetSizeChar(j)]; |
| 663 | } |
| 664 | |
| 665 | // Set tests_sizes |
| 666 | test_sizes=(int**)dw_malloc(n_minimal_idx*sizeof(int*)); |
| 667 | for (i=0; i < n_minimal_idx; i++) test_sizes[i]=(int*)NULL; |
| 668 | DefaultTestSizes(); |
| 669 | } |
| 670 | |
| 671 | TArgList::~TArgList() |
| 672 | { |
| 673 | if (minimal_idx) dw_free(minimal_idx); |
| 674 | if (sizes_idx) dw_free(sizes_idx); |
| 675 | if (correct_sizes) dw_free(correct_sizes); |
| 676 | if (actual_sizes) dw_free(actual_sizes); |
| 677 | if (test_sizes) |
| 678 | { |
| 679 | FreeTestSizes(); |
| 680 | dw_free(test_sizes); |
| 681 | } |
| 682 | if (args) |
| 683 | { |
| 684 | for (int i=0; i < nargs; i++) |
| 685 | if (args[i]) delete args[i]; |
| 686 | dw_free(args); |
| 687 | } |
| 688 | if (arg_string) dw_free(arg_string); |
| 689 | } |
| 690 | |
| 691 | void TArgList::FreeTestSizes(void) |
| 692 | { |
| 693 | for (int i=0; i < n_minimal_idx; i++) |
| 694 | if (test_sizes[i]) |
| 695 | { |
| 696 | dw_free(test_sizes[i]); |
| 697 | test_sizes[i]=(int*)NULL; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | void TArgList::DefaultTestSizes(void) |
| 702 | { |
| 703 | int ts[5]={1,2,3,7,10}; |
| 704 | SetTestSizes(5,ts); |
| 705 | } |
| 706 | |
| 707 | void TArgList::SetTestSizes(int **ts) |
| 708 | { |
| 709 | if (ts) |
| 710 | { |
| 711 | FreeTestSizes(); |
| 712 | for (int i=0; i < n_minimal_idx; i++) |
| 713 | { |
| 714 | if (!(test_sizes[i]=(int*)dw_malloc((ts[i][0]+1)*sizeof(int)))) throw ERR_MEM_FAILURE; |
| 715 | memcpy(test_sizes[i],ts[i],(ts[i][0]+1)*sizeof(int)); |
| 716 | } |
| 717 | } |
| 718 | else |
| 719 | throw ERR_NULL_TEST_SIZES; |
| 720 | } |
| 721 | |
| 722 | void TArgList::SetTestSizes(int nts, int* ts) |
| 723 | { |
| 724 | if ((nts > 0) && ts) |
| 725 | { |
| 726 | FreeTestSizes(); |
| 727 | for (int i=0; i < n_minimal_idx; i++) |
| 728 | { |
| 729 | if (!(test_sizes[i]=(int*)dw_malloc((nts+1)*sizeof(int)))) throw ERR_MEM_FAILURE; |
| 730 | test_sizes[i][0]=nts; |
| 731 | memcpy(test_sizes[i]+1,ts,nts*sizeof(int)); |
| 732 | } |
| 733 | } |
| 734 | else |
| 735 | throw ERR_NULL_TEST_SIZES; |
| 736 | } |
| 737 | |
| 738 | void TArgList::SetTestSizes(int min, int max) |
| 739 | { |
| 740 | if ((min <= 0) || (max < min)) throw ERR_NULL_TEST_SIZES; |
| 741 | int *ts=(int*)dw_malloc((max-min+1)*sizeof(int)); |
| 742 | if (!ts) throw ERR_MEM_FAILURE; |
| 743 | for (int i=min; i <= max; i++) ts[i-min]=i; |
| 744 | SetTestSizes(max-min+1,ts); |
| 745 | dw_free(ts); |
| 746 | } |
| 747 | |
| 748 | int TArgList::ResetMinimalIndexes(void) |
| 749 | { |
| 750 | for (int i=0; i < n_minimal_idx; i++) minimal_idx[i]=1; |
| 751 | return 1; |
| 752 | } |
| 753 | |
| 754 | int TArgList::IncrementMinimalIndexes(void) |
| 755 | { |
| 756 | for (int i=0; i < n_minimal_idx; i++) |
| 757 | if (minimal_idx[i] == test_sizes[i][0]) |
| 758 | minimal_idx[i]=1; |
| 759 | else |
| 760 | { |
| 761 | minimal_idx[i]++; |
| 762 | return 1; |
| 763 | } |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | void TArgList::ComputeCorrectSizes() |
| 768 | { |
| 769 | for (int i=0; i < n_sizes; i++) |
| 770 | correct_sizes[i]=(sizes_idx[i] >= 0) ? test_sizes[sizes_idx[i]][minimal_idx[sizes_idx[i]]] : -1; |
| 771 | } |
| 772 | |
| 773 | void TArgList::MakeSizeInvalid(int i) |
| 774 | { |
| 775 | if (actual_sizes[i] == 1) |
| 776 | actual_sizes[i]=(rand() % 2) + 2; |
| 777 | else |
| 778 | if (actual_sizes[i] == 2) |
| 779 | actual_sizes[i]=(rand() % 2) ? 1 : 3; |
| 780 | else |
| 781 | actual_sizes[i]+=(rand() % 2) ? -((rand() % 2) + 1) : (rand() % 2) + 1; |
| 782 | } |
| 783 | |
| 784 | void TArgList::Allocate(void) |
| 785 | { |
| 786 | for (int i=nargs-1; i >= 0; i--) args[i]->Allocate(); |
| 787 | } |
| 788 | |
| 789 | void TArgList::Initialize(void) |
| 790 | { |
| 791 | for (int i=nargs-1; i >= 0; i--) args[i]->Initialize(); |
| 792 | } |
| 793 | |
| 794 | int TArgList::SizesOK(void) |
| 795 | { |
| 796 | int i, j; |
| 797 | for (i=n_sizes-1; i >= 0; i--) |
| 798 | if (correct_sizes[i] != actual_sizes[i]) |
| 799 | if (sizes_idx[i] != -1) |
| 800 | for (j=n_sizes-1; j >= 0; j--) |
| 801 | if ((sizes_idx[i] == sizes_idx[j]) && (actual_sizes[i] != actual_sizes[j])) |
| 802 | return 0; |
| 803 | return 1; |
| 804 | } |
| 805 | |
| 806 | int TArgList::NullOK(void) |
| 807 | { |
| 808 | TArg_pointer *p; |
| 809 | for (int i=nargs-1; i >= 0; i--) |
| 810 | if ((p=dynamic_cast<TArg_pointer*>(args[i])) && !p->address() && !p->IsNullOK()) |
| 811 | return 0; |
| 812 | return 1; |
| 813 | } |
| 814 | |
| 815 | int TArgList::ArgChanged(void) |
| 816 | { |
| 817 | int i, j; |
| 818 | TArg_pointer *p, *q; |
| 819 | for (i=nargs-1; i >= 0; i--) |
| 820 | if ((p=dynamic_cast<TArg_pointer*>(args[i])) && p->address() && !p->IsChangeOK() && p->Owner()) |
| 821 | { |
| 822 | for (j=nargs-1; j >= 0; j--) |
| 823 | if ((j != i) && (q=dynamic_cast<TArg_pointer*>(args[j])) && (q->address() == p->address()) && q->IsChangeOK()) break; |
| 824 | if ((j < 0) && p->Changed()) return 1; |
| 825 | } |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | #define ARG1(i,j) dynamic_cast<TArg_sized*>(args[i])->GetSize(j) |
| 830 | #define ARG2(type,i) dynamic_cast<type*>(args[i])->GetArg() |
| 831 | #define ARG3(i) dynamic_cast<TArg_pointer*>(args[i]) |
| 832 | void TArgList::PrintArgList(ostream &out) |
| 833 | { |
| 834 | TArg_pointer *p, *q; |
| 835 | int i, j; |
| 836 | out << '('; |
| 837 | for (i=0; i < nargs; i++) |
| 838 | { |
| 839 | switch(args[i]->type()) |
| 840 | { |
| 841 | case dw_pInteger_type: |
| 842 | p=ARG3(i); |
| 843 | if (!p->address()) |
| 844 | out << "null"; |
| 845 | else |
| 846 | if (p->Owner()) |
| 847 | out << "pI" << i << '(' << ARG1(i,0) << ')'; |
| 848 | else |
| 849 | { |
| 850 | for (j=0; j < nargs; j++) |
| 851 | if ((j != i) && (q=ARG3(j)) && (p->address() == q->address())) |
| 852 | { |
| 853 | out << "pI" << j << '(' << ARG1(i,0) << ')'; |
| 854 | break; |
| 855 | } |
| 856 | if (j == nargs) |
| 857 | out << "pI?(" << ARG1(i,0) << ')'; |
| 858 | } |
| 859 | break; |
| 860 | case dw_int_type: |
| 861 | out << ARG2(TArg_int,i); |
| 862 | break; |
| 863 | case dw_int_size_type: |
| 864 | out << ARG2(TArg_int_size,i); |
| 865 | break; |
| 866 | case dw_PRECISION_type: |
| 867 | out << ARG2(TArg_PRECISION,i); |
| 868 | break; |
| 869 | case dw_TVector_type: |
| 870 | p=ARG3(i); |
| 871 | if (!p->address()) |
| 872 | out << "null"; |
| 873 | else |
| 874 | if (p->Owner()) |
| 875 | out << 'V' << i << '(' << ARG1(i,0) << ')'; |
| 876 | else |
| 877 | { |
| 878 | for (j=0; j < nargs; j++) |
| 879 | if ((j != i) && (q=ARG3(j)) && (p->address() == q->address())) |
| 880 | { |
| 881 | out << 'V' << j << '(' << ARG1(i,0) << ')'; |
| 882 | break; |
| 883 | } |
| 884 | if (j == nargs) |
| 885 | out << "V?(" << ARG1(i,0) << ')'; |
| 886 | } |
| 887 | break; |
| 888 | case dw_TMatrix_type: |
| 889 | p=ARG3(i); |
| 890 | if (!p->address()) |
| 891 | out << "null"; |
| 892 | else |
| 893 | if (p->Owner()) |
| 894 | out << 'M' << i << '(' << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 895 | else |
| 896 | { |
| 897 | for (j=0; j < nargs; j++) |
| 898 | if ((q=ARG3(j)) && q->Owner() && (p->address() == q->address())) |
| 899 | { |
| 900 | out << 'M' << j << '(' << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 901 | break; |
| 902 | } |
| 903 | if (j == nargs) |
| 904 | out << "M?(" << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 905 | } |
| 906 | break; |
| 907 | case dw_TMatrix_U_type: |
| 908 | p=ARG3(i); |
| 909 | if (!p->address()) |
| 910 | out << "null"; |
| 911 | else |
| 912 | if (p->Owner()) |
| 913 | out << 'U' << i << '(' << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 914 | else |
| 915 | { |
| 916 | for (j=0; j < nargs; j++) |
| 917 | if ((j != i) && (q=ARG3(j)) && (p->address() == q->address())) |
| 918 | { |
| 919 | out << 'U' << j << '(' << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 920 | break; |
| 921 | } |
| 922 | if (j == nargs) |
| 923 | out << "U?(" << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 924 | } |
| 925 | break; |
| 926 | case dw_TMatrix_L_type: |
| 927 | p=ARG3(i); |
| 928 | if (!p->address()) |
| 929 | out << "null"; |
| 930 | else |
| 931 | if (p->Owner()) |
| 932 | out << 'L' << i << '(' << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 933 | else |
| 934 | { |
| 935 | for (j=0; j < nargs; j++) |
| 936 | if ((j != i) && (q=ARG3(j)) && (p->address() == q->address())) |
| 937 | { |
| 938 | out << 'L' << j << '(' << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 939 | break; |
| 940 | } |
| 941 | if (j == nargs) |
| 942 | out << "L?(" << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 943 | } |
| 944 | break; |
| 945 | case dw_TMatrix_SPD_type: |
| 946 | p=ARG3(i); |
| 947 | if (!p->address()) |
| 948 | out << "null"; |
| 949 | else |
| 950 | if (p->Owner()) |
| 951 | out << "SPD" << i << '(' << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 952 | else |
| 953 | { |
| 954 | for (j=0; j < nargs; j++) |
| 955 | if ((j != i) && (q=ARG3(j)) && (p->address() == q->address())) |
| 956 | { |
| 957 | out << 'L' << j << '(' << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 958 | break; |
| 959 | } |
| 960 | if (j == nargs) |
| 961 | out << "L?(" << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 962 | } |
| 963 | break; |
| 964 | case dw_TMatrix_S_type: |
| 965 | p=ARG3(i); |
| 966 | if (!p->address()) |
| 967 | out << "null"; |
| 968 | else |
| 969 | if (p->Owner()) |
| 970 | out << 'S' << i << '(' << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 971 | else |
| 972 | { |
| 973 | for (j=0; j < nargs; j++) |
| 974 | if ((j != i) && (q=ARG3(j)) && (p->address() == q->address())) |
| 975 | { |
| 976 | out << 'L' << j << '(' << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 977 | break; |
| 978 | } |
| 979 | if (j == nargs) |
| 980 | out << "L?(" << ARG1(i,0) << ',' << ARG1(i,1) << ')'; |
| 981 | } |
| 982 | break; |
| 983 | case dw_TPermutation_type: |
| 984 | p=ARG3(i); |
| 985 | if (!p->address()) |
| 986 | out << "null"; |
| 987 | else |
| 988 | if (p->Owner()) |
| 989 | out << 'P' << i << '(' << ARG1(i,0) << ')'; |
| 990 | else |
| 991 | { |
| 992 | for (j=0; j < nargs; j++) |
| 993 | if ((j != i) && (q=ARG3(j)) && (p->address() == q->address())) |
| 994 | { |
| 995 | out << 'P' << j << '(' << ARG1(i,0) << ')'; |
| 996 | break; |
| 997 | } |
| 998 | if (j == nargs) |
| 999 | out << "P?(" << ARG1(i,0) << ')'; |
| 1000 | } |
| 1001 | break; |
| 1002 | default: |
| 1003 | throw ERR_ARG_TYPE; |
| 1004 | } |
| 1005 | if (i < nargs-1) out << ','; |
| 1006 | } |
| 1007 | out << ')'; |
| 1008 | } |
| 1009 | #undef ARG1 |
| 1010 | #undef ARG2 |
| 1011 | #undef ARG3 |
| 1012 | |
| 1013 | //=== TArgList ========================================================== end === |
| 1014 | |
| 1015 | |
| 1016 | //=== TArg_sized ====================================================== begin === |
| 1017 | TArg_sized::TArg_sized(int n) |
| 1018 | { |
| 1019 | n_sizes=n; |
| 1020 | if (!(size=(int**)dw_malloc(n*sizeof(int*)))) |
| 1021 | throw ERR_MEM_FAILURE; |
| 1022 | if (!(size_char=(unsigned char*)dw_malloc(n*sizeof(unsigned char)))) |
| 1023 | throw ERR_MEM_FAILURE; |
| 1024 | for (int i=0; i < n; i++) |
| 1025 | { |
| 1026 | size[i]=(int*)NULL; |
| 1027 | size_char[i]='?'; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | TArg_sized::~TArg_sized() |
| 1032 | { |
| 1033 | if (size) dw_free(size); |
| 1034 | if (size_char) dw_free(size_char); |
| 1035 | } |
| 1036 | //=== TArg_sized ======================================================== end === |
| 1037 | |
| 1038 | |
| 1039 | //=== TArg_TMatrix ==================================================== begin === |
| 1040 | void TArg_TMatrix::Free(void) |
| 1041 | { |
| 1042 | if (owner) |
| 1043 | { |
| 1044 | if (arg) FreeMatrix(arg); |
| 1045 | if (copy) FreeMatrix(copy); |
| 1046 | } |
| 1047 | arg=(TMatrix)NULL; |
| 1048 | copy=(TMatrix)NULL; |
| 1049 | owner=0; |
| 1050 | } |
| 1051 | |
| 1052 | void TArg_TMatrix::CreateMatrices(int row, int col) |
| 1053 | { |
| 1054 | if (owner && arg) |
| 1055 | { |
| 1056 | if ((RowM(arg) != row) || (ColM(arg) != col)) |
| 1057 | { |
| 1058 | FreeMatrix(arg); |
| 1059 | FreeMatrix(copy); |
| 1060 | arg=CreateMatrix(row,col); |
| 1061 | copy=CreateMatrix(row,col); |
| 1062 | } |
| 1063 | } |
| 1064 | else |
| 1065 | { |
| 1066 | arg=CreateMatrix(row,col); |
| 1067 | copy=CreateMatrix(row,col); |
| 1068 | } |
| 1069 | owner=1; |
| 1070 | } |
| 1071 | |
| 1072 | void TArg_TMatrix::EquatePointers(TArg_pointer* p_arg) |
| 1073 | { |
| 1074 | TArg_TMatrix *p=dynamic_cast<TArg_TMatrix*>(p_arg); |
| 1075 | if (p) |
| 1076 | { |
| 1077 | Free(); |
| 1078 | arg=p->arg; |
| 1079 | copy=p->copy; |
| 1080 | owner=0; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | int TArg_TMatrix::Changed(void) |
| 1085 | { |
| 1086 | if (arg) |
| 1087 | { |
| 1088 | int i; |
| 1089 | PRECISION *a=pElementM(arg), *c=pElementM(copy); |
| 1090 | for (i=RowM(arg)*ColM(arg)-1; i >= 0; i--) |
| 1091 | if (a[i] != c[i]) return 1; |
| 1092 | } |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | int TArg_TMatrix::GetSize(int i) |
| 1097 | { |
| 1098 | if (i == 0) |
| 1099 | return arg ? RowM(arg) : 0; |
| 1100 | else |
| 1101 | if (i == 1) |
| 1102 | return arg ? ColM(arg) : 0; |
| 1103 | else |
| 1104 | throw ERR_INDEX_OUT_OF_RANGE; |
| 1105 | } |
| 1106 | //=== TArg_TMatrix ====================================================== end === |
| 1107 | |
| 1108 | //=== TArg_TMatrix_U ================================================== begin === |
| 1109 | void TArg_TMatrix_U::Initialize(void) |
| 1110 | { |
| 1111 | if (arg && owner) |
| 1112 | { |
| 1113 | int i, j; |
| 1114 | dw_NormalMatrix(arg); |
| 1115 | for (i=RowM(arg)-1; i > 0; i--) |
| 1116 | for (j=(ColM(arg) < i) ? ColM(arg)-1 : i-1; j >= 0; j--) |
| 1117 | ElementM(arg,i,j)=0.0; |
| 1118 | EquateMatrix(copy,arg); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | void TArg_TMatrix_U::EquatePointers(TArg_pointer* p_arg) |
| 1123 | { |
| 1124 | TArg_TMatrix_U *p=dynamic_cast<TArg_TMatrix_U*>(p_arg); |
| 1125 | if (p) |
| 1126 | { |
| 1127 | Free(); |
| 1128 | arg=p->arg; |
| 1129 | copy=p->copy; |
| 1130 | owner=0; |
| 1131 | } |
| 1132 | } |
| 1133 | //=== TArg_TMatrix_U ==================================================== end === |
| 1134 | |
| 1135 | //=== TArg_TMatrix_L ================================================== begin === |
| 1136 | void TArg_TMatrix_L::Initialize(void) |
| 1137 | { |
| 1138 | if (arg && owner) |
| 1139 | { |
| 1140 | int i, j; |
| 1141 | dw_NormalMatrix(arg); |
| 1142 | for (j=ColM(arg)-1; j > 0; j--) |
| 1143 | for (i=(RowM(arg) < j) ? RowM(arg)-1 : j-1; i >= 0; i--) |
| 1144 | ElementM(arg,i,j)=0.0; |
| 1145 | EquateMatrix(copy,arg); |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | void TArg_TMatrix_L::EquatePointers(TArg_pointer* p_arg) |
| 1150 | { |
| 1151 | TArg_TMatrix_L *p=dynamic_cast<TArg_TMatrix_L*>(p_arg); |
| 1152 | if (p) |
| 1153 | { |
| 1154 | Free(); |
| 1155 | arg=p->arg; |
| 1156 | copy=p->copy; |
| 1157 | owner=0; |
| 1158 | } |
| 1159 | } |
| 1160 | //=== TArg_TMatrix_L ==================================================== end === |
| 1161 | |
| 1162 | //=== TArg_TMatrix_SPD ================================================ begin === |
| 1163 | void TArg_TMatrix_SPD::Initialize(void) |
| 1164 | { |
| 1165 | if (arg && owner) |
| 1166 | { |
| 1167 | dw_NormalMatrix(arg); |
| 1168 | if (RowM(arg) == ColM(arg)) |
| 1169 | TransposeProductMM(arg,arg,arg); |
| 1170 | EquateMatrix(copy,arg); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | void TArg_TMatrix_SPD::EquatePointers(TArg_pointer* p_arg) |
| 1175 | { |
| 1176 | TArg_TMatrix_SPD *p=dynamic_cast<TArg_TMatrix_SPD*>(p_arg); |
| 1177 | if (p) |
| 1178 | { |
| 1179 | Free(); |
| 1180 | arg=p->arg; |
| 1181 | copy=p->copy; |
| 1182 | owner=0; |
| 1183 | } |
| 1184 | } |
| 1185 | //=== TArg_TMatrix_SPD ================================================== end === |
| 1186 | |
| 1187 | //=== TArg_TMatrix_S ================================================== begin === |
| 1188 | void TArg_TMatrix_S::Initialize(void) |
| 1189 | { |
| 1190 | if (arg && owner) |
| 1191 | { |
| 1192 | int i, j; |
| 1193 | dw_NormalMatrix(arg); |
| 1194 | if (RowM(arg) == ColM(arg)) |
| 1195 | for (j=ColM(arg)-1; j > 0; j--) |
| 1196 | for (i=j-1; i >= 0; i--) |
| 1197 | ElementM(arg,i,j)=ElementM(arg,j,i); |
| 1198 | EquateMatrix(copy,arg); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | void TArg_TMatrix_S::EquatePointers(TArg_pointer* p_arg) |
| 1203 | { |
| 1204 | TArg_TMatrix_S *p=dynamic_cast<TArg_TMatrix_S*>(p_arg); |
| 1205 | if (p) |
| 1206 | { |
| 1207 | Free(); |
| 1208 | arg=p->arg; |
| 1209 | copy=p->copy; |
| 1210 | owner=0; |
| 1211 | } |
| 1212 | } |
| 1213 | //=== TArg_TMatrix_S ==================================================== end === |
| 1214 | |
| 1215 | //=== TArg_TVector ==================================================== begin === |
| 1216 | void TArg_TVector::Free(void) |
| 1217 | { |
| 1218 | if (owner) |
| 1219 | { |
| 1220 | if (arg) FreeVector(arg); |
| 1221 | if (copy) FreeVector(copy); |
| 1222 | |
| 1223 | } |
| 1224 | arg=(TVector)NULL; |
| 1225 | copy=(TVector)NULL; |
| 1226 | owner=0; |
| 1227 | } |
| 1228 | |
| 1229 | void TArg_TVector::CreateVectors(int dim) |
| 1230 | { |
| 1231 | if (owner && arg) |
| 1232 | { |
| 1233 | if (DimV(arg) != dim) |
| 1234 | { |
| 1235 | FreeVector(arg); |
| 1236 | FreeVector(copy); |
| 1237 | arg=CreateVector(dim); |
| 1238 | copy=CreateVector(dim); |
| 1239 | } |
| 1240 | } |
| 1241 | else |
| 1242 | { |
| 1243 | arg=CreateVector(dim); |
| 1244 | copy=CreateVector(dim); |
| 1245 | } |
| 1246 | owner=1; |
| 1247 | } |
| 1248 | |
| 1249 | void TArg_TVector::EquatePointers(TArg_pointer* p_arg) |
| 1250 | { |
| 1251 | TArg_TVector *p=dynamic_cast<TArg_TVector*>(p_arg); |
| 1252 | if (p) |
| 1253 | { |
| 1254 | Free(); |
| 1255 | arg=p->arg; |
| 1256 | copy=p->copy; |
| 1257 | owner=0; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | int TArg_TVector::Changed(void) |
| 1262 | { |
| 1263 | if (arg) |
| 1264 | { |
| 1265 | int i; |
| 1266 | PRECISION *a=pElementV(arg), *c=pElementV(copy); |
| 1267 | for (i=DimV(arg)-1; i >= 0; i--) |
| 1268 | if (a[i] != c[i]) return 1; |
| 1269 | } |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | int TArg_TVector::GetSize(int i) |
| 1274 | { |
| 1275 | if (i == 0) |
| 1276 | return arg ? DimV(arg) : 0; |
| 1277 | else |
| 1278 | throw ERR_INDEX_OUT_OF_RANGE; |
| 1279 | } |
| 1280 | //=== TArg_TVector ====================================================== end === |
| 1281 | |
| 1282 | |
| 1283 | //=== TArg_TPermutation =============================================== begin === |
| 1284 | void TArg_TPermutation::Free(void) |
| 1285 | { |
| 1286 | if (owner) |
| 1287 | { |
| 1288 | if (arg) FreePermutation(arg); |
| 1289 | if (copy) FreePermutation(copy); |
| 1290 | |
| 1291 | } |
| 1292 | arg=(TPermutation)NULL; |
| 1293 | copy=(TPermutation)NULL; |
| 1294 | owner=0; |
| 1295 | } |
| 1296 | |
| 1297 | void TArg_TPermutation::CreatePermutations(int dim) |
| 1298 | { |
| 1299 | if (owner && arg) |
| 1300 | { |
| 1301 | if (DimP(arg) != dim) |
| 1302 | { |
| 1303 | FreePermutation(arg); |
| 1304 | FreePermutation(copy); |
| 1305 | arg=CreatePermutation(dim); |
| 1306 | copy=CreatePermutation(dim); |
| 1307 | } |
| 1308 | } |
| 1309 | else |
| 1310 | { |
| 1311 | arg=CreatePermutation(dim); |
| 1312 | copy=CreatePermutation(dim); |
| 1313 | } |
| 1314 | owner=1; |
| 1315 | } |
| 1316 | |
| 1317 | void TArg_TPermutation::Initialize(void) |
| 1318 | { |
| 1319 | if (arg && owner) |
| 1320 | { |
| 1321 | UseP(arg)=rand() % DimP(arg); |
| 1322 | for (int i=UseP(arg)-1; i >= 0; i--) |
| 1323 | ElementP(arg,i)=i + (rand() % (DimP(arg)-i)); |
| 1324 | EquatePermutation(copy,arg); |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | void TArg_TPermutation::EquatePointers(TArg_pointer* p_arg) |
| 1329 | { |
| 1330 | TArg_TPermutation *p=dynamic_cast<TArg_TPermutation*>(p_arg); |
| 1331 | if (p) |
| 1332 | { |
| 1333 | Free(); |
| 1334 | arg=p->arg; |
| 1335 | copy=p->copy; |
| 1336 | owner=0; |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | int TArg_TPermutation::Changed(void) |
| 1341 | { |
| 1342 | if (arg) |
| 1343 | { |
| 1344 | if (UseP(arg) != UseP(copy)) return 1; |
| 1345 | int i, *a=pElementP(arg), *c=pElementP(copy); |
| 1346 | for (i=UseP(arg)-1; i >= 0; i--) |
| 1347 | if (a[i] != c[i]) return 1; |
| 1348 | } |
| 1349 | return 0; |
| 1350 | } |
| 1351 | |
| 1352 | int TArg_TPermutation::GetSize(int i) |
| 1353 | { |
| 1354 | if (i == 0) |
| 1355 | return arg ? DimP(arg) : 0; |
| 1356 | else |
| 1357 | throw ERR_INDEX_OUT_OF_RANGE; |
| 1358 | } |
| 1359 | //=== TArg_TPermutation ================================================= end === |
| 1360 | |
| 1361 | |
| 1362 | //=== TArg_pInteger =================================================== begin === |
| 1363 | void TArg_pInteger::Free(void) |
| 1364 | { |
| 1365 | if (owner) |
| 1366 | { |
| 1367 | if (arg) dw_free(arg); |
| 1368 | if (copy) dw_free(copy); |
| 1369 | } |
| 1370 | arg=(int*)NULL; |
| 1371 | copy=(int*)NULL; |
| 1372 | n=0; |
| 1373 | owner=0; |
| 1374 | } |
| 1375 | |
| 1376 | void TArg_pInteger::CreateIntegerArray(int dim) |
| 1377 | { |
| 1378 | if (owner && arg) |
| 1379 | { |
| 1380 | if (n != dim) |
| 1381 | { |
| 1382 | dw_free(arg); |
| 1383 | dw_free(copy); |
| 1384 | arg=(int*)dw_malloc(dim*sizeof(int)); |
| 1385 | copy=(int*)dw_malloc(dim*sizeof(int)); |
| 1386 | } |
| 1387 | } |
| 1388 | else |
| 1389 | { |
| 1390 | arg=(int*)dw_malloc(dim*sizeof(int)); |
| 1391 | copy=(int*)dw_malloc(dim*sizeof(int)); |
| 1392 | } |
| 1393 | n=dim; |
| 1394 | owner=1; |
| 1395 | } |
| 1396 | |
| 1397 | void TArg_pInteger::Initialize(void) |
| 1398 | { |
| 1399 | if (arg && owner) |
| 1400 | { |
| 1401 | for (int i=0; i < n; i++) |
| 1402 | copy[i]=arg[i]=(rand() % 10)+1; |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | void TArg_pInteger::EquatePointers(TArg_pointer* p_arg) |
| 1407 | { |
| 1408 | TArg_pInteger *p=dynamic_cast<TArg_pInteger*>(p_arg); |
| 1409 | if (p) |
| 1410 | { |
| 1411 | Free(); |
| 1412 | arg=p->arg; |
| 1413 | copy=p->copy; |
| 1414 | n=p->GetSize(0); |
| 1415 | owner=0; |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | int TArg_pInteger::Changed(void) |
| 1420 | { |
| 1421 | if (arg) |
| 1422 | { |
| 1423 | for (int i=n-1; i >= 0; i--) |
| 1424 | if (arg[i] != copy[i]) return 1; |
| 1425 | } |
| 1426 | return 0; |
| 1427 | } |
| 1428 | //=== TArg_pInteger ===================================================== end === |
| 1429 | |
| 1430 | |
| 1431 | //=== TFunction ======================================================= begin === |
| 1432 | TFunction::TFunction(char* name_string, char *rtrn_string, char* arg_string) |
| 1433 | : TArgList(arg_string) |
| 1434 | { |
| 1435 | rtrn=(TReturn*)NULL; |
| 1436 | inc=(TArgInc*)NULL; |
| 1437 | name=(char*)NULL; |
| 1438 | default_test_sizes=(int**)NULL; |
| 1439 | |
| 1440 | // Setup rtrn |
| 1441 | if (!rtrn_string || !rtrn_string[0]) |
| 1442 | rtrn=new TReturn_void(); |
| 1443 | else |
| 1444 | switch (rtrn_string[0]) |
| 1445 | { |
| 1446 | case 'p': |
| 1447 | cerr << "Not yet implemented\n"; |
| 1448 | throw ERR_STRING_SYNTAX; |
| 1449 | case 'I': |
| 1450 | rtrn=static_cast<TReturn*>(new TReturn_int()); |
| 1451 | break; |
| 1452 | case 'F': |
| 1453 | rtrn=static_cast<TReturn*>(new TReturn_PRECISION()); |
| 1454 | break; |
| 1455 | case 'V': |
| 1456 | rtrn=static_cast<TReturn*>(new TReturn_TVector()); |
| 1457 | break; |
| 1458 | case 'M': |
| 1459 | rtrn=static_cast<TReturn*>(new TReturn_TMatrix()); |
| 1460 | break; |
| 1461 | case 'P': |
| 1462 | rtrn=static_cast<TReturn*>(new TReturn_TPermutation()); |
| 1463 | break; |
| 1464 | default: |
| 1465 | cerr << "Invalid return string\n"; |
| 1466 | throw ERR_STRING_SYNTAX; |
| 1467 | } |
| 1468 | |
| 1469 | // Copy name |
| 1470 | if (!(name=(char*)dw_malloc((strlen(name_string)+1)*sizeof(char)))) throw ERR_MEM_FAILURE; |
| 1471 | strcpy(name,name_string); |
| 1472 | } |
| 1473 | |
| 1474 | TFunction::~TFunction() |
| 1475 | { |
| 1476 | if (name) dw_free(name); |
| 1477 | if (rtrn) delete rtrn; |
| 1478 | if (inc) delete inc; |
| 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | Returns 0 if all arguments are OK |
| 1483 | Returns 1 if there is a size error |
| 1484 | Returns 2 if there is a NULL error |
| 1485 | */ |
| 1486 | int TFunction::ArgumentsError(void) |
| 1487 | { |
| 1488 | int err=0; |
| 1489 | |
| 1490 | if (args) |
| 1491 | { |
| 1492 | // Sizes OK |
| 1493 | if (!SizesOK()) err|=ARG_ERR_SIZE; |
| 1494 | |
| 1495 | // NULL pointers OK |
| 1496 | if (!NullOK()) err|=ARG_ERR_NULL; |
| 1497 | } |
| 1498 | |
| 1499 | return err; |
| 1500 | } |
| 1501 | |
| 1502 | void TFunction::SetReturnOwner(void) |
| 1503 | { |
| 1504 | TReturn_pointer *p=dynamic_cast<TReturn_pointer*>(rtrn); |
| 1505 | if (p) |
| 1506 | { |
| 1507 | void* ptr=p->address(); |
| 1508 | if (ptr) |
| 1509 | { |
| 1510 | TArg_pointer *q; |
| 1511 | if (args) |
| 1512 | for (int i=nargs-1; i >= 0; i--) |
| 1513 | if ((q=dynamic_cast<TArg_pointer*>(arg(i))) && (ptr == q->address())) |
| 1514 | { |
| 1515 | p->SetOwner(0); |
| 1516 | return; |
| 1517 | } |
| 1518 | p->SetOwner(1); |
| 1519 | } |
| 1520 | else |
| 1521 | p->SetOwner(0); |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | int TFunction::TestBasic(int min, int max) |
| 1526 | { |
| 1527 | if (inc) delete inc; |
| 1528 | inc=((min <= 0) || (max < min)) ? new TArgInc_Basic(this) : new TArgInc_Basic(this,min,max); |
| 1529 | return First(); |
| 1530 | } |
| 1531 | |
| 1532 | int TFunction::TestInvalidSizes(int min, int max) |
| 1533 | { |
| 1534 | if (inc) delete inc; |
| 1535 | inc=((min <= 0) || (max < min)) ? new TArgInc_InvalidSizes(this) : new TArgInc_InvalidSizes(this,min,max); |
| 1536 | return First(); |
| 1537 | } |
| 1538 | |
| 1539 | int TFunction::TestInvalidNulls(int min, int max) |
| 1540 | { |
| 1541 | if (inc) delete inc; |
| 1542 | inc=((min <= 0) || (max < min)) ? new TArgInc_InvalidNulls(this) : new TArgInc_InvalidNulls(this,min,max); |
| 1543 | return First(); |
| 1544 | } |
| 1545 | |
| 1546 | int TFunction::TestEqualPointers(int min, int max) |
| 1547 | { |
| 1548 | if (inc) delete inc; |
| 1549 | inc=((min <= 0) || (max < min)) ? new TArgInc_EqualPointers(this) : new TArgInc_EqualPointers(this,min,max); |
| 1550 | return First(); |
| 1551 | } |
| 1552 | |
| 1553 | int TFunction::TestSpeed(int size) |
| 1554 | { |
| 1555 | if (inc) delete inc; |
| 1556 | if (size <= 0) size=10; |
| 1557 | inc=new TArgInc_Speed(this,1,&size); |
| 1558 | return First(); |
| 1559 | } |
| 1560 | //=== TFunction ========================================================= end === |
| 1561 |