Use smart pointers to manage the selection tree.
[alexxy/gromacs.git] / src / gromacs / selection / selelem.h
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11  * Copyright (c) 2001-2009, The GROMACS development team,
12  * check out http://www.gromacs.org for more information.
13
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * If you want to redistribute modifications, please consider that
20  * scientific software is very special. Version control is crucial -
21  * bugs must be traceable. We will be happy to consider code for
22  * inclusion in the official distribution, but derived work must not
23  * be called official GROMACS. Details are found in the README & COPYING
24  * files - if they are missing, get the official version at www.gromacs.org.
25  *
26  * To help us fund GROMACS development, we humbly ask that you cite
27  * the papers on the package - you can find them in the top README file.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Declares gmx::SelectionTreeElement and related things.
34  *
35  * The selection element trees constructed by the parser and the compiler
36  * are described on the respective pages:
37  * \ref page_module_selection_parser and \ref page_module_selection_compiler.
38  *
39  * This is an implementation header: there should be no need to use it outside
40  * this directory.
41  *
42  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
43  * \ingroup module_selection
44  */
45 #ifndef GMX_SELECTION_SELELEM_H
46 #define GMX_SELECTION_SELELEM_H
47
48 #include <boost/shared_ptr.hpp>
49
50 #include "gromacs/legacyheaders/types/simple.h"
51 #include "gromacs/utility/common.h"
52
53 #include "indexutil.h"
54 #include "selvalue.h"
55
56 struct gmx_ana_poscalc_t;
57 struct gmx_ana_selparam_t;
58 struct gmx_ana_selmethod_t;
59
60 struct gmx_sel_evaluate_t;
61 struct gmx_sel_mempool_t;
62
63 struct t_compiler_data;
64
65 namespace gmx
66 {
67 class SelectionTreeElement;
68
69 //! Smart pointer type for selection tree element pointers.
70 typedef boost::shared_ptr<SelectionTreeElement> SelectionTreeElementPointer;
71 } // namespace gmx
72
73 /********************************************************************/
74 /*! \name Enumerations for expression types
75  ********************************************************************/
76 //!\{
77
78 /** Defines the type of a gmx::SelectionTreeElement object. */
79 typedef enum
80 {
81     /** Constant-valued expression. */
82     SEL_CONST,
83     /** Method expression that requires evaluation. */
84     SEL_EXPRESSION,
85     /** Boolean expression. */
86     SEL_BOOLEAN,
87     /** Arithmetic expression. */
88     SEL_ARITHMETIC,
89     /** Root node of the evaluation tree. */
90     SEL_ROOT,
91     /** Subexpression that may be referenced several times. */
92     SEL_SUBEXPR,
93     /** Reference to a subexpression. */
94     SEL_SUBEXPRREF,
95     /** Unresolved reference to an external group. */
96     SEL_GROUPREF,
97     /** Post-processing of selection value. */
98     SEL_MODIFIER
99 } e_selelem_t;
100
101 /** Defines the boolean operation of gmx::SelectionTreeElement objects with type \ref SEL_BOOLEAN. */
102 typedef enum
103 {
104     BOOL_NOT,           /**< Not */
105     BOOL_AND,           /**< And */
106     BOOL_OR,            /**< Or */
107     BOOL_XOR            /**< Xor (not implemented). */
108 } e_boolean_t;
109
110 /** Defines the arithmetic operation of gmx::SelectionTreeElement objects with type \ref SEL_ARITHMETIC. */
111 typedef enum
112 {
113     ARITH_PLUS,         /**< + */
114     ARITH_MINUS,        /**< - */
115     ARITH_NEG,          /**< Unary - */
116     ARITH_MULT,         /**< * */
117     ARITH_DIV,          /**< / */
118     ARITH_EXP           /**< ^ (to power) */
119 } e_arithmetic_t;
120
121 /** Returns a string representation of the type of a gmx::SelectionTreeElement. */
122 extern const char *
123 _gmx_selelem_type_str(const gmx::SelectionTreeElement &sel);
124 /** Returns a string representation of the boolean type of a \ref SEL_BOOLEAN gmx::SelectionTreeElement. */
125 extern const char *
126 _gmx_selelem_boolean_type_str(const gmx::SelectionTreeElement &sel);
127 /** Returns a string representation of the type of a \c gmx_ana_selvalue_t. */
128 extern const char *
129 _gmx_sel_value_type_str(const gmx_ana_selvalue_t *val);
130
131 //!\}
132
133
134 /********************************************************************/
135 /*! \name Selection expression flags
136  * \anchor selelem_flags
137  ********************************************************************/
138 //!\{
139 /*! \brief
140  * Selection value flags are set.
141  *
142  * If this flag is set, the flags covered by \ref SEL_VALFLAGMASK
143  * have been set properly for the element.
144  */
145 #define SEL_FLAGSSET    1
146 /*! \brief
147  * The element evaluates to a single value.
148  *
149  * This flag is always set for \ref GROUP_VALUE elements.
150  */
151 #define SEL_SINGLEVAL   2
152 /*! \brief
153  * The element evaluates to one value for each input atom.
154  */
155 #define SEL_ATOMVAL     4
156 /*! \brief
157  * The element evaluates to an arbitrary number of values.
158  */
159 #define SEL_VARNUMVAL   8
160 /*! \brief
161  * The element (or one of its children) is dynamic.
162  */
163 #define SEL_DYNAMIC     16
164 /*! \brief
165  * Mask that covers the flags that describe the number of values.
166  */
167 #define SEL_VALTYPEMASK (SEL_SINGLEVAL | SEL_ATOMVAL | SEL_VARNUMVAL)
168 /*! \brief
169  * Mask that covers the flags that describe the value type.
170  */
171 #define SEL_VALFLAGMASK (SEL_FLAGSSET | SEL_VALTYPEMASK | SEL_DYNAMIC)
172 /*! \brief
173  * Data has been allocated for the \p v.u union.
174  *
175  * If not set, the \p v.u.ptr points to data allocated externally.
176  * This is the case if the value of the element is used as a parameter
177  * for a selection method or if the element evaluates the final value of
178  * a selection.
179  *
180  * Even if the flag is set, \p v.u.ptr can be NULL during initialization.
181  *
182  * \todo
183  * This flag overlaps with the function of \p v.nalloc field, and could
184  * probably be removed, making memory management simpler. Currently, the
185  * \p v.nalloc field is not kept up-to-date in all cases when this flag
186  * is changed and is used in places where this flag is not, so this would
187  * require a careful investigation of the selection code.
188  */
189 #define SEL_ALLOCVAL    (1<<8)
190 /*! \brief
191  * Data has been allocated for the group/position structure.
192  *
193  * If not set, the memory allocated for fields in \p v.u.g or \p v.u.p is
194  * managed externally.
195  *
196  * This field has no effect if the value type is not \ref GROUP_VALUE or
197  * \ref POS_VALUE, but should not be set.
198  */
199 #define SEL_ALLOCDATA   (1<<9)
200 /*! \brief
201  * \p method->init_frame should be called for the frame.
202  */
203 #define SEL_INITFRAME   (1<<10)
204 /*! \brief
205  * Parameter has been evaluated for the current frame.
206  *
207  * This flag is set for children of \ref SEL_EXPRESSION elements (which
208  * describe method parameters) after the element has been evaluated for the
209  * current frame.
210  * It is not set for \ref SEL_ATOMVAL elements, because they may need to
211  * be evaluated multiple times.
212  */
213 #define SEL_EVALFRAME   (1<<11)
214 /*! \brief
215  * \p method->init has been called.
216  */
217 #define SEL_METHODINIT  (1<<12)
218 /*! \brief
219  * \p method->outinit has been called.
220  *
221  * This flag is also used for \ref SEL_SUBEXPRREF elements.
222  */
223 #define SEL_OUTINIT     (1<<13)
224 //!\}
225
226
227 namespace gmx
228 {
229
230 /*! \brief
231  * Function pointer for evaluating a gmx::SelectionTreeElement.
232  */
233 typedef void (*sel_evalfunc)(struct gmx_sel_evaluate_t *data,
234                              const SelectionTreeElementPointer &sel,
235                              gmx_ana_index_t *g);
236
237 /*! \internal \brief
238  * Represents an element of a selection expression.
239  */
240 class SelectionTreeElement
241 {
242     public:
243         /*! \brief
244          * Allocates memory and performs common initialization.
245          *
246          * \param[in] type Type of selection element to create.
247          *
248          * \a type is set to \p type,
249          * \a v::type is set to \ref GROUP_VALUE for boolean and comparison
250          * expressions and \ref NO_VALUE for others, and
251          * \ref SEL_ALLOCVAL is set for non-root elements (\ref SEL_ALLOCDATA
252          * is also set for \ref SEL_BOOLEAN elements).
253          * All the pointers are set to NULL.
254          */
255         explicit SelectionTreeElement(e_selelem_t type);
256         ~SelectionTreeElement();
257
258         //! Frees the memory allocated for the \a v union.
259         void freeValues();
260         //! Frees the memory allocated for the \a u union.
261         void freeExpressionData();
262         /* In compiler.cpp */
263         /*! \brief
264          * Frees the memory allocated for the selection compiler.
265          *
266          * This function only frees the data for the given selection, not its
267          * children.  It is safe to call the function when compiler data has
268          * not been allocated or has already been freed; in such a case,
269          * nothing is done.
270          */
271         void freeCompilerData();
272
273         /*! \brief
274          * Reserves memory for value from a memory pool.
275          *
276          * \param[in]     count Number of values to reserve memory for.
277          *
278          * Reserves memory for the values of this element from the \a mempool
279          * memory pool.
280          * If no memory pool is set, nothing is done.
281          */
282         void mempoolReserve(int count);
283         /*! \brief
284          * Releases memory pool used for value.
285          *
286          * Releases the memory allocated for the values of this element from the
287          * \a mempool memory pool.
288          * If no memory pool is set, nothing is done.
289          */
290         void mempoolRelease();
291
292         /*! \brief Name of the element.
293          *
294          * This field is only used for informative purposes.
295          * It is always either NULL or a pointer to a string.
296          * Memory is never allocated for it directly.
297          */
298         const char                         *name;
299         //! Type of the element.
300         e_selelem_t                         type;
301         /*! \brief
302          * Value storage of the element.
303          *
304          * This field contains the evaluated value of the element, as well as
305          * the output value type.
306          */
307         gmx_ana_selvalue_t                  v;
308         /*! \brief
309          * Evaluation function for the element.
310          *
311          * Can be either NULL (if the expression is a constant and does not
312          * require evaluation) or point to one of the functions defined in
313          * evaluate.h.
314          */
315         sel_evalfunc                        evaluate;
316         /*! \brief
317          * Information flags about the element.
318          *
319          * Allowed flags are listed here:
320          * \ref selelem_flags "flags for gmx::SelectionTreeElement".
321          */
322         int                                 flags;
323         //! Data required by the evaluation function.
324         union {
325             /*! \brief Index group data for several element types.
326              *
327              *  - \ref SEL_CONST : if the value type is \ref GROUP_VALUE,
328              *    this field holds the unprocessed group value.
329              *  - \ref SEL_ROOT : holds the group value for which the
330              *    selection subtree should be evaluated.
331              *  - \ref SEL_SUBEXPR : holds the group for which the subexpression
332              *    has been evaluated.
333              */
334             gmx_ana_index_t                 cgrp;
335             //! Data for \ref SEL_EXPRESSION and \ref SEL_MODIFIER elements.
336             struct {
337                 //! Pointer the the method used in this expression.
338                 struct gmx_ana_selmethod_t *method;
339                 //! Pointer to the data allocated by the method's \p init_data (see sel_datafunc()).
340                 void                       *mdata;
341                 //! Pointer to the position data passed to the method.
342                 struct gmx_ana_pos_t       *pos;
343                 //! Pointer to the evaluation data for \p pos.
344                 struct gmx_ana_poscalc_t   *pc;
345             }                               expr;
346             //! Operation type for \ref SEL_BOOLEAN elements.
347             e_boolean_t                     boolt;
348             //! Operation type for \ref SEL_ARITHMETIC elements.
349             struct {
350                 //! Operation type.
351                 e_arithmetic_t              type;
352                 //! String representation.
353                 char                       *opstr;
354             }                               arith;
355             //! Associated selection parameter for \ref SEL_SUBEXPRREF elements.
356             struct gmx_ana_selparam_t      *param;
357             //! The string/number used to reference the group.
358             struct {
359                 //! Name of the referenced external group.
360                 char                       *name;
361                 //! If \a name is NULL, the index number of the referenced group.
362                 int                         id;
363             }                               gref;
364         }                                   u;
365         //! Memory pool to use for values, or NULL if standard memory handling.
366         struct gmx_sel_mempool_t           *mempool;
367         //! Internal data for the selection compiler.
368         t_compiler_data                    *cdata;
369
370         /*! \brief The first child element.
371          *
372          * Other children can be accessed through the \p next field of \p child.
373          */
374         SelectionTreeElementPointer         child;
375         //! The next sibling element.
376         SelectionTreeElementPointer         next;
377
378     private:
379         GMX_DISALLOW_COPY_AND_ASSIGN(SelectionTreeElement);
380 };
381
382 } // namespace gmx
383
384 /********************************************************************/
385 /*! \name Selection expression functions
386  */
387 //!\{
388
389 /* In evaluate.c */
390 /** Writes out a human-readable name for an evaluation function. */
391 void
392 _gmx_sel_print_evalfunc_name(FILE *fp, gmx::sel_evalfunc evalfunc);
393
394 /** Sets the value type of a gmx::SelectionTreeElement. */
395 void
396 _gmx_selelem_set_vtype(const gmx::SelectionTreeElementPointer &sel,
397                        e_selvalue_t vtype);
398
399 /** Frees the memory allocated for a selection method. */
400 void
401 _gmx_selelem_free_method(struct gmx_ana_selmethod_t *method, void *mdata);
402
403 /** Prints a human-readable version of a selection element subtree. */
404 void
405 _gmx_selelem_print_tree(FILE *fp, const gmx::SelectionTreeElement &sel,
406                         bool bValues, int level);
407 /* In compiler.c */
408 /** Prints a human-readable version of the internal compiler data structure. */
409 void
410 _gmx_selelem_print_compiler_info(FILE *fp, const gmx::SelectionTreeElement &sel,
411                                  int level);
412
413 /** Returns true if the selection element subtree requires topology information for evaluation. */
414 bool
415 _gmx_selelem_requires_top(const gmx::SelectionTreeElement &root);
416
417 /* In sm_insolidangle.c */
418 /** Returns true if the covered fraction of the selection can be calculated. */
419 bool
420 _gmx_selelem_can_estimate_cover(const gmx::SelectionTreeElement &sel);
421 /** Returns the covered fraction of the selection for the current frame. */
422 real
423 _gmx_selelem_estimate_coverfrac(const gmx::SelectionTreeElement &sel);
424
425 //!\}
426
427 #endif