/usr/include/ncl/nxscxxdiscretematrix.h is in libncl-dev 2.1.18+dfsg-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | // Copyright (C) 2008 Mark Holder
//
// This file is part of NCL (Nexus Class Library) version 2.1
//
// NCL is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// NCL 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with NCL; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#if !defined(NXS_CXX_DISCRETE_MATRIX_H)
#define NXS_CXX_DISCRETE_MATRIX_H
#include <string>
#include <vector>
#include "ncl/nxsdefs.h"
#include "ncl/nxsallocatematrix.h"
#include "ncl/nxscharactersblock.h"
#include "ncl/nxscdiscretematrix.h"
class NxsCharacterPattern;
/**
* A C++ class that wraps a CDiscretMatrix in order to handle the memory
management more cleanly. This is intended to be an alternate, low-level way
to get character data out of a NxsCharactersBlock
*/
class NxsCXXDiscreteMatrix
{
public:
NxsCXXDiscreteMatrix()
{
Initialize(0L, false);
}
NxsCXXDiscreteMatrix(const NxsCDiscreteMatrix & );
NxsCXXDiscreteMatrix(const NxsCharactersBlock & cb, bool convertGapsToMissing, const NxsUnsignedSet * toInclude = 0L, bool standardizeCoding = true);
void Initialize(const NxsCharactersBlock * cb, bool convertGapsToMissing, const NxsUnsignedSet * toInclude = 0L, bool standardizeCoding = true);
const NxsCDiscreteMatrix & getConstNativeC() const
{
return nativeCMatrix;
}
NxsCDiscreteMatrix & getNativeC()
{
return nativeCMatrix;
}
unsigned getNChar() const
{
return nativeCMatrix.nChar;
}
unsigned getNTax() const
{
return nativeCMatrix.nTax;
}
unsigned getNStates() const
{
return nativeCMatrix.nStates;
}
const char * getSymbolsList() const //POL added 15-Nov-2005
{
return nativeCMatrix.symbolsList;
}
const std::vector<int8_t> &getStateList() const
{
return stateListAlias;
}
const std::vector<unsigned> &getStateListPos() const
{
return stateListPosAlias;
}
const NxsCDiscreteStateSet *getRow(unsigned i) const
{
NCL_ASSERT(i < nativeCMatrix.nTax);
return nativeCMatrix.matrix[i];
}
const std::vector<int8_t> getRowAsVector(unsigned i) const
{
NCL_ASSERT(i < nativeCMatrix.nTax);
std::vector<int8_t> v;
for (unsigned j = 0; j < nativeCMatrix.nChar; j++)
{
v.push_back(nativeCMatrix.matrix[i][j]);
}
return v;
}
const NxsCDiscreteStateSet * const * getMatrix() const
{
return nativeCMatrix.matrix;
}
const int getDatatype() const
{
return (int)nativeCMatrix.datatype;
}
bool hasWeights() const
{
return hasIntWeights() || hasDblWeights();
}
bool hasIntWeights() const
{
return !(intWts.empty());
}
bool hasDblWeights() const
{
return !(dblWts.empty());
}
std::vector<int> & getIntWeights()
{
return intWts;
}
std::vector<double> & getDblWeights()
{
return dblWts;
}
const std::vector<int> & getIntWeightsConst() const
{
return intWts;
}
const std::vector<double> & getDblWeightsConst() const
{
return dblWts;
}
const std::set<unsigned> & getExcludedCharIndices() const
{
return activeExSet;
}
std::vector<unsigned> getExcludedCharIndicesAsVector() const
{
return std::vector<unsigned>(activeExSet.begin(), activeExSet.end());
}
private:
typedef ScopedTwoDMatrix<NxsCDiscreteStateSet> ScopedStateSetTwoDMatrix;
NxsCDiscreteMatrix nativeCMatrix; /** taxa x characters matrix in a C struct*/
std::string symbolsStringAlias; /** memory management alias to symbols field of nativeCMatrix */
ScopedStateSetTwoDMatrix matrixAlias; /** memory management alias to matrix field of nativeCMatrix */
std::vector<NxsCDiscreteState_t> stateListAlias; /** memory management alias to ambigList field of nativeCMatrix */
std::vector<unsigned> stateListPosAlias; /** memory management alias to symbolsList field of nativeCMatrix */
std::vector<int> intWts;
std::vector<double> dblWts;
std::set<unsigned> activeExSet;
NxsCXXDiscreteMatrix(const NxsCXXDiscreteMatrix &); /** don't define, not copyable*/
NxsCXXDiscreteMatrix & operator=(const NxsCXXDiscreteMatrix &); /** don't define, not copyable*/
};
class NxsCharacterPattern
{
public:
bool operator < (const NxsCharacterPattern & other) const {
return this->stateCodes < other.stateCodes;
}
bool operator == (const NxsCharacterPattern & other) const {
return this->stateCodes == other.stateCodes;
}
// returns true if none of the state codes are the missing or gap codes (negative values
// note this does not test if all of the state codes correspond to completely specified
// cells that are only compatible with one state!
bool StateCodesAreNonNegative() const {
for (std::vector<NxsCDiscreteState_t>::const_iterator scIt = stateCodes.begin();
scIt != stateCodes.end();
++scIt)
{
if (*scIt < 0)
return false;
}
return true;
}
std::vector<NxsCDiscreteState_t> stateCodes;
mutable unsigned count;
mutable unsigned patternIndex; // used as scratchspace not always valid!!!
mutable double sumOfPatternWeights; // stored as float. Use NxsCXXDiscreteMatrix::hasIntWeights of the original matrix to see if these weights should be interpretted as ints
};
/*----------------------------------------------------------------------------------------------------------------------
| Fills `compressedTransposedMatrix` with the compressed patterns found in `mat`
|
| Data structure for mapping between indices in these patterns can be obtained by the client providing
| `compressedIndexPattern` arguments.
|
| Characters or taxa can be omitted by providing `taxaToInclude` or `charactersToInclude` arguments.
| If these arguments are 0L (or not provided) then all data will be included. Note that skipping taxa
| will cause the taxon indexing within a pattern to disagree with the overall taxon numbering because there will
| be "frameshifts" for all of the skipped taxa. The included taxa will be present in the expected order, but it is
| the caller code's responsibility to keep track of which taxa are included in the pattern.
*/
unsigned NxsCompressDiscreteMatrix(
const NxsCXXDiscreteMatrix & mat, /**< is the data source */
std::set<NxsCharacterPattern> & patternSet, /* matrix that will hold the compressed columns */
std::vector<const NxsCharacterPattern *> * compressedIndexPattern = 0L, /** if not 0L, this will be filled to provide a map from an index in `compressedTransposedMatrix` to the original character count */
const NxsUnsignedSet * taxaToInclude = 0L, /**< if not 0L, this should be the indices of the taxa in `mat` to include (if 0L all characters will be included). Excluding taxa will result in shorter patterns (the skipped taxa will not be filled with empty codes, instead the taxon indexing will be frameshifted -- the client code must keep track of these frameshifts). */
const NxsUnsignedSet * charactersToInclude = 0L); /**< if not 0L, this should be the indices of the characters in `mat` to include (if 0L all characters will be included) */
/*----------------------------------------------------------------------------------------------------------------------
| Fills `compressedTransposedMatrix` with the compressed patterns found in `mat`
|
| Data structure for mapping between indices in these representations can be obtained by the client providing
| `originalIndexToCompressed` and/or compressedIndexToOriginal arguments.
|
| Characters or taxa can be omitted by providing `taxaToInclude` or `charactersToInclude` arguments.
| If these arguments are 0L (or not provided) then all data will be included. Note that skipping taxa
| will cause the taxon indexing within a pattern to disagree with the overall taxon numbering because there will
| be "frameshifts" for all of the skipped taxa. The included taxa will be present in the expected order, but it is
| the caller code's responsibility to keep track of which taxa are included in the pattern.
*/
unsigned NxsCompressDiscreteMatrix(
const NxsCXXDiscreteMatrix & mat, /**< is the data source */
std::vector<NxsCharacterPattern> & compressedTransposedMatrix, /* matrix that will hold the compressed columns */
std::vector<int> * originalIndexToCompressed, /** if not 0L, this will be filled to provide map an index in `mat` to the corresponding index in `compressedTransposedMatrix` (-1 in the vector indicates that the character was not included) */
std::vector<std::set<unsigned> > * compressedIndexToOriginal, /** if not 0L, this will be filled to provide a map from an index in `compressedTransposedMatrix` to the original character count */
const NxsUnsignedSet * taxaToInclude = 0L, /**< if not 0L, this should be the indices of the taxa in `mat` to include (if 0L all characters will be included). Excluding taxa will result in shorter patterns (the skipped taxa will not be filled with empty codes, instead the taxon indexing will be frameshifted -- the client code must keep track of these frameshifts). */
const NxsUnsignedSet * charactersToInclude = 0L); /**< if not 0L, this should be the indices of the characters in `mat` to include (if 0L all characters will be included) */
void NxsConsumePatternSetToPatternVector(
std::set<NxsCharacterPattern> & patternSet, /* INPUT matrix that will hold the compressed columns */
std::vector<NxsCharacterPattern> & compressedTransposedMatrix, /* OUTPUT matrix that will hold the compressed columns */
const std::vector<const NxsCharacterPattern *> * compressedIndexPattern = 0L, /** INPUT This mapping must be provided if either `originalIndexToCompressed` or `compressedIndexToOriginal` is requested */
std::vector<int> * originalIndexToCompressed = 0L, /** OUTPUT if not 0L, this will be filled to provide map an index in `mat` to the corresponding index in `compressedTransposedMatrix` (-1 in the vector indicates that the character was not included) */
std::vector<std::set<unsigned> > * compressedIndexToOriginal = 0L); /** OUTPUT if not 0L, this will be filled to provide a map from an index in `compressedTransposedMatrix` to the original character count */
void NxsTransposeCompressedMatrix(
const std::vector<NxsCharacterPattern> & compressedTransposedMatrix,
ScopedTwoDMatrix<NxsCDiscreteStateSet> & destination,
std::vector<unsigned> * patternCounts = 0L,
std::vector<double> * patternWeights = 0L);
#endif // NXS_CXX_DISCRETE_MATRIX_H
|