/*
+ * This file is part of the GROMACS molecular simulation package.
*
- * This source code is part of
+ * Copyright (c) 2009,2010,2011,2012,2013,2014, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
*
- * G R O M A C S
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
*
- * GROningen MAchine for Chemical Simulations
+ * GROMACS 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 for more details.
*
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2009, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program 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.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
- * If you want to redistribute modifications, please consider that
- * scientific software is very special. Version control is crucial -
- * bugs must be traceable. We will be happy to consider code for
- * inclusion in the official distribution, but derived work must not
- * be called official GROMACS. Details are found in the README & COPYING
- * files - if they are missing, get the official version at www.gromacs.org.
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
*
* To help us fund GROMACS development, we humbly ask that you cite
- * the papers on the package - you can find them in the top README file.
- *
- * For more info, check our website at http://www.gromacs.org
+ * the research papers on the package. Check out http://www.gromacs.org.
*/
/*! \internal \file
* \brief
- * Declares ::t_selelem and related things.
+ * Declares gmx::SelectionTreeElement and related things.
*
* The selection element trees constructed by the parser and the compiler
* are described on the respective pages:
* This is an implementation header: there should be no need to use it outside
* this directory.
*
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
* \ingroup module_selection
*/
-#ifndef SELECTION_ELEMENT_H
-#define SELECTION_ELEMENT_H
+#ifndef GMX_SELECTION_SELELEM_H
+#define GMX_SELECTION_SELELEM_H
+
+#include <string>
-#include <types/simple.h>
+#include <boost/shared_ptr.hpp>
#include "gromacs/selection/indexutil.h"
-#include "gromacs/selection/selvalue.h"
+#include "gromacs/utility/common.h"
+#include "gromacs/utility/real.h"
+
+#include "selvalue.h"
struct gmx_ana_poscalc_t;
struct gmx_ana_selparam_t;
struct gmx_sel_evaluate_t;
struct gmx_sel_mempool_t;
-struct t_selelem;
+
+struct t_compiler_data;
+
+namespace gmx
+{
+class SelectionTreeElement;
+
+//! Smart pointer type for selection tree element pointers.
+typedef boost::shared_ptr<SelectionTreeElement> SelectionTreeElementPointer;
+} // namespace gmx
/********************************************************************/
/*! \name Enumerations for expression types
********************************************************************/
-/*@{*/
+//!\{
-/** Defines the type of a \c t_selelem object. */
+/** Defines the type of a gmx::SelectionTreeElement object. */
typedef enum
{
/** Constant-valued expression. */
SEL_MODIFIER
} e_selelem_t;
-/** Defines the boolean operation of \c t_selelem objects with type \ref SEL_BOOLEAN. */
+/** Defines the boolean operation of gmx::SelectionTreeElement objects with type \ref SEL_BOOLEAN. */
typedef enum
{
BOOL_NOT, /**< Not */
BOOL_XOR /**< Xor (not implemented). */
} e_boolean_t;
-/** Defines the arithmetic operation of \c t_selelem objects with type \ref SEL_ARITHMETIC. */
+/** Defines the arithmetic operation of gmx::SelectionTreeElement objects with type \ref SEL_ARITHMETIC. */
typedef enum
{
- ARITH_PLUS, /**< + */
- ARITH_MINUS, /**< - */
- ARITH_NEG, /**< Unary - */
- ARITH_MULT, /**< * */
- ARITH_DIV, /**< / */
- ARITH_EXP /**< ^ (to power) */
+ ARITH_PLUS, /**< Addition (`+`) */
+ ARITH_MINUS, /**< Subtraction (`-`) */
+ ARITH_NEG, /**< Unary `-` */
+ ARITH_MULT, /**< Multiplication (`*`) */
+ ARITH_DIV, /**< Division (`/`) */
+ ARITH_EXP /**< Power (`^`) */
} e_arithmetic_t;
-/** Returns a string representation of the type of a \c t_selelem. */
+/** Returns a string representation of the type of a gmx::SelectionTreeElement. */
extern const char *
-_gmx_selelem_type_str(struct t_selelem *sel);
-/** Returns a string representation of the boolean type of a \ref SEL_BOOLEAN \c t_selelem. */
+_gmx_selelem_type_str(const gmx::SelectionTreeElement &sel);
+/** Returns a string representation of the boolean type of a \ref SEL_BOOLEAN gmx::SelectionTreeElement. */
extern const char *
-_gmx_selelem_boolean_type_str(struct t_selelem *sel);
+_gmx_selelem_boolean_type_str(const gmx::SelectionTreeElement &sel);
/** Returns a string representation of the type of a \c gmx_ana_selvalue_t. */
extern const char *
-_gmx_sel_value_type_str(gmx_ana_selvalue_t *val);
+_gmx_sel_value_type_str(const gmx_ana_selvalue_t *val);
-/*@}*/
+//!\}
/********************************************************************/
/*! \name Selection expression flags
* \anchor selelem_flags
********************************************************************/
-/*@{*/
+//!\{
/*! \brief
* Selection value flags are set.
*
* The element (or one of its children) is dynamic.
*/
#define SEL_DYNAMIC 16
+/*! \brief
+ * The element may contain atom indices in an unsorted order.
+ */
+#define SEL_UNSORTED 32
/*! \brief
* Mask that covers the flags that describe the number of values.
*/
* This flag is also used for \ref SEL_SUBEXPRREF elements.
*/
#define SEL_OUTINIT (1<<13)
-/*@}*/
+//!\}
-/********************************************************************/
-/*! \name Selection expression data structures and functions
- ********************************************************************/
-/*@{*/
+namespace gmx
+{
-struct t_selelem;
+class ExceptionInitializer;
/*! \brief
- * Function pointer for evaluating a \c t_selelem.
+ * Function pointer for evaluating a gmx::SelectionTreeElement.
*/
-typedef void (*sel_evalfunc)(struct gmx_sel_evaluate_t *data,
- struct t_selelem *sel, gmx_ana_index_t *g);
+typedef void (*sel_evalfunc)(struct gmx_sel_evaluate_t *data,
+ const SelectionTreeElementPointer &sel,
+ gmx_ana_index_t *g);
/*! \internal \brief
* Represents an element of a selection expression.
*/
-typedef struct t_selelem
+class SelectionTreeElement
{
- /*! \brief Name of the element.
- *
- * This field is only used for informative purposes.
- * It is always either NULL or a pointer to a string.
- * Memory is never allocated for it directly.
- */
- const char *name;
- /** Type of the element. */
- e_selelem_t type;
- /*! \brief
- * Value storage of the element.
- *
- * This field contains the evaluated value of the element, as well as
- * the output value type.
- */
- gmx_ana_selvalue_t v;
- /*! \brief
- * Evaluation function for the element.
- *
- * Can be either NULL (if the expression is a constant and does not require
- * evaluation) or point to one of the functions defined in evaluate.h.
- */
- sel_evalfunc evaluate;
- /*! \brief
- * Information flags about the element.
- *
- * Allowed flags are listed here:
- * \ref selelem_flags "flags for \c t_selelem".
- */
- int flags;
- /** Data required by the evaluation function. */
- union {
- /*! \brief Index group data for several element types.
+ public:
+ /*! \brief
+ * Allocates memory and performs common initialization.
+ *
+ * \param[in] type Type of selection element to create.
+ *
+ * \a type is set to \p type,
+ * \a v::type is set to \ref GROUP_VALUE for boolean and comparison
+ * expressions and \ref NO_VALUE for others, and
+ * \ref SEL_ALLOCVAL is set for non-root elements (\ref SEL_ALLOCDATA
+ * is also set for \ref SEL_BOOLEAN elements).
+ * All the pointers are set to NULL.
+ */
+ explicit SelectionTreeElement(e_selelem_t type);
+ ~SelectionTreeElement();
+
+ //! Frees the memory allocated for the \a v union.
+ void freeValues();
+ //! Frees the memory allocated for the \a u union.
+ void freeExpressionData();
+ /* In compiler.cpp */
+ /*! \brief
+ * Frees the memory allocated for the selection compiler.
+ *
+ * This function only frees the data for the given selection, not its
+ * children. It is safe to call the function when compiler data has
+ * not been allocated or has already been freed; in such a case,
+ * nothing is done.
+ */
+ void freeCompilerData();
+
+ /*! \brief
+ * Reserves memory for value from a memory pool.
+ *
+ * \param[in] count Number of values to reserve memory for.
+ *
+ * Reserves memory for the values of this element from the \a mempool
+ * memory pool.
+ * If no memory pool is set, nothing is done.
+ */
+ void mempoolReserve(int count);
+ /*! \brief
+ * Releases memory pool used for value.
+ *
+ * Releases the memory allocated for the values of this element from the
+ * \a mempool memory pool.
+ * If no memory pool is set, nothing is done.
+ */
+ void mempoolRelease();
+
+ //! Returns the name of the element.
+ const std::string &name() const { return name_; }
+ /*! \brief
+ * Sets the name of the element.
+ *
+ * \param[in] name Name to set (can be NULL).
+ * \throws std::bad_alloc if out of memory.
+ */
+ void setName(const char *name) { name_ = (name != NULL ? name : ""); }
+ //! \copydoc setName(const char *)
+ void setName(const std::string &name) { name_ = name; }
+ /*! \brief
+ * Sets the name of a root element if it is missing.
+ *
+ * \param[in] selectionText Full selection text to use as a fallback.
+ * \throws std::bad_alloc if out of memory.
+ *
+ * If index groups have not yet been set and the selection is a result
+ * of a group reference, the name may still be empty after this call.
*
- * - \ref SEL_CONST : if the value type is \ref GROUP_VALUE,
- * this field holds the unprocessed group value.
- * - \ref SEL_ROOT : holds the group value for which the
- * selection subtree should be evaluated.
- * - \ref SEL_SUBEXPR : holds the group for which the subexpression
- * has been evaluated.
+ * Strong exception safety guarantee.
*/
- gmx_ana_index_t cgrp;
- /** Data for \ref SEL_EXPRESSION and \ref SEL_MODIFIER elements. */
- struct {
- /** Pointer the the method used in this expression. */
- struct gmx_ana_selmethod_t *method;
- /** Pointer to the data allocated by the method's \p init_data (see sel_datafunc()). */
- void *mdata;
- /** Pointer to the position data passed to the method. */
- struct gmx_ana_pos_t *pos;
- /** Pointer to the evaluation data for \p pos. */
- struct gmx_ana_poscalc_t *pc;
- } expr;
- /** Operation type for \ref SEL_BOOLEAN elements. */
- e_boolean_t boolt;
- /** Operation type for \ref SEL_ARITHMETIC elements. */
- struct {
- /** Operation type. */
- e_arithmetic_t type;
- /** String representation. */
- char *opstr;
- } arith;
- /** Associated selection parameter for \ref SEL_SUBEXPRREF elements. */
- struct gmx_ana_selparam_t *param;
- /** The string/number used to reference the group. */
- struct {
- /** Name of the referenced external group. */
- char *name;
- /** If \a name is NULL, the index number of the referenced group. */
- int id;
- } gref;
- } u;
- /** Memory pool to use for values, or NULL if standard memory handling. */
- struct gmx_sel_mempool_t *mempool;
- /** Internal data for the selection compiler. */
- struct t_compiler_data *cdata;
-
- /*! \brief The first child element.
- *
- * Other children can be accessed through the \p next field of \p child.
- */
- struct t_selelem *child;
- /** The next sibling element. */
- struct t_selelem *next;
- /*! \brief Number of references to this element.
- *
- * Should be larger than one only for \ref SEL_SUBEXPR elements.
- */
- int refcount;
-} t_selelem;
+ void fillNameIfMissing(const char *selectionText);
+
+ /*! \brief
+ * Checks that this element and its children do not contain unsupported
+ * elements with unsorted atoms.
+ *
+ * \param[in] bUnsortedAllowed Whether this element's parents allow it
+ * to have unsorted atoms.
+ * \param errors Object for reporting any error messages.
+ * \throws std::bad_alloc if out of memory.
+ *
+ * Errors are reported as nested exceptions in \p errors.
+ */
+ void checkUnsortedAtoms(bool bUnsortedAllowed,
+ ExceptionInitializer *errors) const;
+ /*! \brief
+ * Resolves an unresolved reference to an index group.
+ *
+ * \param[in] grps Index groups to use to resolve the reference.
+ * \param[in] natoms Maximum number of atoms the selections can evaluate to
+ * (zero if the topology/atom count is not set yet).
+ * \throws std::bad_alloc if out of memory.
+ * \throws InconsistentInputError if the reference cannot be
+ * resolved.
+ */
+ void resolveIndexGroupReference(gmx_ana_indexgrps_t *grps, int natoms);
+ /*! \brief
+ * Checks that an index group has valid atom indices.
+ *
+ * \param[in] natoms Maximum number of atoms the selections can evaluate to.
+ * \throws std::bad_alloc if out of memory.
+ * \throws InconsistentInputError if there are invalid atom indices.
+ */
+ void checkIndexGroup(int natoms);
+
+ //! Type of the element.
+ e_selelem_t type;
+ /*! \brief
+ * Value storage of the element.
+ *
+ * This field contains the evaluated value of the element, as well as
+ * the output value type.
+ */
+ gmx_ana_selvalue_t v;
+ /*! \brief
+ * Evaluation function for the element.
+ *
+ * Can be either NULL (if the expression is a constant and does not
+ * require evaluation) or point to one of the functions defined in
+ * evaluate.h.
+ */
+ sel_evalfunc evaluate;
+ /*! \brief
+ * Information flags about the element.
+ *
+ * Allowed flags are listed here:
+ * \ref selelem_flags "flags for gmx::SelectionTreeElement".
+ */
+ int flags;
+ //! Data required by the evaluation function.
+ union {
+ /*! \brief Index group data for several element types.
+ *
+ * - \ref SEL_CONST : if the value type is \ref GROUP_VALUE,
+ * this field holds the unprocessed group value.
+ * - \ref SEL_ROOT : holds the group value for which the
+ * selection subtree should be evaluated.
+ * - \ref SEL_SUBEXPR : holds the group for which the subexpression
+ * has been evaluated.
+ */
+ gmx_ana_index_t cgrp;
+ //! Data for \ref SEL_EXPRESSION and \ref SEL_MODIFIER elements.
+ struct {
+ //! Pointer the the method used in this expression.
+ struct gmx_ana_selmethod_t *method;
+ //! Pointer to the data allocated by the method's \p init_data (see sel_datafunc()).
+ void *mdata;
+ //! Pointer to the position data passed to the method.
+ struct gmx_ana_pos_t *pos;
+ //! Pointer to the evaluation data for \p pos.
+ struct gmx_ana_poscalc_t *pc;
+ } expr;
+ //! Operation type for \ref SEL_BOOLEAN elements.
+ e_boolean_t boolt;
+ //! Operation type for \ref SEL_ARITHMETIC elements.
+ struct {
+ //! Operation type.
+ e_arithmetic_t type;
+ //! String representation.
+ char *opstr;
+ } arith;
+ //! Associated selection parameter for \ref SEL_SUBEXPRREF elements.
+ struct gmx_ana_selparam_t *param;
+ //! The string/number used to reference the group.
+ struct {
+ //! Name of the referenced external group.
+ char *name;
+ //! If \a name is NULL, the index number of the referenced group.
+ int id;
+ } gref;
+ } u;
+ //! Memory pool to use for values, or NULL if standard memory handling.
+ struct gmx_sel_mempool_t *mempool;
+ //! Internal data for the selection compiler.
+ t_compiler_data *cdata;
+
+ /*! \brief The first child element.
+ *
+ * Other children can be accessed through the \p next field of \p child.
+ */
+ SelectionTreeElementPointer child;
+ //! The next sibling element.
+ SelectionTreeElementPointer next;
+
+ private:
+ /*! \brief
+ * Name of the element.
+ *
+ * This field is only used for informative purposes.
+ */
+ std::string name_;
+
+ GMX_DISALLOW_COPY_AND_ASSIGN(SelectionTreeElement);
+};
+
+} // namespace gmx
+
+/********************************************************************/
+/*! \name Selection expression functions
+ */
+//!\{
/* In evaluate.c */
/** Writes out a human-readable name for an evaluation function. */
-extern void
-_gmx_sel_print_evalfunc_name(FILE *fp, sel_evalfunc evalfunc);
-
-/** Allocates memory and performs some common initialization for a \c t_selelem. */
-extern t_selelem *
-_gmx_selelem_create(e_selelem_t type);
-/** Sets the value type of a \c t_selelem. */
-extern int
-_gmx_selelem_set_vtype(t_selelem *sel, e_selvalue_t vtype);
-/** Reserves memory for value of a \c t_selelem from a memory pool. */
-extern void
-_gmx_selelem_mempool_reserve(t_selelem *sel, int count);
-/** Releases memory pool used for value of a \c t_selelem. */
-extern void
-_gmx_selelem_mempool_release(t_selelem *sel);
-/** Frees the memory allocated for a \c t_selelem structure and all its children. */
-extern void
-_gmx_selelem_free(t_selelem *sel);
-/** Frees the memory allocated for a \c t_selelem structure, all its children, and also all structures referenced through t_selelem::next fields. */
-extern void
-_gmx_selelem_free_chain(t_selelem *first);
-
-/** Frees the memory allocated for the \c t_selelem::d union. */
-extern void
-_gmx_selelem_free_values(t_selelem *sel);
+void
+_gmx_sel_print_evalfunc_name(FILE *fp, gmx::sel_evalfunc evalfunc);
+
+/** Sets the value type of a gmx::SelectionTreeElement. */
+void
+_gmx_selelem_set_vtype(const gmx::SelectionTreeElementPointer &sel,
+ e_selvalue_t vtype);
+
/** Frees the memory allocated for a selection method. */
-extern void
+void
_gmx_selelem_free_method(struct gmx_ana_selmethod_t *method, void *mdata);
-/** Frees the memory allocated for the \c t_selelem::u field. */
-extern void
-_gmx_selelem_free_exprdata(t_selelem *sel);
-/* In compiler.c */
-/** Frees the memory allocated for the selection compiler. */
-extern void
-_gmx_selelem_free_compiler_data(t_selelem *sel);
/** Prints a human-readable version of a selection element subtree. */
-extern void
-_gmx_selelem_print_tree(FILE *fp, t_selelem *root, bool bValues, int level);
-/* In compile.c */
+void
+_gmx_selelem_print_tree(FILE *fp, const gmx::SelectionTreeElement &sel,
+ bool bValues, int level);
+/* In compiler.c */
/** Prints a human-readable version of the internal compiler data structure. */
-extern void
-_gmx_selelem_print_compiler_info(FILE *fp, t_selelem *sel, int level);
+void
+_gmx_selelem_print_compiler_info(FILE *fp, const gmx::SelectionTreeElement &sel,
+ int level);
/** Returns true if the selection element subtree requires topology information for evaluation. */
-extern bool
-_gmx_selelem_requires_top(t_selelem *root);
+bool
+_gmx_selelem_requires_top(const gmx::SelectionTreeElement &root);
/* In sm_insolidangle.c */
/** Returns true if the covered fraction of the selection can be calculated. */
-extern bool
-_gmx_selelem_can_estimate_cover(t_selelem *sel);
+bool
+_gmx_selelem_can_estimate_cover(const gmx::SelectionTreeElement &sel);
/** Returns the covered fraction of the selection for the current frame. */
-extern real
-_gmx_selelem_estimate_coverfrac(t_selelem *sel);
+real
+_gmx_selelem_estimate_coverfrac(const gmx::SelectionTreeElement &sel);
-/*@}*/
+//!\}
#endif