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