Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / selection / sm_same.cpp
index c2a6903ceed0cd061a885b25dc6e4d3614416016..cb8126171bb215283086f6f3dc560c3458187d5d 100644 (file)
@@ -1,62 +1,65 @@
 /*
+ * 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
  * Implements the \p same selection method.
  *
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \ingroup module_selection
  */
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "gmxpre.h"
 
 #include <stdlib.h>
 
-#include "macros.h"
-#include "smalloc.h"
-#include "string2.h"
-
-#include "gromacs/selection/selmethod.h"
+#include "gromacs/legacyheaders/macros.h"
 #include "gromacs/utility/exceptions.h"
+#include "gromacs/utility/smalloc.h"
 
 #include "keywords.h"
 #include "parsetree.h"
 #include "selelem.h"
+#include "selmethod.h"
 
-/*! \internal \brief
+/*! \internal
+ * \brief
  * Data structure for the \p same selection method.
  *
  * To avoid duplicate initialization code, the same data structure is used
  * for matching both integer and string keywords; hence the unions.
+ *
+ * \ingroup module_selection
  */
 typedef struct
 {
@@ -94,29 +97,65 @@ typedef struct
     bool                     bSorted;
 } t_methoddata_same;
 
-/** Allocates data for the \p same selection method. */
+/*! \brief
+ * Allocates data for the \p same selection method.
+ *
+ * \param[in]     npar  Not used (should be 2).
+ * \param[in,out] param Method parameters (should point to a copy of
+ *      ::smparams_same_int or ::smparams_same_str).
+ * \returns Pointer to the allocated data (\ref t_methoddata_same).
+ */
 static void *
 init_data_same(int npar, gmx_ana_selparam_t *param);
-/** Initializes the \p same selection method. */
+/*! \brief
+ * Initializes the \p same selection method.
+ *
+ * \param   top   Not used.
+ * \param   npar  Not used (should be 2).
+ * \param   param Initialized method parameters (should point to a copy of
+ *      ::smparams_same_int or ::smparams_same_str).
+ * \param   data  Pointer to \ref t_methoddata_same to initialize.
+ * \returns 0 on success, -1 on failure.
+ */
 static void
 init_same(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data);
 /** Frees the data allocated for the \p same selection method. */
 static void
 free_data_same(void *data);
-/** Initializes the evaluation of the \p same selection method for a frame. */
+/*! \brief
+ * Initializes the evaluation of the \p same selection method for a frame.
+ *
+ * \param[in]  top  Not used.
+ * \param[in]  fr   Current frame.
+ * \param[in]  pbc  PBC structure.
+ * \param      data Should point to a \ref t_methoddata_same.
+ *
+ * Sorts the \c data->as.i array and removes identical values for faster and
+ * simpler lookup.
+ */
 static void
 init_frame_same_int(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data);
 /** Evaluates the \p same selection method. */
 static void
