1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include "scmatrix.hxx"
#include "global.hxx"
#include "address.hxx"
#include "formula/errorcodes.hxx"
#include "interpre.hxx"
#include <svl/zforlist.hxx>
#include <tools/stream.hxx>
#include <rtl/math.hxx>
#include <math.h>
#define MDDS_HASH_CONTAINER_BOOST 1
#include <mdds/mixed_type_matrix.hpp>
using ::std::pair;
using ::std::for_each;
using ::std::count_if;
using ::std::advance;
using ::std::unary_function;
using ::mdds::matrix_element_t;
// ============================================================================
namespace {
typedef ::mdds::mixed_type_matrix<String, sal_uInt8> MatrixImplType;
struct ElemEqual : public unary_function<double, bool>
{
bool operator() (double val) const
{
return val == 0.0;
}
};
struct ElemNotEqual : public unary_function<double, bool>
{
bool operator() (double val) const
{
return val != 0.0;
}
};
struct ElemGreater : public unary_function<double, bool>
{
bool operator() (double val) const
{
return val > 0.0;
}
};
struct ElemLess : public unary_function<double, bool>
{
bool operator() (double val) const
{
return val < 0.0;
}
};
struct ElemGreaterEqual : public unary_function<double, bool>
{
bool operator() (double val) const
{
return val >= 0.0;
}
};
struct ElemLessEqual : public unary_function<double, bool>
{
bool operator() (double val) const
{
return val <= 0.0;
}
};
template<typename _Comp>
void compareMatrix(MatrixImplType& rMat)
{
pair<size_t,size_t> aDim = rMat.size();
_Comp aComp;
for (size_t i = 0; i < aDim.first; ++i)
{
for (size_t j = 0; j < aDim.second; ++j)
{
matrix_element_t eType = rMat.get_type(i, j);
if (eType != mdds::element_numeric && eType != mdds::element_boolean)
// must be of numeric type (boolean can be numeric).
continue;
double fVal = rMat.get_numeric(i, j);
if (!::rtl::math::isFinite(fVal))
continue;
rMat.set_boolean(i, j, aComp(fVal));
}
}
}
::mdds::matrix_density_t toMddsDensityType(ScMatrix::DensityType eType)
{
switch (eType)
{
case ScMatrix::FILLED_EMPTY:
return mdds::matrix_density_filled_empty;
case ScMatrix::FILLED_ZERO:
return mdds::matrix_density_filled_zero;
case ScMatrix::SPARSE_EMPTY:
return mdds::matrix_density_sparse_empty;
case ScMatrix::SPARSE_ZERO:
return mdds::matrix_density_sparse_zero;
default:
;
}
// default density type
return mdds::matrix_density_filled_zero;
}
/**
* Return a numeric value from a matrix element no matter what its type is.
*/
double getNumericValue(const MatrixImplType::element& elem)
{
switch (elem.m_type)
{
case mdds::element_boolean:
return static_cast<double>(elem.m_boolean);
case mdds::element_numeric:
return elem.m_numeric;
default:
;
}
return 0.0;
}
}
class ScMatrixImpl
{
MatrixImplType maMat;
ScMatrix::DensityType meType;
ScInterpreter* pErrorInterpreter;
bool mbCloneIfConst; // Whether the matrix is cloned with a CloneIfConst() call.
MatrixImplType::size_pair_type maCachedSize;
ScMatrixImpl();
ScMatrixImpl(const ScMatrixImpl&);
public:
ScMatrixImpl(SCSIZE nC, SCSIZE nR, ScMatrix::DensityType eType);
~ScMatrixImpl();
void Clear();
void SetImmutable(bool bVal);
bool IsImmutable() const;
void Resize(SCSIZE nC, SCSIZE nR);
ScMatrix::DensityType GetDensityType() const;
void SetErrorInterpreter( ScInterpreter* p);
ScInterpreter* GetErrorInterpreter() const { return pErrorInterpreter; }
void GetDimensions( SCSIZE& rC, SCSIZE& rR) const;
SCSIZE GetElementCount() const;
bool ValidColRow( SCSIZE nC, SCSIZE nR) const;
SCSIZE CalcOffset( SCSIZE nC, SCSIZE nR) const;
bool ValidColRowReplicated( SCSIZE & rC, SCSIZE & rR ) const;
bool ValidColRowOrReplicated( SCSIZE & rC, SCSIZE & rR ) const;
void SetErrorAtInterpreter( sal_uInt16 nError ) const;
void PutDouble(double fVal, SCSIZE nC, SCSIZE nR);
void PutDouble( double fVal, SCSIZE nIndex);
void PutString(const String& rStr, SCSIZE nC, SCSIZE nR);
void PutString(const String& rStr, SCSIZE nIndex);
void PutEmpty(SCSIZE nC, SCSIZE nR);
void PutEmptyPath(SCSIZE nC, SCSIZE nR);
void PutError( sal_uInt16 nErrorCode, SCSIZE nC, SCSIZE nR );
void PutBoolean(bool bVal, SCSIZE nC, SCSIZE nR);
sal_uInt16 GetError( SCSIZE nC, SCSIZE nR) const;
double GetDouble(SCSIZE nC, SCSIZE nR) const;
double GetDouble( SCSIZE nIndex) const;
const String& GetString(SCSIZE nC, SCSIZE nR) const;
const String& GetString( SCSIZE nIndex) const;
String GetString( SvNumberFormatter& rFormatter, SCSIZE nC, SCSIZE nR) const;
ScMatrixValue Get(SCSIZE nC, SCSIZE nR) const;
bool IsString( SCSIZE nIndex ) const;
bool IsString( SCSIZE nC, SCSIZE nR ) const;
bool IsEmpty( SCSIZE nC, SCSIZE nR ) const;
bool IsEmptyPath( SCSIZE nC, SCSIZE nR ) const;
bool IsValue( SCSIZE nIndex ) const;
bool IsValue( SCSIZE nC, SCSIZE nR ) const;
bool IsValueOrEmpty( SCSIZE nC, SCSIZE nR ) const;
bool IsBoolean( SCSIZE nC, SCSIZE nR ) const;
bool IsNumeric() const;
void MatCopy(ScMatrixImpl& mRes) const;
void MatTrans(ScMatrixImpl& mRes) const;
void FillDouble( double fVal, SCSIZE nC1, SCSIZE nR1, SCSIZE nC2, SCSIZE nR2 );
void CompareEqual();
void CompareNotEqual();
void CompareLess();
void CompareGreater();
void CompareLessEqual();
void CompareGreaterEqual();
double And() const;
double Or() const;
ScMatrix::IterateResult Sum(bool bTextAsZero) const;
ScMatrix::IterateResult SumSquare(bool bTextAsZero) const;
ScMatrix::IterateResult Product(bool bTextAsZero) const;
size_t Count(bool bCountStrings) const;
private:
void CalcPosition(SCSIZE nIndex, SCSIZE& rC, SCSIZE& rR) const;
};
ScMatrixImpl::ScMatrixImpl(SCSIZE nC, SCSIZE nR, ScMatrix::DensityType eType) :
maMat(nR, nC, toMddsDensityType(eType)),
meType(eType),
pErrorInterpreter(NULL),
mbCloneIfConst(true)
{
maCachedSize = maMat.size();
}
ScMatrixImpl::~ScMatrixImpl()
{
Clear();
}
void ScMatrixImpl::Clear()
{
maMat.clear();
maCachedSize = maMat.size();
}
void ScMatrixImpl::SetImmutable(bool bVal)
{
mbCloneIfConst = bVal;
}
bool ScMatrixImpl::IsImmutable() const
{
return mbCloneIfConst;
}
void ScMatrixImpl::Resize(SCSIZE nC, SCSIZE nR)
{
maMat.resize(nR, nC);
maCachedSize = maMat.size();
}
ScMatrix::DensityType ScMatrixImpl::GetDensityType() const
{
return meType;
}
void ScMatrixImpl::SetErrorInterpreter( ScInterpreter* p)
{
pErrorInterpreter = p;
}
void ScMatrixImpl::GetDimensions( SCSIZE& rC, SCSIZE& rR) const
{
rR = maCachedSize.first;
rC = maCachedSize.second;
}
SCSIZE ScMatrixImpl::GetElementCount() const
{
return maCachedSize.first * maCachedSize.second;
}
bool ScMatrixImpl::ValidColRow( SCSIZE nC, SCSIZE nR) const
{
return nR < maCachedSize.first && nC < maCachedSize.second;
}
SCSIZE ScMatrixImpl::CalcOffset( SCSIZE nC, SCSIZE nR) const
{
return nC * maMat.size().first + nR;
}
bool ScMatrixImpl::ValidColRowReplicated( SCSIZE & rC, SCSIZE & rR ) const
{
if (maCachedSize.second == 1 && maCachedSize.first == 1)
{
rC = 0;
rR = 0;
return true;
}
else if (maCachedSize.second == 1 && rR < maCachedSize.first)
{
// single column matrix.
rC = 0;
return true;
}
else if (maCachedSize.first == 1 && rC < maCachedSize.second)
{
// single row matrix.
rR = 0;
return true;
}
return false;
}
bool ScMatrixImpl::ValidColRowOrReplicated( SCSIZE & rC, SCSIZE & rR ) const
{
return ValidColRow( rC, rR) || ValidColRowReplicated( rC, rR);
}
void ScMatrixImpl::SetErrorAtInterpreter( sal_uInt16 nError ) const
{
if ( pErrorInterpreter )
pErrorInterpreter->SetError( nError);
}
void ScMatrixImpl::PutDouble(double fVal, SCSIZE nC, SCSIZE nR)
{
if (ValidColRow( nC, nR))
maMat.set_numeric(nR, nC, fVal);
else
{
OSL_FAIL("ScMatrixImpl::PutDouble: dimension error");
}
}
void ScMatrixImpl::PutDouble( double fVal, SCSIZE nIndex)
{
SCSIZE nC, nR;
CalcPosition(nIndex, nC, nR);
PutDouble(fVal, nC, nR);
}
void ScMatrixImpl::PutString(const String& rStr, SCSIZE nC, SCSIZE nR)
{
if (ValidColRow( nC, nR))
maMat.set_string(nR, nC, new String(rStr));
else
{
OSL_FAIL("ScMatrixImpl::PutString: dimension error");
}
}
void ScMatrixImpl::PutString(const String& rStr, SCSIZE nIndex)
{
SCSIZE nC, nR;
CalcPosition(nIndex, nC, nR);
PutString(rStr, nC, nR);
}
void ScMatrixImpl::PutEmpty(SCSIZE nC, SCSIZE nR)
{
if (ValidColRow( nC, nR))
{
maMat.set_empty(nR, nC);
maMat.clear_flag(nR, nC); // zero flag to indicate that this is 'empty', not 'empty path'.
}
else
{
OSL_FAIL("ScMatrixImpl::PutEmpty: dimension error");
}
}
void ScMatrixImpl::PutEmptyPath(SCSIZE nC, SCSIZE nR)
{
if (ValidColRow( nC, nR))
{
maMat.set_empty(nR, nC);
maMat.set_flag(nR, nC, 1); // non-zero flag to indicate empty 'path'.
}
else
{
OSL_FAIL("ScMatrixImpl::PutEmptyPath: dimension error");
}
}
void ScMatrixImpl::PutError( sal_uInt16 nErrorCode, SCSIZE nC, SCSIZE nR )
{
maMat.set_numeric(nR, nC, CreateDoubleError(nErrorCode));
}
void ScMatrixImpl::PutBoolean(bool bVal, SCSIZE nC, SCSIZE nR)
{
if (ValidColRow( nC, nR))
maMat.set_boolean(nR, nC, bVal);
else
{
OSL_FAIL("ScMatrixImpl::PutBoolean: dimension error");
}
}
sal_uInt16 ScMatrixImpl::GetError( SCSIZE nC, SCSIZE nR) const
{
if (ValidColRowOrReplicated( nC, nR ))
{
double fVal = maMat.get_numeric(nR, nC);
return GetDoubleErrorValue(fVal);
}
else
{
OSL_FAIL("ScMatrixImpl::GetError: dimension error");
return errNoValue;
}
}
double ScMatrixImpl::GetDouble(SCSIZE nC, SCSIZE nR) const
{
if (ValidColRowOrReplicated( nC, nR ))
{
double fVal = maMat.get_numeric(nR, nC);
if ( pErrorInterpreter )
{
sal_uInt16 nError = GetDoubleErrorValue(fVal);
if ( nError )
SetErrorAtInterpreter( nError);
}
return fVal;
}
else
{
OSL_FAIL("ScMatrixImpl::GetDouble: dimension error");
return CreateDoubleError( errNoValue);
}
}
double ScMatrixImpl::GetDouble( SCSIZE nIndex) const
{
SCSIZE nC, nR;
CalcPosition(nIndex, nC, nR);
return GetDouble(nC, nR);
}
const String& ScMatrixImpl::GetString(SCSIZE nC, SCSIZE nR) const
{
if (ValidColRowOrReplicated( nC, nR ))
{
switch (maMat.get_type(nR, nC))
{
case ::mdds::element_string:
return *maMat.get_string(nR, nC);
case ::mdds::element_empty:
return ScGlobal::GetEmptyString();
default:
SetErrorAtInterpreter( GetError(nC, nR));
OSL_FAIL("ScMatrixImpl::GetString: access error, no string");
}
}
else
{
OSL_FAIL("ScMatrixImpl::GetString: dimension error");
}
return ScGlobal::GetEmptyString();
}
const String& ScMatrixImpl::GetString( SCSIZE nIndex) const
{
SCSIZE nC, nR;
CalcPosition(nIndex, nC, nR);
return GetString(nC, nR);
}
String ScMatrixImpl::GetString( SvNumberFormatter& rFormatter, SCSIZE nC, SCSIZE nR) const
{
if (!ValidColRowOrReplicated( nC, nR ))
{
OSL_FAIL("ScMatrixImpl::GetString: dimension error");
return String();
}
if (IsString( nC, nR))
{
if (IsEmptyPath( nC, nR))
{ // result of empty FALSE jump path
sal_uLong nKey = rFormatter.GetStandardFormat( NUMBERFORMAT_LOGICAL,
ScGlobal::eLnge);
String aStr;
Color* pColor = NULL;
rFormatter.GetOutputString( 0.0, nKey, aStr, &pColor);
return aStr;
}
return GetString( nC, nR);
}
sal_uInt16 nError = GetError( nC, nR);
if (nError)
{
SetErrorAtInterpreter( nError);
return ScGlobal::GetErrorString( nError);
}
double fVal= GetDouble( nC, nR);
sal_uLong nKey = rFormatter.GetStandardFormat( NUMBERFORMAT_NUMBER,
ScGlobal::eLnge);
String aStr;
rFormatter.GetInputLineString( fVal, nKey, aStr);
return aStr;
}
ScMatrixValue ScMatrixImpl::Get(SCSIZE nC, SCSIZE nR) const
{
ScMatrixValue aVal;
if (ValidColRowOrReplicated(nC, nR))
{
matrix_element_t eType = maMat.get_type(nR, nC);
switch (eType)
{
case mdds::element_boolean:
aVal.nType = SC_MATVAL_BOOLEAN;
aVal.fVal = maMat.get_boolean(nR, nC);
break;
case mdds::element_numeric:
aVal.nType = SC_MATVAL_VALUE;
aVal.fVal = maMat.get_numeric(nR, nC);
break;
case mdds::element_string:
aVal.nType = SC_MATVAL_STRING;
aVal.pS = maMat.get_string(nR, nC);
break;
case mdds::element_empty:
// Empty path equals empty plus flag.
aVal.nType = maMat.get_flag(nR, nC) ? SC_MATVAL_EMPTYPATH : SC_MATVAL_EMPTY;
aVal.fVal = 0.0;
default:
;
}
}
else
{
OSL_FAIL("ScMatrixImpl::Get: dimension error");
}
return aVal;
}
bool ScMatrixImpl::IsString( SCSIZE nIndex ) const
{
SCSIZE nC, nR;
CalcPosition(nIndex, nC, nR);
return IsString(nC, nR);
}
bool ScMatrixImpl::IsString( SCSIZE nC, SCSIZE nR ) const
{
ValidColRowReplicated( nC, nR );
switch (maMat.get_type(nR, nC))
{
case mdds::element_empty:
case mdds::element_string:
return true;
default:
;
}
return false;
}
bool ScMatrixImpl::IsEmpty( SCSIZE nC, SCSIZE nR ) const
{
// Flag must be zero for this to be an empty element, instead of being an
// empty path element.
ValidColRowReplicated( nC, nR );
return maMat.get_type(nR, nC) == ::mdds::element_empty && maMat.get_flag(nR, nC) == 0;
}
bool ScMatrixImpl::IsEmptyPath( SCSIZE nC, SCSIZE nR ) const
{
// 'Empty path' is empty plus non-zero flag.
if (ValidColRowOrReplicated( nC, nR ))
return maMat.get_type(nR, nC) == ::mdds::element_empty && maMat.get_flag(nR, nC) != 0;
else
return true;
}
bool ScMatrixImpl::IsValue( SCSIZE nIndex ) const
{
SCSIZE nC, nR;
CalcPosition(nIndex, nC, nR);
return IsValue(nC, nR);
}
bool ScMatrixImpl::IsValue( SCSIZE nC, SCSIZE nR ) const
{
ValidColRowReplicated(nC, nR);
switch (maMat.get_type(nR, nC))
{
case mdds::element_boolean:
case mdds::element_numeric:
return true;
default:
;
}
return false;
}
bool ScMatrixImpl::IsValueOrEmpty( SCSIZE nC, SCSIZE nR ) const
{
ValidColRowReplicated(nC, nR);
switch (maMat.get_type(nR, nC))
{
case mdds::element_boolean:
case mdds::element_numeric:
case mdds::element_empty:
return true;
default:
;
}
return false;
}
bool ScMatrixImpl::IsBoolean( SCSIZE nC, SCSIZE nR ) const
{
ValidColRowReplicated( nC, nR );
return maMat.get_type(nR, nC) == ::mdds::element_boolean;
}
bool ScMatrixImpl::IsNumeric() const
{
return maMat.numeric();
}
void ScMatrixImpl::MatCopy(ScMatrixImpl& mRes) const
{
if (maCachedSize.first > mRes.maCachedSize.first || maCachedSize.second > mRes.maCachedSize.second)
{
// destination matrix is not large enough.
OSL_FAIL("ScMatrixImpl::MatCopy: dimension error");
return;
}
mRes.maMat.assign(maMat);
mRes.maCachedSize = mRes.maMat.size();
}
void ScMatrixImpl::MatTrans(ScMatrixImpl& mRes) const
{
mRes.maMat = maMat;
mRes.maMat.transpose();
mRes.maCachedSize = mRes.maMat.size();
}
void ScMatrixImpl::FillDouble( double fVal, SCSIZE nC1, SCSIZE nR1, SCSIZE nC2, SCSIZE nR2 )
{
if (ValidColRow( nC1, nR1) && ValidColRow( nC2, nR2))
{
for (SCSIZE i = nR1; i <= nR2; ++i)
for (SCSIZE j = nC1; j <= nC2; ++j)
maMat.set(i, j, fVal);
}
else
{
OSL_FAIL("ScMatrixImpl::FillDouble: dimension error");
}
}
void ScMatrixImpl::CompareEqual()
{
compareMatrix<ElemEqual>(maMat);
}
void ScMatrixImpl::CompareNotEqual()
{
compareMatrix<ElemNotEqual>(maMat);
}
void ScMatrixImpl::CompareLess()
{
compareMatrix<ElemLess>(maMat);
}
void ScMatrixImpl::CompareGreater()
{
compareMatrix<ElemGreater>(maMat);
}
void ScMatrixImpl::CompareLessEqual()
{
compareMatrix<ElemLessEqual>(maMat);
}
void ScMatrixImpl::CompareGreaterEqual()
{
compareMatrix<ElemGreaterEqual>(maMat);
}
namespace {
struct AndEvaluator
{
bool isBadElem(double fVal) const { return fVal == 0; }
bool returnOnElem() const { return false; }
bool returnOnAllElems() const { return true; }
};
struct OrEvaluator
{
bool isBadElem(double fVal) const { return fVal != 0; }
bool returnOnElem() const { return true; }
bool returnOnAllElems() const { return false; }
};
template <typename _Evaluator>
bool EvalMatrix(const MatrixImplType& rMat)
{
_Evaluator aEval;
size_t nRows = rMat.size().first, nCols = rMat.size().second;
for (size_t i = 0; i < nRows; ++i)
{
for (size_t j = 0; j < nCols; ++j)
{
matrix_element_t eType = rMat.get_type(i, j);
if (eType != mdds::element_numeric && eType != mdds::element_boolean)
// assuming a CompareMat this is an error
return CreateDoubleError(errIllegalArgument);
double fVal = rMat.get_numeric(i, j);
if (!::rtl::math::isFinite(fVal))
// DoubleError
return fVal;
if (aEval.isBadElem(fVal))
return aEval.returnOnElem();
}
}
return aEval.returnOnAllElems();
}
}
double ScMatrixImpl::And() const
{
// All elements must be of value type.
// True only if all the elements have non-zero values.
return EvalMatrix<AndEvaluator>(maMat);
}
double ScMatrixImpl::Or() const
{
// All elements must be of value type.
// True if at least one element has a non-zero value.
return EvalMatrix<OrEvaluator>(maMat);
}
namespace {
/**
* Function object to sum all numeric elements (including boolean). It
* stores the first non-zero element value into maRes.mfFirst while the rest
* into maRes.mfRest. This weird requirement comes from
* ScInterpreter::IterateParameters.
*/
class SumElements : public unary_function<void, MatrixImplType::element>
{
ScMatrix::IterateResult maRes;
bool mbTextAsZero;
public:
SumElements(bool bTextAsZero) : maRes(0.0, 0.0, 0), mbTextAsZero(bTextAsZero) {}
ScMatrix::IterateResult getResult() const { return maRes; }
void operator() (const MatrixImplType::element& elem)
{
switch (elem.m_type)
{
case mdds::element_boolean:
if (elem.m_boolean)
{
if (maRes.mfFirst)
maRes.mfFirst = 1.0;
else
maRes.mfRest += 1.0;
}
++maRes.mnCount;
break;
case mdds::element_numeric:
if (elem.m_numeric != 0.0)
{
if (maRes.mfFirst)
maRes.mfFirst = elem.m_numeric;
else
maRes.mfRest += elem.m_numeric;
}
++maRes.mnCount;
break;
case mdds::element_string:
if (mbTextAsZero)
++maRes.mnCount;
default:
;
}
}
};
class SumSquareElements : public unary_function<void, MatrixImplType::element>
{
ScMatrix::IterateResult maRes;
bool mbTextAsZero;
public:
SumSquareElements(bool bTextAsZero) : maRes(0.0, 0.0, 0), mbTextAsZero(bTextAsZero) {}
ScMatrix::IterateResult getResult() const { return maRes; }
void operator() (const MatrixImplType::element& elem)
{
if (elem.m_type == ::mdds::element_empty)
return;
if (elem.m_type == ::mdds::element_string)
{
if (mbTextAsZero)
++maRes.mnCount;
return;
}
double val = getNumericValue(elem);
maRes.mfRest += val*val;
++maRes.mnCount;
}
};
/**
* Multiply all boolean and numeric elements. It skips empty elements, and
* optionally string elements if specified. When text as zero option is
* specified, it treats string elements as if they have values of zero.
*/
class MultiplyElements : public unary_function<void, MatrixImplType::element>
{
ScMatrix::IterateResult maRes;
bool mbTextAsZero;
public:
MultiplyElements(bool bTextAsZero) : maRes(0.0, 1.0, 0), mbTextAsZero(bTextAsZero) {}
ScMatrix::IterateResult getResult() const { return maRes; }
void operator() (const MatrixImplType::element& elem)
{
if (elem.m_type == ::mdds::element_string)
{
++maRes.mnCount;
if (mbTextAsZero)
maRes.mfRest = 0.0;
}
else if (elem.m_type != ::mdds::element_empty)
{
++maRes.mnCount;
maRes.mfRest *= getNumericValue(elem);
}
}
};
/**
* Predicate for counting only boolean, numeric, and optionally string
* elements.
*/
class CountNonEmptyElements : public unary_function<bool, MatrixImplType::element>
{
const bool mbCountString;
public:
CountNonEmptyElements(bool bCountString) : mbCountString(bCountString) {}
bool operator() (const MatrixImplType::element& elem) const
{
switch (elem.m_type)
{
case mdds::element_boolean:
case mdds::element_numeric:
return true;
case mdds::element_string:
return mbCountString;
default:
;
}
return false;
}
};
}
ScMatrix::IterateResult ScMatrixImpl::Sum(bool bTextAsZero) const
{
return for_each(maMat.begin(), maMat.end(), SumElements(bTextAsZero)).getResult();
}
ScMatrix::IterateResult ScMatrixImpl::SumSquare(bool bTextAsZero) const
{
return for_each(maMat.begin(), maMat.end(), SumSquareElements(bTextAsZero)).getResult();
}
ScMatrix::IterateResult ScMatrixImpl::Product(bool bTextAsZero) const
{
return for_each(maMat.begin(), maMat.end(), MultiplyElements(bTextAsZero)).getResult();
}
size_t ScMatrixImpl::Count(bool bCountStrings) const
{
return count_if(maMat.begin(), maMat.end(), CountNonEmptyElements(bCountStrings));
}
void ScMatrixImpl::CalcPosition(SCSIZE nIndex, SCSIZE& rC, SCSIZE& rR) const
{
SCSIZE nRowSize = maCachedSize.first;
rC = nIndex / nRowSize;
rR = nIndex - rC*nRowSize;
}
// ============================================================================
ScMatrix::ScMatrix( SCSIZE nC, SCSIZE nR, DensityType eType) :
pImpl(new ScMatrixImpl(nC, nR, eType)),
nRefCnt(0)
{
}
ScMatrix::~ScMatrix()
{
delete pImpl;
}
ScMatrix* ScMatrix::Clone() const
{
return Clone(GetDensityType());
}
ScMatrix* ScMatrix::Clone( DensityType eType) const
{
SCSIZE nC, nR;
pImpl->GetDimensions(nC, nR);
ScMatrix* pScMat = new ScMatrix(nC, nR, eType);
MatCopy(*pScMat);
pScMat->SetErrorInterpreter(pImpl->GetErrorInterpreter()); // TODO: really?
return pScMat;
}
ScMatrix* ScMatrix::CloneIfConst()
{
return pImpl->IsImmutable() ? Clone() : this;
}
void ScMatrix::SetImmutable( bool bVal )
{
pImpl->SetImmutable(bVal);
}
void ScMatrix::Resize( SCSIZE nC, SCSIZE nR)
{
pImpl->Resize(nC, nR);
}
ScMatrix* ScMatrix::CloneAndExtend( SCSIZE nNewCols, SCSIZE nNewRows, DensityType eType ) const
{
ScMatrix* pScMat = new ScMatrix( nNewCols, nNewRows, eType);
MatCopy(*pScMat);
pScMat->SetErrorInterpreter(pImpl->GetErrorInterpreter());
return pScMat;
}
ScMatrix::DensityType ScMatrix::GetDensityType() const
{
return pImpl->GetDensityType();
}
void ScMatrix::SetErrorInterpreter( ScInterpreter* p)
{
pImpl->SetErrorInterpreter(p);
}
void ScMatrix::GetDimensions( SCSIZE& rC, SCSIZE& rR) const
{
pImpl->GetDimensions(rC, rR);
}
SCSIZE ScMatrix::GetElementCount() const
{
return pImpl->GetElementCount();
}
bool ScMatrix::ValidColRow( SCSIZE nC, SCSIZE nR) const
{
return pImpl->ValidColRow(nC, nR);
}
SCSIZE ScMatrix::CalcOffset( SCSIZE nC, SCSIZE nR) const
{
return pImpl->CalcOffset(nC, nR);
}
bool ScMatrix::ValidColRowReplicated( SCSIZE & rC, SCSIZE & rR ) const
{
return pImpl->ValidColRowReplicated(rC, rR);
}
bool ScMatrix::ValidColRowOrReplicated( SCSIZE & rC, SCSIZE & rR ) const
{
return ValidColRow( rC, rR) || ValidColRowReplicated( rC, rR);
}
void ScMatrix::PutDouble(double fVal, SCSIZE nC, SCSIZE nR)
{
pImpl->PutDouble(fVal, nC, nR);
}
void ScMatrix::PutDouble( double fVal, SCSIZE nIndex)
{
pImpl->PutDouble(fVal, nIndex);
}
void ScMatrix::PutString(const String& rStr, SCSIZE nC, SCSIZE nR)
{
pImpl->PutString(rStr, nC, nR);
}
void ScMatrix::PutString(const String& rStr, SCSIZE nIndex)
{
pImpl->PutString(rStr, nIndex);
}
void ScMatrix::PutEmpty(SCSIZE nC, SCSIZE nR)
{
pImpl->PutEmpty(nC, nR);
}
void ScMatrix::PutEmptyPath(SCSIZE nC, SCSIZE nR)
{
pImpl->PutEmptyPath(nC, nR);
}
void ScMatrix::PutError( sal_uInt16 nErrorCode, SCSIZE nC, SCSIZE nR )
{
pImpl->PutError(nErrorCode, nC, nR);
}
void ScMatrix::PutBoolean(bool bVal, SCSIZE nC, SCSIZE nR)
{
pImpl->PutBoolean(bVal, nC, nR);
}
sal_uInt16 ScMatrix::GetError( SCSIZE nC, SCSIZE nR) const
{
return pImpl->GetError(nC, nR);
}
double ScMatrix::GetDouble(SCSIZE nC, SCSIZE nR) const
{
return pImpl->GetDouble(nC, nR);
}
double ScMatrix::GetDouble( SCSIZE nIndex) const
{
return pImpl->GetDouble(nIndex);
}
const String& ScMatrix::GetString(SCSIZE nC, SCSIZE nR) const
{
return pImpl->GetString(nC, nR);
}
const String& ScMatrix::GetString( SCSIZE nIndex) const
{
return pImpl->GetString(nIndex);
}
String ScMatrix::GetString( SvNumberFormatter& rFormatter, SCSIZE nC, SCSIZE nR) const
{
return pImpl->GetString(rFormatter, nC, nR);
}
ScMatrixValue ScMatrix::Get(SCSIZE nC, SCSIZE nR) const
{
return pImpl->Get(nC, nR);
}
sal_Bool ScMatrix::IsString( SCSIZE nIndex ) const
{
return pImpl->IsString(nIndex);
}
sal_Bool ScMatrix::IsString( SCSIZE nC, SCSIZE nR ) const
{
return pImpl->IsString(nC, nR);
}
sal_Bool ScMatrix::IsEmpty( SCSIZE nC, SCSIZE nR ) const
{
return pImpl->IsEmpty(nC, nR);
}
sal_Bool ScMatrix::IsEmptyPath( SCSIZE nC, SCSIZE nR ) const
{
return pImpl->IsEmptyPath(nC, nR);
}
sal_Bool ScMatrix::IsValue( SCSIZE nIndex ) const
{
return pImpl->IsValue(nIndex);
}
sal_Bool ScMatrix::IsValue( SCSIZE nC, SCSIZE nR ) const
{
return pImpl->IsValue(nC, nR);
}
sal_Bool ScMatrix::IsValueOrEmpty( SCSIZE nC, SCSIZE nR ) const
{
return pImpl->IsValueOrEmpty(nC, nR);
}
sal_Bool ScMatrix::IsBoolean( SCSIZE nC, SCSIZE nR ) const
{
return pImpl->IsBoolean(nC, nR);
}
sal_Bool ScMatrix::IsNumeric() const
{
return pImpl->IsNumeric();
}
void ScMatrix::MatCopy(ScMatrix& mRes) const
{
pImpl->MatCopy(*mRes.pImpl);
}
void ScMatrix::MatTrans(ScMatrix& mRes) const
{
pImpl->MatTrans(*mRes.pImpl);
}
void ScMatrix::FillDouble( double fVal, SCSIZE nC1, SCSIZE nR1, SCSIZE nC2, SCSIZE nR2 )
{
pImpl->FillDouble(fVal, nC1, nR1, nC2, nR2);
}
void ScMatrix::CompareEqual()
{
pImpl->CompareEqual();
}
void ScMatrix::CompareNotEqual()
{
pImpl->CompareNotEqual();
}
void ScMatrix::CompareLess()
{
pImpl->CompareLess();
}
void ScMatrix::CompareGreater()
{
pImpl->CompareGreater();
}
void ScMatrix::CompareLessEqual()
{
pImpl->CompareLessEqual();
}
void ScMatrix::CompareGreaterEqual()
{
pImpl->CompareGreaterEqual();
}
double ScMatrix::And() const
{
return pImpl->And();
}
double ScMatrix::Or() const
{
return pImpl->Or();
}
ScMatrix::IterateResult ScMatrix::Sum(bool bTextAsZero) const
{
return pImpl->Sum(bTextAsZero);
}
ScMatrix::IterateResult ScMatrix::SumSquare(bool bTextAsZero) const
{
return pImpl->SumSquare(bTextAsZero);
}
ScMatrix::IterateResult ScMatrix::Product(bool bTextAsZero) const
{
return pImpl->Product(bTextAsZero);
}
size_t ScMatrix::Count(bool bCountStrings) const
{
return pImpl->Count(bCountStrings);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|