Files
rdkit/Code/DataStructs/DiscreteValueVect.h
Greg Landrum 644296fe13 Add Molecular Interaction Fields (#7993)
* Add RealValueVect.

* Add UniformRealValueGrid3D

* Add Molecular Interaction Fields (MIFs)

* line endings

* cherry-pick f1bc94a4c8

* format

* Adapt tests for python3.

* Adapt RealValueVector pickling for python3.

* Speed-up of MIF calculations.

* Bugfix in MIFDescriptors.cpp.

* all tests pass

* clean up some memory leaks

* update copyrights

* rename

* rename the library

* complete the rename

* lost file

* another forgotten file

* cleanup

* clang-tidy

* clang-tidy

* windows DLL builds work

* python wrapper and tests cleanup

* convert to catch2 testing

* switch RealValueVect to use std::vector

* remove obsolete friend

* - Replace explicit loops with stdlib implicit equivalents
- Replace explicit types with auto where possible
- Avoid unnecessary copy operations where possible
- Replace raw pointers with exception-safe unique_ptr
- Replace C-style #define with constexpr
- Replace C-style casts with C++ casts
- Replace C-style arrays with std::vector
- Avoid code duplication with templated operators
- Replace VdWaals class taking multiple atom type definitions and force-field name as string parameter with force-field-specific classes deriving from an abstract VdWaals class
- Replace x,y,z doubles with Point3D class where possible
- Removed unused (and untested) DistanceToClosestAtom class
- Renamed some variables and functions for better clarity
- Converted tabs to spaces
- Made the mol parameter in cube read/write functions optional for convenience
- Made the Python wrappers more pythonic (e.g., avoid C++-style passing objects as parameters which are modified in place)
- Implemented alternative Python class constructors using boost::python::make_constructor rather than with external non-class functions
- The Python wrappers taking a sequence of Point3D now take a sequence of sequences, such that the output of Conformer.GetPositions() can be passed
- Made the Python wrapper sequence parsing more robust
- Removed duplicated code from Python wrappers

* - avoid an unnecessary copy

* progress

* works

* more cleanup

* all tests pass

* changes in response to review

---------

Co-authored-by: dfhahn <dfhahn@users.noreply.github.com>
Co-authored-by: ptosco <paolo.tosco@novartis.com>
2024-12-19 09:54:52 +01:00

147 lines
4.3 KiB
C++

//
// Copyright (C) 2004-2008 Greg Landrum and Rational Discovery LLC
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include <RDGeneral/export.h>
#ifndef __RD_DISCRETE_VALUE_VECT_20050124__
#define __RD_DISCRETE_VALUE_VECT_20050124__
#include <boost/smart_ptr.hpp>
#include <string>
#include <cstring>
#include <cstdint>
namespace RDKit {
// we require 32bit unsigneds using the std::uint32_t type:
const unsigned int BITS_PER_INT = 32;
//! a class for efficiently storing vectors of discrete values
class RDKIT_DATASTRUCTS_EXPORT DiscreteValueVect {
public:
typedef boost::shared_array<std::uint32_t> DATA_SPTR;
//! used to define the possible range of the values
typedef enum {
ONEBITVALUE = 0,
TWOBITVALUE,
FOURBITVALUE,
EIGHTBITVALUE,
SIXTEENBITVALUE,
} DiscreteValueType;
//! initialize with a particular type and size
DiscreteValueVect(DiscreteValueType valType, unsigned int length)
: d_type(valType), d_length(length) {
d_bitsPerVal = (1 << static_cast<unsigned int>(valType));
d_valsPerInt = BITS_PER_INT / d_bitsPerVal;
d_numInts = (length + d_valsPerInt - 1) / d_valsPerInt;
d_mask = ((1 << d_bitsPerVal) - 1);
auto *data = new std::uint32_t[d_numInts];
memset(static_cast<void *>(data), 0, d_numInts * sizeof(std::uint32_t));
d_data.reset(data);
}
//! Copy constructor
DiscreteValueVect(const DiscreteValueVect &other);
DiscreteValueVect &operator=(const DiscreteValueVect &other);
//! constructor from a pickle
DiscreteValueVect(const std::string &pkl) {
initFromText(pkl.c_str(), static_cast<unsigned int>(pkl.size()));
}
//! constructor from a pickle
DiscreteValueVect(const char *pkl, const unsigned int len) {
initFromText(pkl, len);
}
~DiscreteValueVect() = default;
//! return the value at an index
unsigned int getVal(unsigned int i) const;
//! support indexing using []
int operator[](unsigned int idx) const { return getVal(idx); }
//! set the value at an index
/*!
NOTE: it is an error to have val > the max value this
DiscreteValueVect can accommodate
*/
void setVal(unsigned int i, unsigned int val);
//! returns the sum of all the elements in the vect
unsigned int getTotalVal() const;
//! returns the length
unsigned int getLength() const;
//! returns the length
unsigned int size() const { return getLength(); }
//! return a pointer to our raw data storage
const std::uint32_t *getData() const;
//! return the number of bits used to store each value
unsigned int getNumBitsPerVal() const { return d_bitsPerVal; }
//! return the type of value being stored
DiscreteValueType getValueType() const { return d_type; }
//! returns the size of our storage
unsigned int getNumInts() const { return d_numInts; }
//! support dvv3 = dvv1&dvv2
/*!
operator& returns the minimum value for each element.
e.g.:
[0,1,2,0] & [0,1,1,1] -> [0,1,1,0]
*/
DiscreteValueVect operator&(const DiscreteValueVect &other) const;
//! support dvv3 = dvv1|dvv2
/*!
operator& returns the maximum value for each element.
e.g.:
[0,1,2,0] | [0,1,1,1] -> [0,1,2,1]
*/
DiscreteValueVect operator|(const DiscreteValueVect &other) const;
// DiscreteValueVect operator^ (const DiscreteValueVect &other) const;
// DiscreteValueVect operator~ () const;
DiscreteValueVect &operator+=(const DiscreteValueVect &other);
DiscreteValueVect &operator-=(const DiscreteValueVect &other);
//! returns a binary string representation (pickle)
std::string toString() const;
private:
DiscreteValueType d_type;
unsigned int d_bitsPerVal;
unsigned int d_valsPerInt;
unsigned int d_numInts;
unsigned int d_length;
unsigned int d_mask;
DATA_SPTR d_data;
void initFromText(const char *pkl, const unsigned int len);
};
RDKIT_DATASTRUCTS_EXPORT unsigned int computeL1Norm(
const DiscreteValueVect &v1, const DiscreteValueVect &v2);
RDKIT_DATASTRUCTS_EXPORT DiscreteValueVect
operator+(const DiscreteValueVect &p1, const DiscreteValueVect &p2);
RDKIT_DATASTRUCTS_EXPORT DiscreteValueVect
operator-(const DiscreteValueVect &p1, const DiscreteValueVect &p2);
} // namespace RDKit
#endif