-evaluate_same_int(t_topology *top, t_trxframe *fr, t_pbc *pbc,
+evaluate_same_int(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pbc */,
                   gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
-/** Initializes the evaluation of the \p same selection method for a frame. */
+/*! \brief
+ * Initializes the evaluation of the \p same selection method for a frame.
+ *
+ * \param[in]  top  Not used.
+ * \param[in]  fr   Current frame.
+ * \param[in]  pbc  PBC structure.
+ * \param      data Should point to a \ref t_methoddata_same.
+ *
+ * Sorts the \c data->as.s array and removes identical values for faster and
+ * simpler lookup.
+ */
 static void
 init_frame_same_str(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data);
 /** Evaluates the \p same selection method. */
 static void
-evaluate_same_str(t_topology *top, t_trxframe *fr, t_pbc *pbc,
-                 gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
+evaluate_same_str(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pbc */,
+                  gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data);
 
 /** Parameters for the \p same selection method. */
 static gmx_ana_selparam_t smparams_same_int[] = {
@@ -177,20 +216,14 @@ static gmx_ana_selmethod_t sm_same_str = {
     {"same KEYWORD as ATOM_EXPR", asize(help_same), help_same},
 };
 
-/*!
- * \param[in]     npar  Not used (should be 2).
- * \param[in,out] param Method parameters (should point to a copy of
- *      ::smparams_same_int or ::smparams_same_str).
- * \returns Pointer to the allocated data (\ref t_methoddata_same).
- */
 static void *
-init_data_same(int npar, gmx_ana_selparam_t *param)
+init_data_same(int /* npar */, gmx_ana_selparam_t *param)
 {
     t_methoddata_same *data;
 
     snew(data, 1);
     data->as_s_sorted = NULL;
-    param[1].nvalptr = &data->nas;
+    param[1].nvalptr  = &data->nas;
     return data;
 }
 
@@ -204,68 +237,59 @@ init_data_same(int npar, gmx_ana_selparam_t *param)
  * immediately.
  */
 int
-_gmx_selelem_custom_init_same(gmx_ana_selmethod_t **method,
-                              t_selexpr_param *params,
-                              void *scanner)
+_gmx_selelem_custom_init_same(gmx_ana_selmethod_t                           **method,
+                              const gmx::SelectionParserParameterListPointer &params,
+                              void                                           *scanner)
 {
-    gmx_ana_selmethod_t *kwmethod;
-    t_selelem           *kwelem;
-    t_selexpr_param     *param;
-    char                *pname;
-    int                  rc;
 
     /* Do nothing if this is not a same method. */
-    if (!*method || (*method)->name != sm_same.name)
+    if (!*method || (*method)->name != sm_same.name || params->empty())
     {
         return 0;
     }
 
-    if (params->nval != 1 || !params->value->bExpr
-        || params->value->u.expr->type != SEL_EXPRESSION)
+    const gmx::SelectionParserValueList &kwvalues = params->front().values();
+    if (kwvalues.size() != 1 || !kwvalues.front().hasExpressionValue()
+        || kwvalues.front().expr->type != SEL_EXPRESSION)
     {
         _gmx_selparser_error(scanner, "'same' should be followed by a single keyword");
         return -1;
     }
-    kwmethod = params->value->u.expr->u.expr.method;
-
+    gmx_ana_selmethod_t *kwmethod = kwvalues.front().expr->u.expr.method;
     if (kwmethod->type == STR_VALUE)
     {
         *method = &sm_same_str;
     }
 
-    /* We do custom processing with the second parameter, so remove it from
-     * the params list, but save the name for later. */
-    param        = params->next;
-    params->next = NULL;
-    pname        = param->name;
-    param->name  = NULL;
-    /* Create a second keyword evaluation element for the keyword given as
-     * the first parameter, evaluating the keyword in the group given by the
-     * second parameter. */
-    rc = _gmx_sel_init_keyword_evaluator(&kwelem, kwmethod, param, scanner);
-    if (rc != 0)
+    /* We do custom processing for the "as" parameter. */
+    gmx::SelectionParserParameterList::iterator asparam = ++params->begin();
+    if (asparam != params->end() && asparam->name() == sm_same.param[1].name)
     {
-        sfree(pname);
-        return rc;
+        gmx::SelectionParserParameterList    kwparams;
+        gmx::SelectionParserValueListPointer values(
+                new gmx::SelectionParserValueList(asparam->values()));
+        kwparams.push_back(
+                gmx::SelectionParserParameter::create(NULL, move(values)));
+
+        /* Create a second keyword evaluation element for the keyword given as
+         * the first parameter, evaluating the keyword in the group given by the
+         * second parameter. */
+        gmx::SelectionTreeElementPointer kwelem
+            = _gmx_sel_init_keyword_evaluator(kwmethod, kwparams, scanner);
+        // FIXME: Use exceptions.
+        if (!kwelem)
+        {
+            return -1;
+        }
+        /* Replace the second parameter with one with a value from \p kwelem. */
+        std::string pname = asparam->name();
+        *asparam = gmx::SelectionParserParameter::createFromExpression(pname, kwelem);
     }
-    /* Replace the second parameter with one with a value from \p kwelem. */
-    param        = _gmx_selexpr_create_param(pname);
-    param->nval  = 1;
-    param->value = _gmx_selexpr_create_value_expr(kwelem);
-    params->next = param;
     return 0;
 }
 
-/*!
- * \param   top   Not used.
- * \param   npar  Not used (should be 2).
- * \param   param Initialized method parameters (should point to a copy of
- *      ::smparams_same_int or ::smparams_same_str).
- * \param   data  Pointer to \ref t_methoddata_same to initialize.
- * \returns 0 on success, -1 on failure.
- */
 static void
-init_same(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
+init_same(t_topology * /* top */, int /* npar */, gmx_ana_selparam_t *param, void *data)
 {
     t_methoddata_same *d = (t_methoddata_same *)data;
 
@@ -278,8 +302,8 @@ init_same(t_topology *top, int npar, gmx_ana_selparam_t *param, void *data)
     if (!(param[0].flags & SPAR_ATOMVAL))
     {
         GMX_THROW(gmx::InvalidInputError(
-                    "The 'same' selection keyword combined with a "
-                    "non-keyword does not make sense"));
+                          "The 'same' selection keyword combined with a "
+                          "non-keyword does not make sense"));
     }
 }
 
@@ -292,6 +316,7 @@ free_data_same(void *data)
     t_methoddata_same *d = (t_methoddata_same *)data;
 
     sfree(d->as_s_sorted);
+    sfree(d);
 }
 
 /*! \brief
@@ -311,17 +336,8 @@ cmp_int(const void *a, const void *b)
     return 0;
 }
 
-/*!
- * \param[in]  top  Not used.
- * \param[in]  fr   Current frame.
- * \param[in]  pbc  PBC structure.
- * \param      data Should point to a \ref t_methoddata_same.
- *
- * Sorts the \c data->as.i array and removes identical values for faster and
- * simpler lookup.
- */
 static void
-init_frame_same_int(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data)
+init_frame_same_int(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pbc */, void *data)
 {
     t_methoddata_same *d = (t_methoddata_same *)data;
     int                i, j;
@@ -369,20 +385,23 @@ init_frame_same_int(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data)
  * \c data->val.
  */
 static void
-evaluate_same_int(t_topology *top, t_trxframe *fr, t_pbc *pbc,
+evaluate_same_int(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pbc */,
                   gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
 {
-    t_methoddata_same *d = (t_methoddata_same *)data;
+    t_methoddata_same     *d = (t_methoddata_same *)data;
     int                    i, j;
 
     out->u.g->isize = 0;
-    i = j = 0;
+    i               = j = 0;
     while (j < g->isize)
     {
         if (d->bSorted)
         {
             /* If we are sorted, we can do a simple linear scan. */
-            while (i < d->nas && d->as.i[i] < d->val.i[j]) ++i;
+            while (i < d->nas && d->as.i[i] < d->val.i[j])
+            {
+                ++i;
+            }
         }
         else
         {
@@ -411,7 +430,10 @@ evaluate_same_int(t_topology *top, t_trxframe *fr, t_pbc *pbc,
             /* If not, skip all atoms with the same value. */
             int tmpval = d->val.i[j];
             ++j;
-            while (j < g->isize && d->val.i[j] == tmpval) ++j;
+            while (j < g->isize && d->val.i[j] == tmpval)
+            {
+                ++j;
+            }
         }
         else
         {
@@ -438,17 +460,8 @@ cmp_str(const void *a, const void *b)
     return strcmp(*(char **)a, *(char **)b);
 }
 
-/*!
- * \param[in]  top  Not used.
- * \param[in]  fr   Current frame.
- * \param[in]  pbc  PBC structure.
- * \param      data Should point to a \ref t_methoddata_same.
- *
- * Sorts the \c data->as.s array and removes identical values for faster and
- * simpler lookup.
- */
 static void
-init_frame_same_str(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data)
+init_frame_same_str(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pbc */, void *data)
 {
     t_methoddata_same *d = (t_methoddata_same *)data;
     int                i, j;
@@ -456,7 +469,7 @@ init_frame_same_str(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data)
     /* Collapse adjacent values.
      * For strings, it's unlikely that the values would be sorted originally,
      * so set bSorted always to false. */
-    d->bSorted = false;
+    d->bSorted        = false;
     d->as_s_sorted[0] = d->as.s[0];
     for (i = 1, j = 0; i < d->nas; ++i)
     {
@@ -491,14 +504,14 @@ init_frame_same_str(t_topology *top, t_trxframe *fr, t_pbc *pbc, void *data)
  * \c data->val.
  */
 static void
-evaluate_same_str(t_topology *top, t_trxframe *fr, t_pbc *pbc,
+evaluate_same_str(t_topology * /* top */, t_trxframe * /* fr */, t_pbc * /* pbc */,
                   gmx_ana_index_t *g, gmx_ana_selvalue_t *out, void *data)
 {
-    t_methoddata_same *d = (t_methoddata_same *)data;
+    t_methoddata_same     *d = (t_methoddata_same *)data;
     int                    j;
 
     out->u.g->isize = 0;
-    j = 0;
+    j               = 0;
     while (j < g->isize)
     {
         /* Do a binary search of the strings. */
@@ -511,7 +524,10 @@ evaluate_same_str(t_topology *top, t_trxframe *fr, t_pbc *pbc,
             /* If not, skip all atoms with the same value. */
             const char *tmpval = d->val.s[j];
             ++j;
-            while (j < g->isize && strcmp(d->val.s[j], tmpval) == 0) ++j;
+            while (j < g->isize && strcmp(d->val.s[j], tmpval) == 0)
+            {
+                ++j;
+            }
         }
         else
         {