Merge remote-tracking branch 'gerrit/release-4-6'
[alexxy/gromacs.git] / src / gromacs / selection / selvalue.cpp
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  * Implements functions in selvalue.h.
34  *
35  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36  * \ingroup module_selection
37  */
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <smalloc.h>
43
44 #include "gromacs/selection/indexutil.h"
45 #include "gromacs/selection/position.h"
46 #include "gromacs/selection/selvalue.h"
47
48 /*!
49  * \param[out] val  Output structure
50  *
51  * The type of \p val is not touched.
52  * Any contents of \p val are discarded without freeing.
53  */
54 void
55 _gmx_selvalue_clear(gmx_ana_selvalue_t *val)
56 {
57     val->nr     = 0;
58     val->u.ptr  = NULL;
59     val->nalloc = 0;
60 }
61
62 /*!
63  * \param[in,out] val  Value structure to allocate.
64  * \param[in]     n    Maximum number of values needed.
65  * \returns       Zero on success.
66  *
67  * Reserves memory for the values within \p val to store at least \p n values,
68  * of the type specified in the \p val structure.
69  *
70  * If the type is \ref POS_VALUE or \ref GROUP_VALUE, memory is reserved for
71  * the data structures, but no memory is reserved inside these newly allocated
72  * data structures.
73  * Similarly, for \ref STR_VALUE values, the pointers are set to NULL.
74  * For other values, the memory is uninitialized.
75  */
76 int
77 _gmx_selvalue_reserve(gmx_ana_selvalue_t *val, int n)
78 {
79     int  i;
80
81     if (val->nalloc == -1)
82     {
83         return 0;
84     }
85
86     if (!val->u.ptr || val->nalloc < n)
87     {
88         switch (val->type)
89         {
90             case INT_VALUE:   srenew(val->u.i, n); break;
91             case REAL_VALUE:  srenew(val->u.r, n); break;
92             case STR_VALUE:
93                 srenew(val->u.s, n);
94                 for (i = val->nalloc; i < n; ++i)
95                 {
96                     val->u.s[i] = NULL;
97                 }
98                 break;
99             case POS_VALUE:
100                 srenew(val->u.p, n);
101                 for (i = val->nalloc; i < n; ++i)
102                 {
103                     gmx_ana_pos_clear(&val->u.p[i]);
104                 }
105                 break;
106             case GROUP_VALUE:
107                 srenew(val->u.g, n);
108                 for (i = val->nalloc; i < n; ++i)
109                 {
110                     gmx_ana_index_clear(&val->u.g[i]);
111                 }
112                 break;
113             case NO_VALUE:    break;
114         }
115         val->nalloc = n;
116     }
117     return 0;
118 }
119
120 /*!
121  * \param[in,out] val    Value structure to allocate.
122  * \param[in]     ptr    Pointer where the values should be stored.
123  * \returns       Zero on success.
124  *
125  * Automatic memory management is disabled for \p ptr, unless \p ptr is NULL.
126  */
127 int
128 _gmx_selvalue_setstore(gmx_ana_selvalue_t *val, void *ptr)
129 {
130     val->u.ptr  = ptr;
131     val->nalloc = (ptr ? -1 : 0);
132     return 0;
133 }
134
135 /*!
136  * \param[in,out] val    Value structure to allocate.
137  * \param[in]     ptr    Pointer where the values should be stored.
138  * \param[in]     nalloc Number of values allocated for \p ptr.
139  * \returns       Zero on success.
140  */
141 int
142 _gmx_selvalue_setstore_alloc(gmx_ana_selvalue_t *val, void *ptr, int nalloc)
143 {
144     val->u.ptr  = ptr;
145     val->nalloc = nalloc;
146     return 0;
147 }