00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00031
00032
00033
00034
00035 #ifdef __GNUG__
00036 #pragma implementation "ElementArray.h"
00037 #endif
00038
00039
00040 #include "mc/mcprec.h"
00041 #ifdef __BORLANDC__
00042 #pragma hdrstop
00043 #endif
00044
00045 #ifndef mcPRECOMP
00046 #include "mc/MathUtils.h"
00047 #include "mc/ElementArray.h"
00048 #include "mc/Polynomial.h"
00049 #include "mc/Monomial.h"
00050 #include "mc/EmptyBox.h"
00051 #include "mc/Number.h"
00052 #include "mc/Bracket.h"
00053 #include "mc/Fraction.h"
00054 #include "mc/Symbol.h"
00055 #include "mc/Radical.h"
00056 #endif
00057
00058
00059 mcIMPLEMENT_ABSTRACT_CLASS(mcElementArray, mcElement);
00060
00061
00062 #ifdef mcELEMENTARRAY_DRAW_DECORATIONS
00063
00064
00065 #include "BeginTri.xpm"
00066 #include "EndTri.xpm"
00067
00068
00069 wxBitmap *mcElementArrayHelpers::gui_pBeginTriBmp = NULL;
00070 wxBitmap *mcElementArrayHelpers::gui_pEndTriBmp = NULL;
00071
00072 #endif
00073
00074
00075
00076
00077 wxPen *mcElementArrayHelpers::sgui_pSelectionPen = NULL;
00078 bool mcElementArrayHelpers::sgui_bDrawSelectionInverting = TRUE;
00079
00080
00081
00082 mcArrayEntry mcEmptyArrayEntry(NULL, -1);
00083
00084
00085
00086
00087
00088
00089
00090
00091 mcElement &mcArrayEntry::data_GetRef() const
00092 { return ((mcElementArrayHelpers *)mdata_arr)->data_GetRefFromEntry(this); }
00093
00094 int mcArrayEntry::data_GetIdx() const
00095 { return mdata_arr->data_GetIdxFromEntry(this); }
00096
00097 int mcArrayEntry::math_GetIdx() const
00098 { return mdata_arr->math_DataToMathIdx(data_GetIdx()); }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 mcFilter::mcFilter(mcFilterType type, mcElementType t1, int count1)
00109 {
00110
00111 for (int i=0; i < mcNUM_ELEMENT_TYPES; i++) {
00112
00113 m_nMaxInstances[i] = 0;
00114 }
00115
00116
00117 m_nType = type;
00118
00119
00120 SetMaxInstancesFor(t1, count1);
00121 }
00122
00123 void mcFilter::SetMaxInstancesFor(mcElementType t, int count)
00124 {
00125
00126 m_nMaxInstances[t] = count;
00127 }
00128
00129 bool mcFilter::isElementTypeAllowed(mcElementType t, int num)
00130 {
00131 if (m_nMaxInstances[t] == mcFILTER_NO_INSTANCES_LIMIT ||
00132 m_nMaxInstances[t] > num)
00133 return TRUE;
00134 return FALSE;
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 #ifdef __MCDEBUG__
00146
00147 wxString mcElementArrayHelpers::data_Debug(long flags) const
00148 {
00149 wxString f = wxT(" ");
00150 if (mcMathCore::Get()->isGUIEnabled() && data_isUsingFilter())
00151 f = wxT(" (using filter) ");
00152 wxString tmp = data_GetDebugName() + f + wxT("[\n");
00153
00154
00155 int step = mcMathCore::Get()->m_nIndentationStep, max = 1;
00156 for (int i=0; i < data_GetCount(); i++) {
00157
00158
00159 f = data_Get(i).data_GetDebug(step, flags);
00160
00161 max = mcMAX(((int)f.Len()), max);
00162 tmp += f;
00163 }
00164
00165
00166 max = mcMIN(40, max);
00167 tmp += data_GetDebugName() + wxT(" ]\n");
00168
00169 return tmp;
00170 }
00171
00172 void mcElementArrayHelpers::data_Check() const
00173 {
00174
00175 mcElementHelpers::data_Check();
00176
00177
00178 for (int i=0; i < data_GetCount(); i++) {
00179
00180
00181
00182
00183
00184
00185 mcASSERT((data_Get(i).data_GetType() != data_GetType()), wxT("Nested array found"));
00186 }
00187 }
00188
00189 #endif // __MCDEBUG__
00190
00191 bool mcElementArrayHelpers::data_isSameAs(const mcElementHelpers *p) const
00192 {
00193 if (!mcElementHelpers::data_isSameAs(p))
00194 return FALSE;
00195
00196 const mcElementArrayHelpers *e = (const mcElementArrayHelpers *)p;
00197 if (mdata_nElements != e->mdata_nElements)
00198 return FALSE;
00199
00200
00201
00202
00203
00204
00205 return TRUE;
00206 }
00207
00208 bool mcElementArrayHelpers::data_isWrappingOnly(mcElementType t) const
00209 {
00210
00211
00212 if (data_GetCount() > 1)
00213 return FALSE;
00214 if (data_Get(0).data_GetType() == t)
00215 return TRUE;
00216 return FALSE;
00217 }
00218
00219 const mcElement &mcElementArrayHelpers::data_GetWrapped(mcElementType t) const
00220 {
00221 if (!data_isWrappingOnly(t))
00222 return mcEmptyElement;
00223 return data_Get(0);
00224 }
00225
00226 void mcElementArrayHelpers::data_AddNewEmptyBox(int pos)
00227 {
00228 mcEmptyBox pnew;
00229 pnew.data_AddProperty(mcEP_INITIALIZED);
00230 data_AddElements(&pnew, 1, pos, TRUE);
00231 }
00232
00233 void mcElementArrayHelpers::data_Delete(int entry)
00234 {
00235 data_CheckIndex(entry);
00236
00237
00238
00239 data_SetAsEmptyEntry(entry);
00240 }
00241
00242 bool mcElementArrayHelpers::data_Delete(const mcElement &elem)
00243 {
00244 int idx = data_GetIndexOf(elem);
00245 if (idx == -1) return FALSE;
00246
00247 data_Delete(idx);
00248 data_MoveElemLeft(idx);
00249 return TRUE;
00250 }
00251
00252 void mcElementArrayHelpers::data_DeleteLast(int num)
00253 {
00254 int count = data_GetCount();
00255 for (int i=0; i < num; i++)
00256 data_Delete(count-1-i);
00257 mdata_nElements -= num;
00258 }
00259
00260 void mcElementArrayHelpers::data_DeleteFirst(int num)
00261 {
00262 for (int i=0; i < num; i++) {
00263 data_Delete(0);
00264 data_MoveElemLeft(0);
00265 }
00266 }
00267
00268 void mcElementArrayHelpers::data_DeleteAllElemType(mcElementType t)
00269 {
00270 for (int i=0, max=data_GetNumOfElemType(t); i < max; i++) {
00271
00272
00273
00274 int idx = data_GetElemIndexOfType(0, t);
00275 data_Delete(idx);
00276 data_MoveElemLeft(idx);
00277 }
00278 }
00279
00280 mcElement mcElementArrayHelpers::data_Detach(int entry)
00281 {
00282 data_CheckIndex(entry);
00283
00284
00285
00286
00287 mcElement p = data_Get(entry);
00288
00289
00290 data_SetAsEmptyEntry(entry);
00291
00292 return p;
00293 }
00294
00295 mcElement mcElementArrayHelpers::data_DetachLastElem(int num)
00296 {
00297 int count = data_GetCount();
00298 mcElement p = mcEmptyElement;
00299
00300 for (int i=0; i < num; i++)
00301 p = data_Detach(count-1-i);
00302
00303
00304 mdata_nElements -= num;
00305
00306 return p;
00307 }
00308
00309 bool mcElementArrayHelpers::data_ScanArray(bool (*func)(const mcElement &), bool odd) const
00310 {
00311 bool flag = TRUE;
00312
00313 for (int i=(int)!odd; i < data_GetCount(); i++)
00314 flag &= (*func)(data_Get(i));
00315 return flag;
00316 }
00317
00318 void mcElementArrayHelpers::data_CheckArrayLimit()
00319 {
00320
00321 if (data_GetCount()+1 >= mdata_nUpperBound) {
00322
00323
00324 mcEXTEND_ARRAY(mcElement, mdata_pElemArray, mdata_nUpperBound,
00325 mdata_nUpperBound+mcELEMENTARRAY_DEFAULT_ROOM);
00326
00327 mdata_nUpperBound += mcELEMENTARRAY_DEFAULT_ROOM;
00328
00329
00330
00331
00332
00333
00334 }
00335 }
00336
00337 void mcElementArrayHelpers::data_DeleteAll()
00338 {
00339
00340 if (mdata_pElemArray == NULL)
00341 return;
00342
00343
00344 for (int i=0; i < mdata_nUpperBound; i++)
00345 data_Delete(i);
00346
00347
00348 mdata_nElements = 0;
00349 }
00350
00351 mcElement mcElementArrayHelpers::data_DetachAll()
00352 {
00353
00354 if (mdata_pElemArray == NULL)
00355 return mcEmptyElement;
00356
00357
00358 mcElement p = mcEmptyElement;
00359 for (int i=0; i < mdata_nUpperBound; i++)
00360 p = data_Detach(i);
00361
00362
00363 mdata_nElements = 0;
00364
00365 return p;
00366 }
00367
00368 void mcElementArrayHelpers::data_MoveElemRight(int start)
00369 {
00370
00371 data_CheckArrayLimit();
00372 for (int n=mdata_nElements; n > start; n--)
00373 mdata_pElemArray[n] = mdata_pElemArray[n-1];
00374
00375
00376 mdata_nElements++;
00377
00378
00379 data_SetAsEmptyEntry(start);
00380 }
00381
00382 void mcElementArrayHelpers::data_MoveElemLeft(int start)
00383 {
00384
00385 for (; start < data_GetCount()-1; start++)
00386 mdata_pElemArray[start] = mdata_pElemArray[start+1];
00387
00388
00389 mdata_nElements--;
00390
00391
00392 data_SetAsEmptyEntry(data_GetCount());
00393 }
00394
00395 int mcElementArrayHelpers::data_GetIndexOf(const mcElement &p) const
00396 {
00397
00398 for (int i=0; i < data_GetCount(); i++)
00399 if (data_Get(i) == p)
00400 return i;
00401
00402
00403 return -1;
00404 }
00405
00406 void mcElementArrayHelpers::data_Swap(int index1, int index2)
00407 {
00408 data_CheckIndex(index1);
00409 data_CheckIndex(index2);
00410
00411
00412 mcElement tmp = data_Get(index1);
00413 mdata_pElemArray[index1] = data_Get(index2);
00414 mdata_pElemArray[index2] = tmp;
00415 }
00416
00417 void mcElementArrayHelpers::data_UpdateNeighbor(int n)
00418 {
00419
00420 if (n >= 1 && data_isValidElem(n-1) && data_Get(n-1).data_hasProperty(mcEP_NOTIFY_NEIGHBOR_CHANGE))
00421 data_Get(n-1).hlp()->data_OnNeighborChange();
00422 if (n < data_GetCount()-1 && data_isValidElem(n+1) && data_Get(n+1).data_hasProperty(mcEP_NOTIFY_NEIGHBOR_CHANGE))
00423 data_Get(n+1).hlp()->data_OnNeighborChange();
00424 }
00425
00426 void mcElementArrayHelpers::data_Merge(const mcElementArray &p, int n, bool bAddToEnd)
00427 {
00428
00429 data_AddElements(p.data_GetArray(n),
00430 p.data_GetCount()-n,
00431 (bAddToEnd ? -1 : 0));
00432
00433 }
00434
00435 mcElement &mcElementArrayHelpers::data_AddNewElement(mcElementType t, bool bOverwrite, int pos)
00436 {
00437
00438 mcElement pnew = mcElementHelpers::data_NewElem(t);
00439
00440
00441 int n = data_AddElements(&pnew, 1, pos, bOverwrite);
00442 return data_Get(n);
00443 }
00444
00445 void mcElementArrayHelpers::data_Set(int n, const mcElement &pnew)
00446 {
00447 data_CheckIndex(n);
00448 mdata_pElemArray[n] = pnew;
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471 #ifdef mcEP_NOTIFY_NEIGHBOR_CHANGE
00472
00473
00474 if (pnew.data_hasProperty(mcEP_NOTIFY_NEIGHBOR_CHANGE))
00475 mdata_pElemArray[n].hlp()->data_OnNeighborChange();
00476 #endif
00477 }
00478
00479 int mcElementArrayHelpers::data_AddElements(const mcElement *p, int num, int pos,
00480 bool bOverwrite, bool bForceCopy)
00481 {
00482 mcElement pnew;
00483 int firstpos = -1;
00484
00485
00486
00487 if (pos == -1)
00488 pos = data_GetCount();
00489
00490
00491 for (int i=0; i < num; i++) {
00492
00493
00494 pnew = p[i];
00495 mcASSERT(data_isValidElem(pnew), wxT("Cannot add empty elements"));
00496 mcASSERT(pnew.data_GetType() != data_GetType(), wxT("Cannot nest arrays"));
00497
00498 if (bForceCopy) {
00499
00500
00501
00502 pnew = p[i].data_Clone();
00503 mcMATHLOG(wxT("mcElementArrayHelpers::data_AddElements - I was forced to copy [%s]"), mcTXT(pnew));
00504 }
00505
00506 if (pos == data_GetCount()) {
00507
00508
00509 data_CheckArrayLimit();
00510
00511
00512
00513
00514 data_Set(mdata_nElements, pnew);
00515 if (firstpos == -1) firstpos = mdata_nElements;
00516 mdata_nElements++;
00517
00518 } else {
00519
00520
00521 data_CheckIndex(pos);
00522
00523
00524
00525 if (bOverwrite) {
00526
00527
00528 data_Delete(pos);
00529 } else {
00530
00531 data_MoveElemRight(pos);
00532 }
00533
00534
00535 data_Set(pos, pnew);
00536 if (firstpos == -1) firstpos = pos;
00537 }
00538
00539
00540 pos++;
00541 }
00542
00543 return firstpos;
00544 }
00545
00546
00547
00548
00549 #define SCAN_ARRAY_COUNTING(do_check) \
00550 int n=0; \
00551 for (int i=0; i < data_GetCount(); i++) \
00552 if (do_check) n++; \
00553 return n;
00554
00555 int mcElementArrayHelpers::data_GetNumOfElemType(mcElementType t) const
00556 {
00557 SCAN_ARRAY_COUNTING(data_Get(i).data_GetType() == t);
00558 }
00559
00560 int mcElementArrayHelpers::data_GetOpCount() const
00561 {
00562 SCAN_ARRAY_COUNTING(data_isOp(i));
00563 }
00564
00565
00566 #define SCAN_ARRAY_SEARCHING(do_check) \
00567 \
00568 \
00569 \
00570 int occurrence=-1; \
00571 \
00572 \
00573 \
00574 \
00575 for (int i=0; i < data_GetCount(); i++) { \
00576 if (do_check) { \
00577 occurrence++; \
00578 if (n == occurrence) \
00579 return i; \
00580 } \
00581 } \
00582 \
00583 \
00584 return -1;
00585
00586
00587 int mcElementArrayHelpers::data_GetElemIndexOfType(int n, mcElementType t) const
00588 {
00589 SCAN_ARRAY_SEARCHING(data_Get(i).data_GetType() == t);
00590 }
00591
00592 int mcElementArrayHelpers::data_GetNonOpElemIndex(int n) const
00593 {
00594 SCAN_ARRAY_SEARCHING(!data_isOp(i));
00595 }
00596
00597 int mcElementArrayHelpers::data_GetOpIndex(int n) const
00598 {
00599 SCAN_ARRAY_SEARCHING(data_isOp(i));
00600 }
00601
00602
00603
00604 #define RETURN_ELEM_FROM_INDEX(idx_func, cast) \
00605 int i; \
00606 if ((i=idx_func) != -1) \
00607 return (cast)data_Get(i); \
00608 \
00609 \
00610 return (cast)mcEmptyElement;
00611
00612
00613 mcElement &mcElementArrayHelpers::data_GetElemOfType(int n, mcElementType t) const
00614 {
00615 RETURN_ELEM_FROM_INDEX(data_GetElemIndexOfType(n, t), mcElement &);
00616 }
00617
00618 mcElement &mcElementArrayHelpers::data_GetNonOpElem(int n) const
00619 {
00620 RETURN_ELEM_FROM_INDEX(data_GetNonOpElemIndex(n), mcElement &);
00621 }
00622
00623 mcOperator &mcElementArrayHelpers::data_GetOp(int n) const
00624 {
00625 RETURN_ELEM_FROM_INDEX(data_GetOpIndex(n), mcOperator &);
00626 }
00627
00628
00629 int mcElementArrayHelpers::data_QueueElemType(mcElementType t, int firstpos)
00630 {
00631 int curpos=firstpos+2;
00632
00633
00634
00635 for (int i=firstpos; i < data_GetCount(); i++) {
00636
00637
00638 if (data_Get(i).data_GetType() == t) {
00639
00640
00641 if (data_Get(firstpos).data_GetType() != t) {
00642
00643
00644
00645
00646 data_Swap(firstpos, i);
00647
00648
00649
00650
00651
00652 } else if (i != firstpos) {
00653
00654
00655
00656 if (i != curpos)
00657 data_Swap(curpos, i);
00658 }
00659 }
00660 }
00661
00662 return curpos-firstpos;
00663 }
00664
00665 void mcElementArrayHelpers::data_DeepCopy(const mcElementHelpers *p)
00666 {
00667 const mcElementArrayHelpers *e = (const mcElementArrayHelpers *)p;
00668
00669
00670 data_DeleteAll();
00671
00672
00673 if (e->mdata_pElemArray != NULL)
00674 data_AddElements(e->data_GetArray(), e->data_GetCount(), 0, TRUE);
00675
00676
00677 mdata_pFilter = e->mdata_pFilter;
00678
00679
00680 mcElementHelpers::data_DeepCopy(p);
00681 }
00682
00683
00684
00685 void mcElementArrayHelpers::data_SetFilter(const mcFilter *p)
00686 {
00687
00688 data_ChangeFilter(p);
00689
00690 mcElement pthis(this);
00691 data_UpdateFilterFor(pthis, p);
00692 }
00693
00694 void mcElementArrayHelpers::data_ChangeFilter(const mcFilter *p)
00695 {
00696 mcSAFE_DELETE(mdata_pFilter);
00697
00698 if (p) {
00699 mdata_pFilter = new mcFilter();
00700 mdata_pFilter->DeepCopy(p);
00701 }
00702 }
00703
00704 void mcElementArrayHelpers::data_UpdateFilterFor(mcElement &p, const mcFilter *f)
00705 {
00706
00707 p.data_AddRecursiveProperty(mcET_MONOMIAL, mcEP_ELEMENTARRAY_FILTER, (void *)f);
00708 p.data_AddRecursiveProperty(mcET_POLYNOMIAL, mcEP_ELEMENTARRAY_FILTER, (void *)f);
00709 }
00710
00711 bool mcElementArrayHelpers::data_isElementAllowed(mcElementType type) const
00712 {
00713
00714
00715 if (mdata_pFilter) {
00716
00717
00718
00719 if (!mdata_pFilter->isElementTypeAllowed(type, data_GetNumOfElemType(type)))
00720 return FALSE;
00721 }
00722
00723 return TRUE;
00724 }
00725
00726 mcElementType mcElementArrayHelpers::data_GetOpTypeBetween(int n1, int n2) const
00727 {
00728 data_CheckIndex(n1);
00729 data_CheckIndex(n2);
00730
00731
00732 if (n1 > n2) mcSWAP(int, n1, n2);
00733 if (n1 == n2) return data_Get(n1).data_GetType();
00734
00735
00736 mcASSERT((n2 != -1) && (n1 != -1),
00737 wxT("Invalid indexes"));
00738 mcASSERT(n2 > n1,
00739 wxT("GetIndex function doesn't work because n2 > n1"));
00740 mcASSERT(n2-n1 <= 2,
00741 wxT("Two non-op elements cannot be separed by more than one element..."));
00742
00743
00744
00745 if (n1+1 == n2)
00746 return math_GetNeutralOpType();
00747
00748 mcElementType res = data_Get(n1+1).data_GetType();
00749 mcASSERT(mcOperatorHelpers::data_isOp(res),
00750 wxT("Two non-ops are separed by a non-op element ???"));
00751
00752 return res;
00753 }
00754
00755 mcElementType mcElementArrayHelpers::data_GetOpTypePreceding(int n) const
00756 {
00757
00758 if (n == 0 || (n > 0 && !data_isOp(n-1)))
00759 return math_GetNeutralOpType();
00760
00761 mcASSERT(data_isOp(n-1), wxT("Something wrong"));
00762 return data_Get(n-1).data_GetType();
00763 }
00764
00765
00766
00767
00768 bool mcElementArrayHelpers::data_isOp(int entry) const
00769 {
00770 data_CheckIndex(entry);
00771 return mcOperatorHelpers::data_isOp(data_Get(entry).data_GetType());
00772 }
00773
00774 void mcElementArrayHelpers::data_AddNewOp(mcElementType type, int pos)
00775 {
00776 mcElement pnew = mcElementHelpers::data_NewElem(type);
00777 data_AddElements(&pnew, 1, pos, TRUE);
00778 }
00779
00780 void mcElementArrayHelpers::data_Move(int dn, int dk)
00781 {
00783
00784
00785 mcASSERT(!data_isOp(dn), wxT("Invalid data index"));
00786
00787 bool bmoveop = FALSE;
00788 if (dn > 0) bmoveop = data_isOp(dn-1);
00789
00790
00791 mcElement tomove = data_Detach(dn);
00792 data_MoveElemLeft(dn);
00793
00794
00795 mcElement optomove = NULL;
00796 if (bmoveop) {
00797 optomove = data_Detach(dn-1);
00798 data_MoveElemLeft(dn-1);
00799 }
00800
00801
00802 data_MoveElemRight(dk);
00803 if (bmoveop) data_MoveElemRight(dk);
00804
00805
00806 if (bmoveop)
00807 data_Set(dk++, optomove);
00808 data_Set(dk, tomove);
00809 }
00810
00811 int mcElementArrayHelpers::data_MoveAllElemType(mcElementType t, mcElementArray &m)
00812 {
00813 int i,max;
00814
00815 for (i=0,max=data_GetNumOfElemType(t); i < max; i++) {
00816
00817
00818 int dataidx = data_GetElemIndexOfType(0, t);
00819
00820
00821
00822 if (dataidx > 0 && data_isOp(dataidx-1)) {
00823 mcElement p = data_Detach(dataidx-1);
00824 data_MoveElemLeft(dataidx-1);
00825 m.data_AddElements(&p, 1, -1, TRUE);
00826
00827
00828
00829 dataidx--;
00830 }
00831
00832
00833 mcElement p = data_Detach(dataidx);
00834 data_MoveElemLeft(dataidx);
00835
00836
00837
00838 m.data_AddElements(&p, 1, -1, TRUE);
00839
00840 }
00841
00842
00843 return max;
00844 }
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857 int mcElementArrayHelpers::gui_GetSpaceBetween() const
00858 {
00859
00860
00861
00862 return gui_GetSpaceBetweenRatio()/(gui_GetExpDepth()+1);
00863 }
00864
00865 bool mcElementArrayHelpers::gui_isEndKey(const mcKey &ev) const
00866 {
00867 if (data_GetCount() > 0 && data_Get(mgui_nCursorPos).gui_isEndKey(ev))
00868 if (gui_isArrEndKey(ev))
00869 return TRUE;
00870 return FALSE;
00871 }
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883 int mcElementArrayHelpers::gui_AddNewElement(mcElementType type, const mcKey &key, int pos)
00884 {
00885 if (pos == -1) pos = data_GetCount();
00886
00887
00888 mcElement fakearray = mcElementHelpers::data_NewElem(type);
00889
00890
00891
00892
00893
00894
00895 fakearray.gui_Input(key, NULL);
00896
00897
00898 data_AddElements(&fakearray, 1, pos, TRUE);
00899
00900
00901 return pos;
00902 }
00903
00904 void mcElementArrayHelpers::gui_AddNewEmptyBox(int pos)
00905 {
00906 mcKey fake(mcEmptyBoxHelpers::sgui_pNewEmptyBox->GetEvent(), TRUE);
00907 gui_AddNewElement(mcET_EMPTYBOX, fake, pos);
00908 }
00909
00910 void mcElementArrayHelpers::gui_DeleteNextOp()
00911 {
00912 if (mgui_nCursorPos < data_GetCount()-1 &&
00913 data_isOp(mgui_nCursorPos+1)) {
00914
00915 data_Delete(mgui_nCursorPos+1);
00916 data_MoveElemLeft(mgui_nCursorPos+1);
00917 }
00918 }
00919
00920 void mcElementArrayHelpers::gui_DeletePreviousOp()
00921 {
00922 if (mgui_nCursorPos > 0 &&
00923 data_isOp(mgui_nCursorPos-1)) {
00924 data_Delete(mgui_nCursorPos-1);
00925 data_MoveElemLeft(mgui_nCursorPos-1);
00926 mgui_nCursorPos--;
00927 }
00928 }
00929
00930 wxPoint mcElementArrayHelpers::gui_GetOriginOfElem(int n, int cl) const
00931 {
00932 wxPoint p;
00933 if (cl == -1) cl = gui_GetCenterLine();
00934
00935
00936
00937 p.x = gui_CalcSizeOfFirst(n, cl).GetWidth();
00938
00939
00940 p.y = cl - data_Get(n).gui_GetYAnchor();
00941
00942 return p;
00943 }
00944
00945 wxSize mcElementArrayHelpers::gui_CalcSizeOfRange(int begin, int end, int cl) const
00946 {
00947 wxSize tmp, acc;
00948 int sb = gui_GetSpaceBetween();
00949 if (cl == -1) cl = gui_GetCenterLine();
00950
00951
00952
00953 for (int c=begin; c < end; c++) {
00954
00955
00956 const mcElement &g = data_Get(c);
00957 tmp.SetWidth(g.gui_GetWidth());
00958 tmp.SetHeight(cl - g.gui_GetYAnchor()+ g.gui_GetHeight());
00959
00960
00961
00962 acc.SetWidth(acc.GetWidth() + tmp.GetWidth() + sb);
00963 acc.SetHeight(mcMAX(acc.GetHeight(), tmp.GetHeight()));
00964 }
00965
00966
00967 return acc;
00968 }
00969
00970 wxSize mcElementArrayHelpers::gui_CalcSizeOfFirst(int nElem, int cl) const
00971 {
00972
00973
00974
00975
00976
00977 if (cl == -1) cl = gui_GetCenterLine();
00978 return gui_CalcSizeOfRange(0, nElem, cl);
00979 }
00980
00981 wxRect mcElementArrayHelpers::gui_GetSelectionRect(wxDC &, int x, int y, int cl) const
00982 {
00983 int sb = gui_GetSpaceBetween();
00984 if (cl == -1) cl = gui_GetCenterLine();
00985
00986
00987 int a=gui_GetSelStart(), b=gui_GetSelEnd();
00988
00989
00990 if (a == -1) return wxRect(0, 0, 0, 0);
00991
00992
00993 wxSize tmp = gui_CalcSizeOfRange(a, b, cl);
00994
00995
00996
00997 int miny = 0xFFFF;
00998 for (int i=a; i < b; i++)
00999 miny = mcMIN(miny, cl - data_Get(i).gui_GetYAnchor());
01000
01001
01002
01003 wxRect ret(x+gui_CalcSizeOfFirst(a, cl).GetWidth()-sb/2, y+miny+1,
01004 tmp.GetWidth()+sb, tmp.GetHeight()-miny-1);
01005
01006 return ret;
01007 }
01008
01009 int mcElementArrayHelpers::gui_DrawSelectedElements(wxDC &hDC, int x, int y,
01010 long flags, const wxPoint &pt, int cl) const
01011 {
01012
01013 int a=gui_GetSelStart(), b=gui_GetSelEnd();
01014
01015
01016
01017 return gui_ExDraw(hDC, x, y, flags, pt, cl, a, b, TRUE);
01018 }
01019
01020 int mcElementArrayHelpers::gui_DrawSelection(wxDC &hDC, int x, int y, long flags,
01021 const wxPoint &pt, int cl) const
01022 {
01023 wxPoint mypt(pt);
01024 int myflags = flags;
01025
01026 int n = mcDRW_NOACTIVEELEM;
01027 wxBrush *pbrush = wxWHITE_BRUSH;
01028 bool ret = FALSE;
01029
01030
01031 if (cl == -1) cl = gui_GetCenterLine();
01032
01033
01034 wxRect rc = gui_GetSelectionRect(hDC, x, y, cl);
01035
01036
01037 if (rc.width == 0 || rc.height == 0)
01038 return n;
01039
01040
01041
01042 bool bDrawRect = FALSE;
01043
01044
01045
01046 bDrawRect = this->gui_isAllSelected() && (flags & mcDRW_ALLOW_TOTAL_SELECTION);
01047
01048
01049
01050 if (!bDrawRect) {
01051
01052
01053
01054 int nn = data_GetCount();
01055 int ns = gui_GetSelElemCount();
01056
01057
01058
01059
01060 bool selectokay = TRUE;
01061 for (int i=0; i < ns; i++)
01062 selectokay &= gui_GetSelElem(i).gui_isAllSelected();
01063 bDrawRect |= (nn > ns && selectokay == TRUE);
01064 }
01065
01066 if (bDrawRect) {
01067
01068
01069
01070 if (flags & mcDRW_ALLACTIVE || (flags & mcDRW_USEPOINT && rc.Inside(pt))) {
01071
01072
01073 pbrush = mcElementHelpers::sgui_pSelectionBrush;
01074
01075
01076
01077
01078
01079
01080
01081
01082 mypt = wxDefaultPosition;
01083 myflags = myflags & ~mcDRW_USEPOINT;
01084
01085 n = mcDRW_ONSELECTION;
01086 }
01087
01088
01089
01090
01091
01092
01093 if (!sgui_bDrawSelectionInverting) {
01094 hDC.SetPen(*sgui_pSelectionPen);
01095 hDC.SetBrush(*pbrush);
01096 hDC.DrawRectangle(rc);
01097 }
01098
01099
01100
01101
01102 myflags = myflags & ~mcDRW_ALLOW_TOTAL_SELECTION;
01103
01104
01105
01106
01107
01108
01109 ret = FALSE;
01110
01111 } else {
01112
01113
01114
01115
01116
01117 mypt = pt;
01118
01119
01120
01121 ret = TRUE;
01122 }
01123
01124
01125 int n2 = gui_DrawSelectedElements(hDC, x, y, myflags, mypt, cl);
01126
01127
01128 #ifdef mcELEMENTARRAY_DRAW_DECORATIONS
01129
01130 hDC.gui_DrawBitmap(*mcMathCore::Get()->bmpBeginTri, rc.x, rc.y, TRUE);
01131 hDC.gui_DrawBitmap(*mcMathCore::Get()->bmpEndTri, rc.x+rc.width-
01132 mcMathCore::Get()->bmpEndTri.gui_GetWidth()-1, rc.y+rc.height-
01133 mcMathCore::Get()->bmpEndTri.gui_GetHeight()-1, TRUE);
01134
01135 #endif
01136
01137
01138
01139 if (bDrawRect && sgui_bDrawSelectionInverting) {
01140
01141 mcGUILOG(wxT("mcElementArrayHelpers::gui_DrawSelection [%s] - ")
01142 wxT("drawing the selection rectangle INVERTING"), mcTXTTHIS);
01143
01144
01145
01146
01147
01148
01149 int old = hDC.GetLogicalFunction();
01150 hDC.SetLogicalFunction(wxINVERT);
01151 hDC.DrawRectangle(rc);
01152 hDC.SetLogicalFunction(old);
01153 }
01154
01155
01156
01157
01158 return (ret == TRUE) ? n2 : n;
01159 }
01160
01161 int mcElementArrayHelpers::gui_GetBB(int n, wxRect *rc, int yCenter, int w) const
01162 {
01163 mcUNUSED(w);
01164
01165
01166 if (yCenter == -1) yCenter = gui_GetCenterLine();
01167
01168 wxPoint o = gui_GetOriginOfElem(n, yCenter);
01169 rc->x = o.x;
01170 rc->y = o.y;
01171 rc->height = data_Get(n).gui_GetHeight();
01172 rc->width = data_Get(n).gui_GetWidth();
01173
01174
01175 return rc->width + gui_GetSpaceBetween();
01176 }
01177
01178 int mcElementArrayHelpers::gui_GetCenterLine() const
01179 {
01180 int y=0;
01181
01182
01183 for (int i=0; i < data_GetCount(); i++)
01184 y = mcMAX(y, data_Get(i).gui_GetYAnchor());
01185 return y;
01186 }
01187
01188 void mcElementArrayHelpers::gui_DeleteSelection()
01189 {
01190 int i = gui_GetSelStart(), max = gui_GetSelEnd();
01191 if (max == -1) return;
01192
01193 if (max-i == 1) {
01194
01195
01196 mcElement m = gui_GetSelElem(0);
01197
01198 if (m.gui_isAllSelected()) {
01199
01200
01201 data_Delete(i);
01202 data_MoveElemLeft(i);
01203
01204 } else {
01205
01206
01207 m.gui_DeleteSelection();
01208 }
01209
01210 } else {
01211
01212
01213
01214 int todel = i;
01215 for (; i < max; i++) {
01216 mcASSERT(data_Get(todel).gui_isAllSelected(),
01217 wxT("extended selection implies elements completely selected..."));
01218 data_Delete(todel);
01219 data_MoveElemLeft(todel);
01220 }
01221 }
01222
01223 gui_RecalcSize();
01224 }
01225
01226
01227
01228
01229
01230
01231
01232
01233 void mcElementArrayHelpers::gui_DoRecalcSize()
01234 {
01235
01236
01237 mgui_sz = gui_CalcSizeOfFirst(data_GetCount(), -1);
01238 }
01239
01240 void mcElementArrayHelpers::gui_UpdateExpDepth()
01241 {
01242
01243
01244 for (int i=0; i < data_GetCount(); i++)
01245 data_Get(i).gui_SetAtSameLevelOf(this);
01246
01247
01248
01249 mcElementHelpers::gui_UpdateExpDepth();
01250 }
01251
01252 int mcElementArrayHelpers::gui_ExDraw(wxDC &dc, int x, int y, long flags, const wxPoint &pt,
01253 int cl, int b, int e, bool bDrawSelected) const
01254 {
01255 mcGUILOG(wxT("mcElementArrayHelpers::gui_ExDraw [%s]"), mcTXTTHIS);
01256
01257 int n=mcDRW_NOACTIVEELEM, w=0;
01258 wxRect rc;
01259
01260
01261 if (cl == -1) cl = gui_GetCenterLine();
01262
01263
01264 if (e == -1) e = data_GetCount();
01265
01266 if (b > 0) {
01267
01268
01269 w = gui_CalcSizeOfFirst(b, cl).GetWidth();
01270 }
01271
01272
01273
01274 int nonactiveflags = mcDRW_NONACTIVE;
01275 if (flags & mcDRW_ALLOW_TOTAL_SELECTION)
01276 nonactiveflags |= mcDRW_ALLOW_TOTAL_SELECTION;
01277
01278
01279 for (int i=b; i < e; i++) {
01280
01281
01282 w += gui_GetBB(i, &rc, cl, w);
01283
01284
01285 rc.Offset(x, y);
01286
01287
01288 if (data_Get(i).gui_isSelected()) {
01289
01290 if (!bDrawSelected) {
01291
01292
01293
01294
01295
01296 continue;
01297 }
01298 }
01299
01300
01301 if (flags & mcDRW_ALLACTIVE || (flags & mcDRW_USEPOINT && rc.Inside(pt))) {
01302
01303
01304 n = data_Get(i).gui_Draw(dc, rc.x, rc.y, flags, pt);
01305 mcGUILOG(wxT("mcElementArrayHelpers::gui_ExDraw - just drawn as active [%s] at (%d;%d)"),
01306 mcTXT(data_Get(i)), rc.x, rc.y);
01307
01308
01309
01310 if (!(flags & mcDRW_ALLACTIVE)) flags &= ~mcDRW_USEPOINT;
01311
01312 } else {
01313
01314
01315 data_Get(i).gui_Draw(dc, rc.x, rc.y, nonactiveflags, wxDefaultPosition);
01316 mcGUILOG(wxT("mcElementArrayHelpers::gui_ExDraw - just drawn as inactive [%s] at (%d;%d)"),
01317 mcTXT(data_Get(i)), rc.x, rc.y);
01318 }
01319 }
01320
01321
01322 #ifdef mcELEMENTARRAY_DBG_DRAWCENTERLINE
01323 dc.DrawLine(x, y+cl, x+gui_GetWidth(), y+cl);
01324 #endif
01325
01326
01327 return n;
01328 }
01329
01330 int mcElementArrayHelpers::gui_ExGetRelCursorPos(wxDC &hDC, wxPoint *pt, int cl) const
01331 {
01332 int n, n2, n3 = ((cl == -1) ? gui_GetCenterLine() : cl);
01333
01334
01335 if (data_GetCount() == 0)
01336 return 0;
01337 gui_CheckCursorPos();
01338
01339
01340 n = data_Get(mgui_nCursorPos).gui_GetRelCursorPos(hDC, pt);
01341 n2 = data_Get(mgui_nCursorPos).gui_GetYAnchor();
01342
01343
01344
01345 pt->x += gui_CalcSizeOfFirst(mgui_nCursorPos, cl).GetWidth();
01346
01347
01348
01349 pt->y += n3-n2;
01350
01351 return n;
01352 }
01353
01354 void mcElementArrayHelpers::gui_ExOnSelect(wxDC &dc, wxRect &rc, int cl)
01355 {
01356 int i, w=0, begin = 0xFFFF, end = -1;
01357 wxRect bb;
01358
01359 #if 0
01360 dc.SetPen(*wxBLACK_PEN);
01361 dc.SetBrush(*wxTRANSPARENT_BRUSH);
01362 dc.DrawRectangle(rc);
01363 #endif
01364
01365
01366 if (cl == -1) cl = gui_GetCenterLine();
01367
01368
01369 gui_DeSelect();
01370
01371
01372 for (i=0; i < data_GetCount(); i++) {
01373
01374
01375 w += gui_GetBB(i, &bb, cl, w);
01376
01377 if (bb.Intersects(rc)) {
01378
01379
01380
01381 wxRect tmp(rc);
01382 tmp.Offset(-bb.x, -bb.y);
01383
01384 data_Get(i).gui_OnSelect(dc, tmp);
01385 begin = mcMIN(begin, i);
01386 end = mcMAX(end, i+1);
01387
01388 }
01389
01390
01391
01392
01393
01394
01395
01396
01397 }
01398
01399
01400
01401
01402 if (end-begin > 1) {
01403
01404
01405
01406
01407 for (i=begin; i < end; i++)
01408 data_Get(i).gui_SelectAll();
01409
01410
01411
01412
01413 int a = gui_GetSelStart();
01414 if (a > 0 && data_isOp(a-1))
01415 data_Get(a-1).gui_SelectAll();
01416
01417
01418
01419 a = gui_GetSelEnd();
01420 if (a > 0 && data_isOp(a-1))
01421 data_Get(a-1).gui_DeSelect();
01422 }
01423
01424
01425
01426
01427
01428
01429
01430
01431 if (gui_GetSelElemCount() > 0)
01432 this->gui_Select();
01433 }
01434
01435 mcInputRes mcElementArrayHelpers::gui_Input(const mcKey &key, mcElement *pnew)
01436 {
01437 mcGUILOG(wxT("mcElementArrayHelpers::gui_Input - before processing: [%s]"), mcTXTTHIS);
01438
01439 mcInputRes res = mcIR_OKAY;
01440 mcElementType n;
01441 mcCursorPos pos;
01442
01443
01444 if (data_GetCount() == 0 || data_Get(mgui_nCursorPos).gui_isEndKey(key)) {
01445
01446
01447 n = mcElementHelpers::gui_isKeyBeginKey(key);
01448 if (n == mcET_INVALID) {
01449
01450
01451 mcMathCore::Get()->SyntaxError(wxT("Character not recognized"));
01452 return mcIR_OKAY;
01453 }
01454
01455
01456 if (!data_isElementAllowed(n)) {
01457
01458
01459 mcMathCore::Get()->SyntaxError(wxT("Element not allowed"));
01460 return mcIR_OKAY;
01461 }
01462
01463
01464 if (data_GetCount() == 0)
01465 pos = mcCP_BEGIN;
01466 else
01467 pos = data_Get(mgui_nCursorPos).gui_GetCursorPos();
01468
01469
01470
01471 if (pos.isBegin()) {
01472
01473
01474
01475
01476
01477
01478
01479 data_MoveElemRight(mgui_nCursorPos);
01480
01481
01482 mgui_nCursorPos = gui_AddNewElement(n, key, mgui_nCursorPos);
01483
01484 } else if (pos.isInside()) {
01485
01486
01487
01488
01489
01490
01491 gui_DoSplit(n, key);
01492
01493 } else if (pos.isEnd()) {
01494
01495 data_MoveElemRight(mgui_nCursorPos+1);
01496
01497 mgui_nCursorPos = gui_AddNewElement(n, key, mgui_nCursorPos+1);
01498
01499
01500 } else {
01501
01502 mcASSERT(0, wxT("Unhandled cursor position"));
01503 }
01504
01505 } else {
01506
01507
01508 res = data_Get(mgui_nCursorPos).gui_Input(key, pnew);
01509
01510
01511
01512 res = gui_BackInput(key, pnew, res);
01513 }
01514
01515
01516 gui_UpdateExpDepth();
01517
01518 gui_RecalcSize();
01519
01520
01521 data_Check();
01522 gui_CheckCursorPos();
01523
01524
01525 mcGUILOG(wxT("mcElementArrayHelpers::gui_Input - after processing: [%s]"), mcTXTTHIS);
01526 return res;
01527 }
01528
01529 void mcElementArrayHelpers::gui_MoveCursorLeft()
01530 {
01531 for (int i=mgui_nCursorPos-1; i >= 0; i--) {
01532 if (data_Get(i).gui_LetInCursor(mcCP_END)) {
01533 mgui_nCursorPos = i;
01534 return;
01535 }
01536 }
01537 }
01538
01539 void mcElementArrayHelpers::gui_MoveCursorRight()
01540 {
01541 for (int i=mgui_nCursorPos+1; i <= data_GetCount(); i++) {
01542 if (data_Get(i).gui_LetInCursor(mcCP_BEGIN)) {
01543 mgui_nCursorPos = i;
01544 return;
01545 }
01546 }
01547 }
01548
01549 mcMoveCursorRes mcElementArrayHelpers::gui_MoveCursor(mcMoveCursorFlag flag, long modifiers)
01550 {
01551 int result=0;
01552
01553
01554 gui_CheckCursorPos();
01555
01556
01557 result = data_Get(mgui_nCursorPos).gui_MoveCursor(flag, modifiers);
01558 if (data_Get(mgui_nCursorPos).gui_isSelected())
01559 gui_Select();
01560
01561
01562
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579 if (result == mcMCR_OKAY &&
01580 data_Get(mgui_nCursorPos).gui_GetCursorPos().isBegin()) {
01581
01582
01583
01584 if (mgui_nCursorPos > 0 && !data_isOp(mgui_nCursorPos-1)) {
01585
01586
01587 gui_MoveCursorLeft();
01588
01589
01590 }
01591 }
01592
01593
01594
01595 switch (result) {
01596 case mcMCR_SETFOCUS_PREVIOUS:
01597
01598 if (mgui_nCursorPos > 0) {
01599
01600
01601
01602
01603
01604
01605
01606
01607 bool b = !data_isOp(mgui_nCursorPos) && !data_isOp(mgui_nCursorPos-1);
01608
01609 gui_MoveCursorLeft();
01610 if (b) gui_MoveCursor(mcMCF_LEFT, modifiers);
01611
01612 } else {
01613
01614
01615
01616 return mcMCR_SETFOCUS_PREVIOUS;
01617 }
01618 break;
01619
01620 case mcMCR_SETFOCUS_NEXT:
01621
01622 if (mgui_nCursorPos < data_GetCount()-1) {
01623
01624
01625
01626
01627 bool b = !data_isOp(mgui_nCursorPos) && !data_isOp(mgui_nCursorPos+1);
01628
01629 gui_MoveCursorRight();
01630 if (b) gui_MoveCursor(mcMCF_RIGHT, modifiers);
01631
01632 } else {
01633
01634
01635
01636 return mcMCR_SETFOCUS_NEXT;
01637 }
01638 break;
01639
01640
01641 case mcMCR_SETFOCUS_BELOW:
01642 return mcMCR_SETFOCUS_BELOW;
01643 case mcMCR_SETFOCUS_ABOVE:
01644 return mcMCR_SETFOCUS_ABOVE;
01645 }
01646
01647 return mcMCR_OKAY;
01648 }
01649
01650 int mcElementArrayHelpers::gui_ExMoveCursorUsingPoint(wxDC &hDC, const wxPoint &pt, int cl)
01651 {
01652
01653 if (pt.x > gui_GetWidth() || pt.y > gui_GetHeight())
01654 return mcMCR_CANNOT_SETFOCUS;
01655
01656
01657 if (cl == -1) cl = gui_GetCenterLine();
01658 int w=0;
01659 wxRect rc;
01660
01661 for (int i=0; i < data_GetCount(); i++) {
01662
01663
01664 w += gui_GetBB(i, &rc, cl, w);
01665
01666
01667 if (rc.Inside(pt) == TRUE) {
01668
01669
01670
01671
01672
01673 if (data_isOp(i)) {
01674
01675
01676
01677 mgui_nCursorPos = (i-1 >= 0) ? i-1 : i+1;
01678 data_Get(mgui_nCursorPos).gui_LetInCursor(mcCP_END);
01679
01680
01681 return mcMCR_OKAY;
01682 }
01683
01684
01685 mgui_nCursorPos = i;
01686 gui_CheckCursorPos();
01687
01688
01689 wxPoint p(pt.x-rc.x-gui_GetSpaceBetween(), pt.y-rc.y);
01690 data_Get(mgui_nCursorPos).gui_MoveCursorUsingPoint(hDC, p);
01691
01692
01693 return mcMCR_OKAY;
01694 }
01695 }
01696
01697
01698 return mcMCR_CANNOT_SETFOCUS;
01699 }
01700
01701 void mcElementArrayHelpers::gui_GetCursorPos(mcCursorPos &cp) const
01702 {
01703
01704 if (data_GetCount() == 0) {
01705 cp.gui_Push(mcCP_BEGINEND);
01706 return;
01707 }
01708
01709 if (mgui_nCursorPos == 0) {
01710 mcCursorPos r(data_Get(0).gui_GetCursorPos());
01711
01712 if (r.isBegin() || r.isBeginEnd()) {
01713 cp.gui_Push(mcCP_BEGIN);
01714 return;
01715 }
01716 }
01717
01718
01719
01720
01721 if (mgui_nCursorPos == 1 && data_isOp(0)) {
01722 mcCursorPos r(data_Get(1).gui_GetCursorPos());
01723
01724 if (r.isBegin() || r.isBeginEnd()) {
01725 cp.gui_Push(mcCP_BEGIN);
01726 return;
01727 }
01728 }
01729
01730
01731 int n = data_GetCount()-1;
01732 if (mgui_nCursorPos == n) {
01733 mcCursorPos r(data_Get(n).gui_GetCursorPos());
01734
01735 if (r.isEnd() || r.isBeginEnd()) {
01736 cp.gui_Push(mcCP_END);
01737 return;
01738 }
01739 }
01740
01741
01742 data_Get(mgui_nCursorPos).gui_GetCursorPos(cp);
01743 }
01744
01745 void mcElementArrayHelpers::gui_SetCursorPos(const mcCursorPos &code)
01746 {
01747 if (data_isArrayEmpty())
01748 return;
01749
01750
01751 if (code.isBegin()) {
01752
01753 mgui_nCursorPos = 0;
01754
01755
01756 if (data_isOp(mgui_nCursorPos))
01757 mgui_nCursorPos++;
01758 data_Get(mgui_nCursorPos).gui_SetCursorPos(mcCP_BEGIN);
01759
01760 } else if (code.isEnd()) {
01761
01762 mgui_nCursorPos = data_GetCount()-1;
01763
01764
01765 if (data_isOp(mgui_nCursorPos))
01766 mgui_nCursorPos--;
01767 data_Get(mgui_nCursorPos).gui_SetCursorPos(mcCP_END);
01768
01769 } else {
01770
01771 mcASSERT(0, wxT("Couldn't accept these flags"));
01772 }
01773 }
01774
01775 mcElement mcElementArrayHelpers::gui_GetSelection() const
01776 {
01777 int n = gui_GetSelElemCount();
01778
01779
01780 if (n == 0)
01781 return mcEmptyElement;
01782
01783 if (n == 1) {
01784
01785
01786 mcElement tmp(gui_GetSelElem(0).gui_GetSelection());
01787 mcASSERT(tmp != mcEmptyElement, wxT("Something wrong"));
01788
01789
01790
01791
01792 if (tmp.data_GetType() == mcET_POLYNOMIAL ||
01793 tmp.data_GetType() == data_GetType())
01794 return tmp;
01795
01796
01797
01798 }
01799
01800
01801
01802 mcElementArray sel(mcElementHelpers::data_NewElem(data_GetType()));
01803
01804
01805
01806 for (int i=0; i < n; i++) {
01807
01808 mcElement toinsert(gui_GetSelElem(i).gui_GetSelection());
01809 mcASSERT(toinsert != mcEmptyElement, wxT("We should have a valid selection !"));
01810
01811 toinsert.data_MakePrivateCopy();
01812 sel.data_AddElements(&toinsert, 1, -1, TRUE);
01813 }
01814
01815 return sel;
01816 }
01817
01818 int mcElementArrayHelpers::gui_GetActiveElemIndex(int x, int y, const wxPoint &pt, int cl)
01819 {
01820 wxRect rc;
01821 int w=0;
01822
01823
01824 if (cl == -1) cl = gui_GetCenterLine();
01825
01826
01827 for (int i=0; i < data_GetCount(); i++) {
01828
01829
01830 w += gui_GetBB(i, &rc, cl, w);
01831
01832
01833 rc.Offset(x, y);
01834
01835
01836 if (rc.Inside(pt)) {
01837 if (data_Get(i).gui_isSelected())
01838 return mcDRW_ONSELECTION;
01839 return i;
01840 }
01841 }
01842
01843
01844 return mcDRW_NOACTIVEELEM;
01845 }
01846
01847 void mcElementArrayHelpers::gui_AddNewOp(mcElementType nOpType, int pos)
01848 {
01849
01850
01851 mcKey fake(mcOperatorHelpers::data_GetOpSymbol(nOpType).GetChar(0));
01852 gui_AddNewElement(nOpType, fake, pos);
01853 }
01854
01855
01856
01857
01858
01859
01860
01861
01862
01863
01864
01865
01866
01867
01868
01869
01870
01871
01872
01873
01874
01875
01876
01877
01878
01879
01880
01881
01882
01883
01884 wxString mcElementArrayHelpers::io_GetInlinedExpr() const
01885 {
01886 wxString str;
01887
01888
01889 for (int i=0; i < data_GetCount(); i++)
01890 str += data_Get(i).io_GetInlinedExpr();
01891
01892 return str;
01893 }
01894
01895
01896
01897
01898
01899
01900
01901
01902
01903 void mcElementArrayHelpers::math_EmbedInBracketAndRaiseTo(const mcRealValue &n)
01904 { math_EmbedInBracket().data_GetBracket().math_RaiseTo(mcPolynomial(n)); }
01905
01906 void mcElementArrayHelpers::math_EmbedInBracketAndRaiseTo(const mcPolynomial &p)
01907 { math_EmbedInBracket().data_GetBracket().math_RaiseTo(p); }
01908
01909 void mcElementArrayHelpers::math_EmbedInRadicalAndRaiseTo(const mcPolynomial &p)
01910 { math_EmbedInRadical().data_GetBracket().math_RaiseTo(p); }
01911
01912 int mcElementArrayHelpers::math_GetIndexOf(int occ, const mcElement &tofind) const
01913 {
01914 int idx = math_NonRecursiveFindInChildren(occ, tofind);
01915 if (idx == -1) return -1;
01916
01917
01918
01919 return math_DataToMathIdx(idx);
01920 }
01921
01922 mcArrayEntry mcElementArrayHelpers::math_WrapSymbol(const mcSymbolProperties *sym)
01923 {
01924 mcSymbol s(sym->data_GetSafeLinkedSym());
01925 mcASSERT(s.data_isOk(), wxT("Invalid safe symbol ?"));
01926 return math_WrapSimple(s);
01927 }
01928
01929 mcArrayEntry mcElementArrayHelpers::math_WrapNumber(const mcRealValue &val)
01930 {
01931 mcNumber n(val);
01932 return math_WrapSimple(n);
01933 }
01934
01935 const mcSymbolProperties *mcElementArrayHelpers::math_GetWrappedSymbol() const
01936 {
01937 mcSymbol sym(math_GetWrapped(mcET_SYMBOL));
01938 if (sym != mcEmptyElement)
01939 return sym.data_GetProperties();
01940 return NULL;
01941 }
01942
01943 mcRealValue mcElementArrayHelpers::math_GetWrappedNumber() const
01944 {
01945 const mcNumber num(math_GetWrapped(mcET_NUMBER));
01946 if (num != mcEmptyElement)
01947 return num.data_Get();
01948 return *mcRealValue::pNAN;
01949 }
01950
01951 void mcElementArrayHelpers::math_ResetToOne()
01952 {
01953
01954 data_DeleteAll();
01955
01956
01957 mcASSERT(mcNumberHelpers::smath_pOne, wxT("All statics should be ready..."));
01958 math_WrapSimple(*mcNumberHelpers::smath_pOne);
01959 }
01960
01961 void mcElementArrayHelpers::math_ResetToZero()
01962 {
01963
01964 data_DeleteAll();
01965
01966
01967 mcASSERT(mcNumberHelpers::smath_pZero, wxT("All statics should be ready..."));
01968 math_WrapSimple(*mcNumberHelpers::smath_pZero);
01969 }
01970
01971 mcArrayEntry mcElementArrayHelpers::math_EmbedInBracket()
01972 {
01973
01974 mcBracket br;
01975 mcPolynomial pol(math_GetPolynomialWrapper());
01976 br.data_SetContent(pol);
01977
01978
01979 data_DeleteAll();
01980
01981
01982 mcArrayEntry ourb = math_WrapSimple(br);
01983 mcASSERT(ourb.data_GetRef().data_GetType() == mcET_BRACKET, wxT("What is this ?!?!?"));
01984
01985 return ourb;
01986 }
01987
01988 mcArrayEntry mcElementArrayHelpers::math_EmbedInRadical()
01989 {
01990
01991 mcRadical rad;
01992 mcPolynomial pol(math_GetPolynomialWrapper());
01993 rad.data_SetContent(pol);
01994
01995
01996 data_DeleteAll();
01997
01998
01999 mcArrayEntry ourb = math_WrapSimple(rad);
02000 mcASSERT(ourb.data_GetRef().data_GetType() == mcET_RADICAL, wxT("What is this ?!?!?"));
02001
02002 return ourb;
02003 }
02004
02005 mcArrayEntry mcElementArrayHelpers::math_EmbedInFraction(bool bAsDenominator)
02006 {
02007
02008 mcFraction pelem;
02009 mcPolynomial pol = math_GetPolynomialWrapper();
02010
02011 if (bAsDenominator) {
02012
02013
02014
02015 pelem.data_SetDen(pol);
02016 pelem.data_SetNum(*mcPolynomialHelpers::smath_pOne);
02017
02018 } else {
02019
02020
02021 pelem.data_SetNum(pol);
02022 pelem.data_SetDen(*mcPolynomialHelpers::smath_pOne);
02023 }
02024
02025
02026 data_DeleteAll();
02027 mcMATHLOG(wxT("mcElementArrayHelpers::math_EmbedInFraction - the fraction is [%s]"), mcTXT(pelem));
02028
02029
02030 mcArrayEntry ourf = math_WrapSimple(pelem);
02031 data_Check();
02032
02033
02034 mcASSERT(ourf.data_GetRef().data_GetType() == mcET_FRACTION, wxT("What is this ?!?!?"));
02035 return ourf;
02036 }
02037
02038 mcRealValue mcElementArrayHelpers::math_Evaluate() const
02039 {
02040 mcMATHLOG(wxT("mcElementArrayHelpers::math_Evaluate [%s]"), mcTXTTHIS);
02041
02042
02043 if (data_isArrayEmpty())
02044 return math_GetNeutralValue();
02045
02046
02047
02048
02049
02050
02051
02052
02053
02054
02055 mcRealValue acc = math_Get(0).math_Evaluate();
02056 if (!acc.isValid()) return *mcRealValue::pNAN;
02057
02058
02059
02060
02061 if (data_isOp(0))
02062 acc = mcOperator(data_Get(0)).math_Evaluate(math_GetNeutralValue(), acc);
02063
02064 for (int i=0, max=math_GetCount()-1; i < max; i++) {
02065
02066
02067
02068 mcRealValue tmp = math_Get(i+1).math_Evaluate();
02069
02070
02071 if (tmp.isNAN() || acc.isNAN())
02072 return *mcRealValue::pNAN;
02073
02074 acc = math_GetOpBetween(i, i+1).math_Evaluate(acc, tmp);
02075 }
02076
02077 return acc;
02078 }
02079
02080 mcBasicOpRes mcElementArrayHelpers::math_MakeReciprocal(mcElement *)
02081 {
02082
02083 math_EmbedInFraction(TRUE);
02084
02085 return mcBOR_REMOVE_OPERAND;
02086 }
02087
02088 int mcElementArrayHelpers::math_ReorderElements(mcElement *arr, int nelements,
02089 int start, int toskip)
02090 {
02091 int mov = 0;
02092
02093
02094
02095
02096
02097
02098
02099
02100
02101
02102
02103
02104
02105
02106
02107 for (int i=start; i < nelements; i+=toskip) {
02108
02109 mcElement removedop(mcEmptyElement);
02110 mcElement removed = arr[i];
02111 if (i > 0 && mcOperatorHelpers::data_isOp(arr[i-1].data_GetType()))
02112 removedop = arr[i-1];
02113
02114 int insert = i;
02115
02116
02117
02118
02119
02120
02121
02122
02123
02124
02125 while (insert > start) {
02126
02127
02128 mcElement previous = arr[insert-toskip];
02129 mcASSERT(!mcOperatorHelpers::data_isOp(previous.data_GetType()),
02130 wxT("I've found two adiacent operators..."));
02131
02132
02133 bool cont = removed.math_isListedBeforeOf(previous);
02134 if (!cont) break;
02135
02136
02137 arr[insert] = arr[insert-toskip];
02138 if (insert-toskip > 0)
02139 arr[insert-1] = arr[insert-toskip-1];
02140 insert-=toskip;
02141
02142
02143 mov++;
02144 }
02145
02146 if (removedop != mcEmptyElement) {
02147
02148
02149 mcASSERT(insert > 0, wxT("Something wrong"));
02150 arr[insert-1] = removedop;
02151 }
02152
02153
02154 arr[insert] = removed;
02155 }
02156
02157 return mov;
02158 }
02159
02160 bool mcElementArrayHelpers::math_isWrappingOnly(mcElementType t) const
02161 {
02162
02163
02164
02165 if (math_GetCount() > 1)
02166 return FALSE;
02167 if (data_Get(0).data_GetType() == t)
02168 return TRUE;
02169 return FALSE;
02170 }
02171
02172 const mcElement &mcElementArrayHelpers::math_GetWrapped(mcElementType t) const
02173 {
02174 if (!math_isWrappingOnly(t))
02175 return mcEmptyElement;
02176 return math_Get(0);
02177 }
02178
02179
02180
02181
02182
02183
02184
02185
02186
02187 mcExpSimRes mcElementArrayHelpers::math_HandleExpSimFlag(mcExpSimRes r,
02188 mcElement *pnew, int i)
02189 {
02190
02191
02192 data_CheckIndex(i);
02193
02194 switch (r) {
02195 case mcESR_REPLACE_THIS:
02196 mcASSERT(pnew, wxT("What should I use as replacement ?"));
02197 if ((*pnew).data_GetType() == data_GetType()) {
02198
02199
02200 data_Delete(i);
02201 data_MoveElemLeft(i);
02202
02203
02204 mcElementArray m(*pnew);
02205 data_AddElements(m.data_GetArray(), m.data_GetCount(), i);
02206
02207
02208
02209
02210
02211 m.data_DetachAll();
02212
02213 } else {
02214
02215
02216 data_AddElements(pnew, 1, i, TRUE);
02217 }
02218
02219 return mcESR_NOTFINISHED;
02220
02221 case mcESR_DELETE_THIS:
02222 data_Delete(i);
02223 data_MoveElemLeft(i);
02224 return mcESR_NOTFINISHED;
02225
02226 case mcESR_NOTFINISHED:
02227 return mcESR_NOTFINISHED;
02228
02229 case mcESR_DONE:
02230 break;
02231
02232 case mcESR_INVALID_DATA:
02233 case mcESR_DISTRIBUTE:
02234 mcASSERT(0, wxT("Cannot handle this return flag..."));
02235
02236 default:
02237 mcASSERT(0, wxT("Unhandled return flag"));
02238 }
02239
02240
02241 return mcESR_DONE;
02242 }
02243
02244 #define mcIMPLEMENT_EXPSIM_FUNCTION(x, y, cond) \
02245 mcExpSimRes mcElementArrayHelpers::x(long flags, mcElement *pp) \
02246 { \
02247 bool stilltowork = FALSE; \
02248 mcElement pnew; \
02249 \
02250 mcMATHLOG(wxT("mcElementArrayHelpers::") wxT(#x) \
02251 wxT(" [%s]"), mcTXTTHIS); \
02252 \
02253 \
02254 \
02255 \
02256 for (int i=0; i < data_GetCount(); i++) { \
02257 \
02258 \
02259 if (cond) { \
02260 mcMATHLOG(wxT("mcElementArrayHelpers::") wxT(#x) \
02261 wxT(" - I won't process [%s]"), mcTXT(data_Get(i))); \
02262 continue; \
02263 } \
02264 \
02265 mcMATHLOG(wxT("mcElementArrayHelpers::") wxT(#x) \
02266 wxT(" - I'm going to process [%s]"), mcTXT(data_Get(i))); \
02267 mcExpSimRes r = data_Get(i).y(flags, &pnew); \
02268 \
02269 \
02270 \
02271 \
02272 mcExpSimRes res = math_HandleExpSimFlag(r, &pnew, i); \
02273 \
02274 \
02275 switch (res) { \
02276 case mcESR_NOTFINISHED: \
02277 stilltowork = TRUE; \
02278 break; \
02279 case mcESR_CHANGE_SIGN: \
02280 return mcESR_CHANGE_SIGN; \
02281 case mcESR_REPLACE_THIS: \
02282 mcASSERT(pp, wxT("Need a valid pointer")); \
02283 *pp=pnew; \
02284 return mcESR_REPLACE_THIS; \
02285 case mcESR_DISTRIBUTE: \
02286 mcASSERT(pp, wxT("Need a valid pointer")); \
02287 *pp=pnew; \
02288 return mcESR_DISTRIBUTE; \
02289 \
02290 default: \
02291 \
02292 \
02293 mcASSERT(res == mcESR_DONE, \
02294 wxT("Bug in math_HandleExpSimFlag ?")); \
02295 break; \
02296 } \
02297 } \
02298 \
02299 \
02300 data_Check(); \
02301 \
02302 \
02303 \
02304 if (stilltowork) \
02305 return mcESR_NOTFINISHED; \
02306 return mcESR_DONE; \
02307 }
02308
02309 mcIMPLEMENT_EXPSIM_FUNCTION(math_SimplifyAll, math_Simplify, 0)
02310 mcIMPLEMENT_EXPSIM_FUNCTION(math_SimplifyExp, math_Expand,
02311 !math_SimplifyNeedExp(flags, i))
02312
02313 bool mcElementArrayHelpers::math_SimplifyNeedExp(long flags, int i) const
02314 {
02315 const mcElement &p = data_Get(i);
02316
02317 if (flags & mcEXPSIM_KEEP_FACTORIZATION)
02318 return FALSE;
02319
02320
02321
02322
02323
02324
02325 if (p.math_ContainsSymbols())
02326 return TRUE;
02327 return FALSE;
02328 }
02329
02330 mcExpSimRes mcElementArrayHelpers::math_SimplifyRemoveNeutrals(long flags)
02331 {
02332 mcMATHLOG(wxT("mcElementArrayHelpers::math_SimplifyRemoveNeutrals"));
02333 bool stilltowork = FALSE;
02334
02335
02336
02337
02338
02339 if (data_GetCount() > 1) {
02340
02341
02342 for (int i=0; i < data_GetCount(); i++) {
02343
02344
02345
02346
02347
02348 if (data_Get(i).math_EvaluatesTo(math_GetNeutralValue())) {
02349
02350
02351 mcLOG(wxT("mcElementArrayHelpers::math_SimplifyRemoveNeutrals -")
02352 wxT(" removing the %dth element"), i);
02353 math_Remove(math_DataToMathIdx(i));
02354 stilltowork = TRUE;
02355 }
02356 }
02357 }
02358
02359 if (stilltowork)
02360 return mcESR_NOTFINISHED;
02361 return mcESR_DONE;
02362 }
02363
02364 void mcElementArrayHelpers::math_HandleBasicOpRes(mcBasicOpRes res, mcElement replacement,
02365 int repidx)
02366 {
02367 mcMATHLOG(wxT("mcElementArrayHelpers::math_HandleBasicOpRes [%s]"), mcTXTTHIS);
02368
02369 mcElementType newop = mcET_INVALID;
02370 if (res == mcBOR_REPLACE_OPERAND_AND_SET_MULTOP) newop = mcET_MULTOP;
02371 if (res == mcBOR_REPLACE_OPERAND_AND_SET_DIVOP) newop = mcET_DIVOP;
02372 if (res == mcBOR_REPLACE_OPERAND_AND_SET_ADDOP) newop = mcET_ADDOP;
02373 if (res == mcBOR_REPLACE_OPERAND_AND_SET_SUBOP) newop = mcET_SUBOP;
02374
02375 switch (res) {
02376 case mcBOR_INVALID:
02377 default:
02378 mcASSERT(0, wxT("Something is wrong... cannot handle this flag"));
02379 break;
02380
02381 case mcBOR_REPLACE_OPERAND:
02382 case mcBOR_REPLACE_OPERAND_AND_SET_MULTOP:
02383 case mcBOR_REPLACE_OPERAND_AND_SET_DIVOP:
02384 case mcBOR_REPLACE_OPERAND_AND_SET_ADDOP:
02385 case mcBOR_REPLACE_OPERAND_AND_SET_SUBOP:
02386 {
02387
02388
02389 int repdataidx = math_MathToDataIdx(repidx);
02390
02391
02392 mcASSERT(replacement != mcEmptyElement, wxT("Invalid pointer"));
02393 data_AddElements(&replacement, 1, repdataidx, TRUE);
02394 data_Check();
02395
02396 if (newop != mcET_INVALID) {
02397
02398 mcASSERT(repdataidx > 0, wxT("There is no operator we can change..."));
02399
02400
02401
02402 if (!data_isOp(repdataidx-1))
02403 data_MoveElemRight(repdataidx-1);
02404 else
02405 data_Delete(repdataidx-1);
02406
02407 mcElement pnewop = mcElementHelpers::data_NewElem(newop);
02408 data_AddElements(&pnewop, 1, repdataidx-1);
02409 }
02410 }
02411 break;
02412
02413 case mcBOR_REMOVE_OPERAND:
02414
02415
02416
02417
02418
02419
02420
02421
02422
02423 math_Remove(repidx);
02424 }
02425 }
02426
02427 mcExpSimRes mcElementArrayHelpers::math_SimplifySolveOp(long flags)
02428 {
02429 mcMATHLOG(wxT("mcElementArrayHelpers::math_SimplifySolveOp [%s]"), mcTXTTHIS);
02430 bool stilltowork = FALSE;
02431
02432
02433 for (int i=0; i < math_GetCount(); i++) {
02434
02435
02436 mcElement &left = math_Get(i);
02437
02438 for (int j=0; j < math_GetCount(); j++) {
02439
02440
02441 mcElement &right = math_Get(j);
02442 if (i == j) continue;
02443
02444
02445 mcOperator p = math_GetOpPreceding(j);
02446 if (p == mcEmptyElement) continue;
02447 mcMATHLOG(wxT("mcElementArrayHelpers::math_SimplifySolveOp - I'm trying to apply ")
02448 wxT("the op [%s] between the [%s] and [%s] elements"),
02449 mcTXT(p), mcTXT(left), mcTXT(right));
02450
02451
02452 mcElement rep = mcEmptyElement;
02453 mcBasicOpRes res = p.math_Apply(left, right, &rep);
02454 if (res == mcBOR_INVALID)
02455 continue;
02456
02457
02458 stilltowork = TRUE;
02459 mcMATHLOG(wxT("mcElementArrayHelpers::math_SimplifySolveOp - applied ")
02460 wxT("the op [%s] between the [%s] and [%s] elements"),
02461 mcTXT(p), mcTXT(left), mcTXT(right));
02462
02463
02464 math_HandleBasicOpRes(res, rep, j);
02465 }
02466 }
02467
02468 if (stilltowork)
02469 return mcESR_NOTFINISHED;
02470 return mcESR_DONE;
02471 }
02472
02473 mcExpSimRes mcElementArrayHelpers::math_Simplify(long flags, mcElement *pnew)
02474 {
02475 mcExpSimRes ret;
02476
02477 #define SIMCHECK_EXIT if (ret != mcESR_DONE) { math_EndSimSteps(); return ret; }
02478
02479 mcMATHLOG(wxT("mcElementArrayHelpers::math_Simplify [%s] start..."), mcTXTTHIS);
02480
02481
02482 ret = math_BeginSimSteps();
02483 SIMCHECK_EXIT;
02484
02485
02486 ret = math_SimplifyRemoveNeutrals(flags);
02487 SIMCHECK_EXIT;
02488
02489
02490 ret = math_Reorder();
02491 SIMCHECK_EXIT;
02492
02493
02494 ret = math_SimplifyAll(flags, pnew);
02495 SIMCHECK_EXIT;
02496
02497
02498 ret = math_SimplifyRemoveNeutrals(flags);
02499 SIMCHECK_EXIT;
02500
02501
02502 ret = math_SimplifySolveOp(flags);
02503 SIMCHECK_EXIT;
02504
02505
02506 ret = math_SimplifyExp(flags, pnew);
02507 SIMCHECK_EXIT;
02508
02509
02510 ret = math_FinalSimSteps();
02511 SIMCHECK_EXIT;
02512
02513
02514 data_Check();
02515 data_Repair();
02516
02517
02518 mcMATHLOG(wxT("mcElementArrayHelpers::math_Simplify - ended..."));
02519 return mcESR_DONE;
02520 }
02521
02522
02523
02524
02525
02526
02527
02528
02529
02530
02531 mcIMPLEMENT_EXPSIM_FUNCTION(math_ExpandAll, math_Expand, 0)
02532
02533 mcExpSimRes mcElementArrayHelpers::math_Expand(long flags, mcElement *pnew)
02534 {
02535 mcExpSimRes ret;
02536 #define EXPCHECK_EXIT if (ret != mcESR_DONE) { math_EndExpSteps(); return ret; }
02537
02538 mcMATHLOG(wxT("mcElementArrayHelpers::math_Expand [%s] start..."), mcTXTTHIS);
02539
02540
02541 ret = math_ExpandAll(flags, pnew);
02542 EXPCHECK_EXIT
02543
02544
02545 data_Check();
02546
02547
02548 return mcESR_DONE;
02549 }
02550
02551
02552
02553
02554
02555
02556
02557
02558
02559
02560 bool mcElementArrayHelpers::math_CanBeAddedWith(const mcElement &p) const
02561 {
02562
02563
02564
02565 if (p.data_GetType() == data_GetType())
02566 return TRUE;
02567 return FALSE;
02568 }
02569
02570 bool mcElementArrayHelpers::math_CanBeDivBy(const mcElement &p) const
02571 { return math_CanBeAddedWith(p); }
02572
02573 bool mcElementArrayHelpers::math_CanBeMultWith(const mcElement &p) const
02574 { return math_CanBeAddedWith(p); }
02575
02576 mcElementArray mcElementArrayHelpers::math_CreateWrapperFor(const mcElement &toembed) const
02577 {
02578
02579 if (toembed.data_GetType() == data_GetType())
02580 return mcElementArray(toembed);
02581
02582
02583
02584 mcElementArray tmp = mcElementHelpers::data_NewElem(data_GetType());
02585 tmp.data_AddElements(&toembed, 1);
02586
02587
02588 return tmp;
02589 }
02590
02591 void mcElementArrayHelpers::math_SimpleMultiplyBy(const mcElement &p)
02592 {
02593 mcElementArray tmp = math_CreateWrapperFor(p);
02594 mcElementHelpers::math_SimpleMultiplyBy(tmp);
02595 }
02596
02597 void mcElementArrayHelpers::math_SimpleDivideBy(const mcElement &p)
02598 {
02599 mcElementArray tmp = math_CreateWrapperFor(p);
02600 mcElementHelpers::math_SimpleDivideBy(tmp);
02601 }
02602
02603 void mcElementArrayHelpers::math_SimpleAdd(const mcElement &p, bool add)
02604 {
02605
02606 mcElementArray tmp = math_CreateWrapperFor(p);
02607 mcElementHelpers::math_SimpleAdd(tmp, add);
02608 }
02609
02610
02611
02612
02613
02614
02615
02616
02617
02618
02619 bool mcElementArrayHelpers::math_Remove(int idx, bool bAddZero)
02620 {
02621 int n = math_MathToDataIdx(idx);
02622 mcASSERT(n != -1, wxT("Invalid index !"));
02623
02624
02625 data_Delete(n);
02626 data_MoveElemLeft(n);
02627
02628
02629
02630
02631 if (n > 0 && data_isOp(n-1)) {
02632
02633 data_Delete(n-1);
02634 data_MoveElemLeft(n-1);
02635
02636 } else {
02637
02638
02639
02640
02641
02642 if (n == 0 && !data_isArrayEmpty() && data_isOp(n) &&
02643 data_Get(n).data_GetType() == math_GetNeutralOpType()) {
02644
02645
02646 data_Delete(n);
02647 data_MoveElemLeft(n);
02648 }
02649
02650
02651
02652
02653
02654
02655
02656
02657
02658
02659
02660
02661 if (n == 0 && !data_isArrayEmpty() &&
02662 data_Get(n).data_GetType() == mcET_DIVOP) {
02663
02664 mcASSERT(data_GetType() == mcET_MONOMIAL, wxT("A mcDivOp inside a mcPolynomial ?"));
02665 data_MoveElemRight(0);
02666 data_Set(0, *mcNumberHelpers::smath_pOne);
02667 }
02668 }
02669
02670
02671
02672 if (data_isArrayEmpty()) {
02673
02674
02675
02676 if (bAddZero)
02677 math_WrapSimple(*mcNumberHelpers::smath_pZero);
02678 else
02679 math_WrapSimple(*mcNumberHelpers::smath_pOne);
02680
02681
02682 return TRUE;
02683 }
02684
02685
02686 return FALSE;
02687 }
02688
02689 bool mcElementArrayHelpers::math_Delete(int n, const mcElement &elem, bool bmath_AddToZero)
02690 {
02691
02692 int idx = math_NonRecursiveFindInChildren(n, elem.hlp());
02693 if (idx == -1)
02694 return FALSE;
02695
02696 math_Remove(idx, bmath_AddToZero);
02697 return TRUE;
02698 }
02699
02700 void mcElementArrayHelpers::math_ApplyOpSimple(mcElementType optype, mcElement res,
02701 const mcElement &factor)
02702 {
02703 mcOperator tmp = mcElementHelpers::data_NewElem(optype);
02704 mcLOG(wxT("mcElementArrayHelpers::math_ApplyOpmath_Simple - applying the %s op"), mcTXT(tmp));
02705
02706 tmp.math_ApplySimple(res, factor);
02707 }
02708
02709 void mcElementArrayHelpers::math_ApplyOp(mcElementType optype, mcElement res,
02710 const mcElement &factor, mcElement *rep)
02711 {
02712 mcOperator tmp = mcElementHelpers::data_NewElem(optype);
02713 mcLOG(wxT("mcElementArrayHelpers::math_ApplyOp - applying the %s op"), mcTXT(tmp));
02714
02715 tmp.math_Apply(res, factor, rep);
02716 }
02717
02718 mcMathType mcElementArrayHelpers::math_GetMathType() const
02719 {
02720 mcMathType res(mcMTL1_POLYNOMIAL, mcMTL2_ALGEBRAIC, mcMTL3_CONSTANT);
02721
02722 if (data_isArrayEmpty())
02723 return res;
02724
02725
02726 res = data_Get(0).math_GetMathType();
02727 for (int i=0, max=math_GetCount()-1; i < max; i++) {
02728
02729 mcMathType next = data_Get(i+1).math_GetMathType();
02730
02731
02732 mcElementType op = math_GetOpTypeBetween(i, i+1);
02733 res.math_ApplyOp(op, next);
02734 }
02735
02736 return res;
02737 }
02738
02739 mcOperator &mcElementArrayHelpers::math_GetOpBetween(int n1, int n2) const
02740 {
02741 mcElement *p = mcElementHelpers::data_GetInstanceOf(math_GetOpTypeBetween(n1, n2));
02742 mcASSERT(mcOperatorHelpers::data_isOp(p->data_GetType()),
02743 wxT("math_GetOpTypeBetween() not returning an operator type ?"));
02744 return (mcOperator &)*p;
02745 }
02746
02747 mcOperator &mcElementArrayHelpers::math_GetOpPreceding(int n) const
02748 {
02749 mcElement *p = mcElementHelpers::data_GetInstanceOf(math_GetOpTypePreceding(n));
02750 mcASSERT(mcOperatorHelpers::data_isOp(p->data_GetType()),
02751 wxT("math_GetOpPreceding() not returning an operator type ?"));
02752 return (mcOperator &)*p;
02753 }
02754
02755 void mcElementArrayHelpers::math_PrepareForMathOperations(mcElementArray &) const
02756 {
02757
02758 }
02759
02760
02761
02762
02763
02764
02765
02766
02767
02768
02769
02770
02771 void mcElementArrayHelpers::math_PrepareForComparison(mcElementArray &) const
02772 {
02773
02774
02775
02776
02777
02778
02779
02780
02781 }
02782
02783 bool mcElementArrayHelpers::math_Compare(const mcElement &m, long flags) const
02784 {
02785 mcMATHLOG(wxT("mcElementArrayHelpers::math_Compare [%s] - comparing with [%s]"),
02786 mcTXTTHIS, mcTXT(m));
02787 int i;
02788
02789
02790 if (m.data_GetType() != data_GetType())
02791 return FALSE;
02792
02793
02794 mcElementArray m1(this);
02795 mcElementArray m2(m);
02796
02797
02798
02799
02800 if (flags & mcFIND_STRICT) {
02801 math_PrepareForComparison(m1);
02802 math_PrepareForComparison(m2);
02803 }
02804
02805
02806 int m1count = m1.data_GetCount();
02807 int m2count = m2.data_GetCount();
02808
02809
02810 if (m1count < m2count) {
02811
02812
02813 mcSWAP(mcElementArray, m1, m2);
02814 mcSWAP(int, m2count, m1count);
02815 }
02816
02817
02818
02819
02820
02821
02822
02823
02824
02825
02826
02827
02828
02829
02830
02831
02832
02833
02834
02835 bool *b2 = new bool[m2count];
02836 for (i=0; i < m2count; i++)
02837 b2[i] = FALSE;
02838
02839
02840
02841 for (i=0; i < m1count; i++) {
02842
02843 if (m1.data_isOp(i))
02844 continue;
02845
02846 mcElementType t = m1.data_Get(i).data_GetType();
02847 mcElementType opt = m1.data_GetOpTypePreceding(i);
02848 bool matched = FALSE;
02849
02850
02851 for (int c=0, max=m2.data_GetNumOfElemType(t); c < max; c++) {
02852
02853
02854 int j = m2.data_GetElemIndexOfType(c, t);
02855
02856
02857 if (b2[j] == TRUE)
02858 continue;
02859
02860
02861
02862 mcElementType opt2 = m2.data_GetOpTypePreceding(j);
02863 if (opt != opt2)
02864 continue;
02865
02866
02867 bool same = FALSE;
02868 same = m1.data_Get(i).math_Compare(m2.data_Get(j), flags);
02869
02870 if (same) {
02871
02872
02873 matched = TRUE;
02874 b2[j] = TRUE;
02875 break;
02876 }
02877 }
02878
02879
02880
02881 if (!matched)
02882 break;
02883 }
02884
02885
02886
02887
02888 delete [] b2;
02889
02890
02891
02892
02893 return (i == m1count);
02894 }
02895
02896 bool mcElementArrayHelpers::math_CompareThisOnly(const mcElement &p, long flags) const
02897 {
02898 if (!mcElementHelpers::math_CompareThisOnly(p, flags))
02899 return FALSE;
02900
02901
02902
02903 return TRUE;
02904 }
02905
02906
02907
02908
02909
02910
02911
02912
02913
02914
02915
02916
02917
02918
02919
02920
02921
02922
02923
02924
02925
02926
02927
02928
02929
02930
02931
02932
02933
02934
02935
02936
02937
02938
02939
02940
02941
02942
02943
02944
02945
02946
02947
02948
02949
02950
02951 mcMonomial mcElementArrayHelpers::math_GetFactors() const
02952 {
02953 mcMonomial res;
02954
02955 res.math_ResetToOne();
02956 for (int i=0,max=math_GetCount(); i<max; i++) {
02957 mcMonomial factor = math_Get(i).math_GetFactors();
02958
02959 mcMATHLOG(wxT("mcElementArrayHelpers::math_FactoreOut - I've factored out [%s]"), mcTXT(factor));
02960 res.math_GetGCD(factor);
02961 mcMATHLOG(wxT("mcElementArrayHelpers::math_FactoreOut - result currently is [%s]"), mcTXT(res));
02962 }
02963
02964 return res;
02965 }
02966