1 /* 2 3 HyPhy - Hypothesis Testing Using Phylogenies. 4 5 Copyright (C) 1997-now 6 Core Developers: 7 Sergei L Kosakovsky Pond (spond@ucsd.edu) 8 Art FY Poon (apoon42@uwo.ca) 9 Steven Weaver (sweaver@ucsd.edu) 10 11 Module Developers: 12 Lance Hepler (nlhepler@gmail.com) 13 Martin Smith (martin.audacis@gmail.com) 14 15 Significant contributions from: 16 Spencer V Muse (muse@stat.ncsu.edu) 17 Simon DW Frost (sdf22@cam.ac.uk) 18 19 Permission is hereby granted, free of charge, to any person obtaining a 20 copy of this software and associated documentation files (the 21 "Software"), to deal in the Software without restriction, including 22 without limitation the rights to use, copy, modify, merge, publish, 23 distribute, sublicense, and/or sell copies of the Software, and to 24 permit persons to whom the Software is furnished to do so, subject to 25 the following conditions: 26 27 The above copyright notice and this permission notice shall be included 28 in all copies or substantial portions of the Software. 29 30 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 31 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 32 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 33 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 34 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 35 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 36 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 37 38 */ 39 40 #ifndef __VARIABLECONTAINER__ 41 #define __VARIABLECONTAINER__ 42 43 #include "variable.h" 44 #include "baseobj.h" 45 #include "list.h" 46 #include "avllistx.h" 47 #include "avllistxl.h" 48 #include "operation.h" 49 50 //__________________________________________________________________________________ 51 52 // this class defines a computational (or storage) class which, as a variable, may contain 53 // other variables locally. 54 55 56 class _Matrix; 57 class _VariableContainer: public _Variable { 58 59 friend class _Operation; 60 friend class _Variable; 61 62 public: 63 64 _VariableContainer (void); 65 _VariableContainer (_String const & theName, _String theTmplt = "", _VariableContainer* theP = nil); 66 // name, matrix constructor, the parent (if there is one) 67 virtual ~_VariableContainer(void); 68 69 void InitializeVarCont (_String const&, _String&, _VariableContainer*, _AVLListXL* = nil); 70 void ScanModelBasedVariables (_String const&, _AVLListXL*); 71 virtual void SetModel (long,_AVLListXL*); 72 73 virtual void MarkDone (void); 74 75 // variable access/operation functions 76 IsContainer(void)77 virtual bool IsContainer (void) { 78 return true; 79 } 80 81 virtual bool HasChanged (bool = false); 82 virtual bool NeedToExponentiate (bool = false) const; 83 84 void ScanAndAttachVariables (void); 85 86 void ScanContainerForVariables (_AVLList&,_AVLList&, _AVLListX* tagger = nil, long weight = 0, _AVLListX * map_variables_to_nodes = nil, long track_node = 0); 87 virtual void ScanForDVariables (_AVLList&,_AVLList&) const; 88 virtual void ScanForGVariables (_AVLList&,_AVLList&, _AVLListX* tagger = nil, long weight = 0) const; 89 90 virtual bool IsConstant (void); 91 virtual BaseRef makeDynamic (void) const; 92 virtual void Duplicate (BaseRefConst); 93 94 virtual BaseRef toStr (unsigned long); 95 96 bool HasLocals (void); 97 98 virtual bool RemoveDependance (long); 99 virtual void RemoveDependance (_AVLList const&); 100 virtual long SetDependance (long); 101 bool SetMDependance (_SimpleList const&); 102 103 virtual void Clear (void); 104 virtual void ClearConstraints (void); 105 106 long CountIndependents (void) const; 107 long CountDependents (void) const; 108 long CountAll (void) const; 109 110 virtual _Variable* GetIthIndependent (long) const; 111 virtual _Variable* GetIthDependent (long) const; 112 virtual _Variable* GetIthParameter (long) const; 113 114 long CheckAndAddUserExpression (_String&, long startWith = 0); 115 void KillUserExpression (long); 116 virtual void CompileListOfDependents (_SimpleList&); 117 118 void MatchParametersToList (_List&, bool doAll = false, bool indOnly = false); 119 _Matrix* GetModelMatrix (_List* = nil, _SimpleList* = nil) const; 120 _Matrix* GetFreqMatrix (void) const; 121 bool HasExplicitFormModel (void) const; 122 _Formula* GetExplicitFormModel (void) const; 123 GetModelIndex(void)124 long GetModelIndex (void) const { 125 return theModel; 126 } 127 128 _String const* GetModelName (void) const; 129 130 long GetModelDimension (void); 131 /* 20100316 SLKP 132 return the dimension of the model; needed to handle the case 133 of explicit model exponentials 134 */ 135 136 void CopyMatrixParameters (_VariableContainer*, bool match_by_name = false); 137 void GetListOfModelParameters (_List&); 138 _String* GetSaveableListOfUserParameters (void); 139 void TrimMemory (void); GetTheParent(void)140 _VariableContainer* GetTheParent (void) { 141 return theParent; 142 } 143 144 protected: // data members 145 146 _SimpleList *iVariables, 147 *dVariables, 148 *gVariables; 149 150 void SortVars (void); 151 152 void PushGlobalVariable (long var_ref); 153 void PushIndVariable (long var_ref, long local_ref); 154 void PushDepVariable (long var_ref, long local_ref); 155 bool HasIndVariable (long var_ref) const; 156 bool HasDepVariable (long var_ref) const; 157 void RemoveLocalVariable (_SimpleList* & array, long array_index); 158 void RemoveGlobalVariable (long array_index); 159 long InsertVariableInSortedList (_SimpleList * & list, _String const& var_name, long var_idx, long ref_idx); 160 161 static void CopyModelParameterValue (long var_idx, long ref_index, unsigned long); 162 ForEachLocalVariable(_SimpleList const * array,LAMBDA && cb)163 template <typename LAMBDA> void ForEachLocalVariable (_SimpleList const * array, LAMBDA && cb) const { 164 if (array) { 165 unsigned long array_l = array->countitems(); 166 for (unsigned long index = 0UL; index < array_l; index += 2UL) { 167 cb (array->get (index), array->get (index+1), index); 168 } 169 } 170 }; 171 FindLocalVariable(_SimpleList const * array,LAMBDA && cb)172 template <typename LAMBDA> long FindLocalVariable (_SimpleList const * array, LAMBDA && cb) const { 173 if (array) { 174 unsigned long array_l = array->countitems(); 175 for (long index = 0UL; index < array_l; index += 2UL) { 176 if (cb (array->get (index), array->get (index+1), index)) { 177 return index >> 1; 178 } 179 } 180 } 181 return kNotFound; 182 }; 183 AnyLocalVariable(_SimpleList const * array,LAMBDA && cb)184 template <typename LAMBDA> bool AnyLocalVariable (_SimpleList const * array, LAMBDA && cb) const { 185 if (array) { 186 unsigned long array_l = array->countitems(); 187 for (unsigned long index = 0UL; index < array_l; index += 2UL) { 188 if ((cb) (array->get (index), array->get (index+1), index)) { 189 return true; 190 } 191 } 192 } 193 return false; 194 }; 195 196 197 198 long theModel; // model template for the container 199 _VariableContainer *theParent; // a higher level container, if there is one. 200 201 }; 202 203 #endif 204