Loading [MathJax]/extensions/TeX/AMSsymbols.js
LAL 7.7.0.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
LALDictSequence.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2023 Jolien Creighton
3*
4* This program is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation; either version 2 of the License, or
7* (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*
14* You should have received a copy of the GNU General Public License
15* along with with program; see the file COPYING. If not, write to the
16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17* MA 02110-1301 USA
18*/
19
20#include <lal/LALStdlib.h>
21#include <lal/LALDict.h>
22#include <lal/LALDictSequence.h>
23
24void XLALDestroyDictSequence(LALDictSequence *sequence)
25{
26 if (sequence) {
27 if (sequence->data) {
28 for (size_t i = 0; i < sequence->length; ++i)
29 XLALDestroyDict(sequence->data[i]);
30 LALFree(sequence->data);
31 }
32 LALFree(sequence);
33 }
34 return;
35}
36
37LALDictSequence * XLALCreateDictSequence(size_t length)
38{
39 LALDictSequence *sequence;
40 sequence = LALMalloc(sizeof(*sequence));
42 sequence->length = length;
43 sequence->data = LALCalloc(length, sizeof(*sequence->data));
44 if (sequence->data == NULL) {
45 LALFree(sequence);
47 }
48 for (size_t i = 0; i < length; ++i) {
49 sequence->data[i] = XLALCreateDict();
50 if (sequence->data[i] == NULL) {
53 }
54 }
55 return sequence;
56}
57
58LALDictSequence * XLALCutDictSequence(const LALDictSequence *sequence, size_t first, size_t length)
59{
60 LALDictSequence *new;
62 XLAL_CHECK_NULL(first + length <= sequence->length, XLAL_EBADLEN);
63 new = XLALCreateDictSequence(length);
65 for (size_t i = 0; i < length; ++i) {
66 int retval = XLALDictUpdate(new->data[i], sequence->data[i + first]);
67 if (retval < 0) {
70 }
71 }
72 return new;
73}
74
75LALDictSequence * XLALCopyDictSequence(const LALDictSequence *sequence)
76{
78 return XLALCutDictSequence(sequence, 0, sequence->length);
79}
80
81/* helper routine to reverse elements in sequence in range [start, end) */
82/* note: calling routine is responsible for ensuring that end > start */
83static void reverse(LALDict **a, size_t start, size_t end)
84{
85 size_t left = start;
86 size_t right = end - 1;
87 while (left < right) {
88 LALDict *tmp = a[left];
89 a[left] = a[right];
90 a[right] = tmp;
91 ++left;
92 --right;
93 }
94}
95
96void XLALRollDictSequence(LALDictSequence *sequence, int count)
97{
98 size_t modabscount;
99 size_t shift;
100
101 XLAL_CHECK_VOID(sequence, XLAL_EFAULT);
102 XLAL_CHECK_VOID(sequence->data || sequence->length == 0, XLAL_EINVAL);
103
104 modabscount = abs(count) % sequence->length;
105 shift = count > 0 ? sequence->length - modabscount: modabscount;
106
107 if (shift != 0) {
108 reverse(sequence->data, 0, shift);
109 reverse(sequence->data, shift, sequence->length);
110 reverse(sequence->data, 0, sequence->length);
111 }
112 return;
113}
114
115void XLALShiftDictSequence(LALDictSequence *sequence, int count)
116{
117 size_t abscount = count > 0 ? count : -count;
118
119 XLAL_CHECK_VOID(sequence, XLAL_EFAULT);
120 XLAL_CHECK_VOID(sequence->data || sequence->length == 0, XLAL_EINVAL);
121
122 if (count == 0) /* no-op */
123 return;
124
125 if (abscount >= sequence->length) {
126 /* shifted beyond bounds: clear the entire sequence */
127 for (size_t i = 0; i < sequence->length; ++i)
128 XLALClearDict(sequence->data[i]);
129 } else if (count < 0) {
130 /* clear the first abscount entries and then roll backwards */
131 for (size_t i = 0; i < abscount; ++i)
132 XLALClearDict(sequence->data[i]);
133 XLALRollDictSequence(sequence, count);
134 } else {
135 /* roll forwards and then clear the first abscount entries */
136 XLALRollDictSequence(sequence, count);
137 for (size_t i = 0; i < abscount; ++i)
138 XLALClearDict(sequence->data[i]);
139 }
140
141 return;
142}
143
144LALDictSequence * XLALResizeDictSequence(LALDictSequence *sequence, int first, size_t length)
145{
146 LALDict **data;
147 XLAL_CHECK_NULL(sequence, XLAL_EFAULT);
148 XLAL_CHECK_NULL(sequence->data || sequence->length == 0, XLAL_EINVAL);
149
150 if (length > sequence->length) { /* need to increase memory */
151 data = XLALRealloc(sequence->data, length * sizeof(*sequence->data));
153 for (size_t i = sequence->length; i < length; ++i)
154 data[i] = XLALCreateDict();
155 sequence->data = data;
156 sequence->length = length;
157 XLALShiftDictSequence(sequence, -first);
158 } else if (length > 0) { /* need to decrease memory */
159 XLALShiftDictSequence(sequence, -first);
160 for (size_t i = length; i < sequence->length; ++i)
161 XLALDestroyDict(sequence->data[i]);
162 data = XLALRealloc(sequence->data, length * sizeof(*sequence->data));
164 sequence->data = data;
165 sequence->length = length;
166 } else { /* length == 0: need to release all memory */
167 for (size_t i = 0; i < sequence->length; ++i)
168 XLALDestroyDict(sequence->data[i]);
169 XLALFree(sequence->data);
170 sequence->data = NULL;
171 sequence->length = 0;
172 }
173
174 return sequence;
175}
176
177size_t XLALDictSequenceLength(LALDictSequence *sequence)
178{
179 XLAL_CHECK(sequence, XLAL_EFAULT);
180 return sequence->length;
181}
182
183LALDict * XLALDictSequenceGet(LALDictSequence *sequence, int pos)
184{
185 XLAL_CHECK_NULL(sequence, XLAL_EFAULT);
186 XLAL_CHECK_NULL(sequence->data || sequence->length == 0, XLAL_EINVAL);
187 if (pos < 0) /* negative positions are from end */
188 pos += sequence->length;
189 XLAL_CHECK_NULL(pos >= 0 && (unsigned)pos < sequence->length, XLAL_EBADLEN);
190 return XLALDictDuplicate(sequence->data[pos]);
191}
192
193int XLALDictSequenceSet(LALDictSequence *sequence, LALDict *dict, int pos)
194{
195 XLAL_CHECK(sequence, XLAL_EFAULT);
196 XLAL_CHECK(sequence->data || sequence->length == 0, XLAL_EINVAL);
197 if (pos < 0) /* negative positions are from end */
198 pos += sequence->length;
199 XLAL_CHECK(pos >= 0 && (unsigned)pos < sequence->length, XLAL_EBADLEN);
200 XLALDestroyDict(sequence->data[pos]);
201 sequence->data[pos] = XLALDictDuplicate(dict);
202 return 0;
203}
int XLALDictUpdate(LALDict *dst, const LALDict *src)
Definition: LALDict.c:208
void XLALDestroyDict(LALDict *dict)
Definition: LALDict.c:137
LALDict * XLALDictDuplicate(const LALDict *orig)
Definition: LALDict.c:239
LALDict * XLALCreateDict(void)
Definition: LALDict.c:148
void XLALClearDict(LALDict *dict)
Definition: LALDict.c:127
void XLALDestroyDictSequence(LALDictSequence *sequence)
LALDictSequence * XLALCopyDictSequence(const LALDictSequence *sequence)
void XLALRollDictSequence(LALDictSequence *sequence, int count)
LALDict * XLALDictSequenceGet(LALDictSequence *sequence, int pos)
LALDictSequence * XLALResizeDictSequence(LALDictSequence *sequence, int first, size_t length)
void XLALShiftDictSequence(LALDictSequence *sequence, int count)
LALDictSequence * XLALCutDictSequence(const LALDictSequence *sequence, size_t first, size_t length)
int XLALDictSequenceSet(LALDictSequence *sequence, LALDict *dict, int pos)
LALDictSequence * XLALCreateDictSequence(size_t length)
size_t XLALDictSequenceLength(LALDictSequence *sequence)
static void reverse(LALDict **a, size_t start, size_t end)
#define LALCalloc(m, n)
Definition: LALMalloc.h:94
#define LALMalloc(n)
Definition: LALMalloc.h:93
#define LALFree(p)
Definition: LALMalloc.h:96
#define XLALFree(p)
Definition: LALMalloc.h:47
#define XLALRealloc(p, n)
Definition: LALMalloc.h:46
static const INT4 a
Definition: Random.c:79
#define XLAL_ERROR_NULL(...)
Macro to invoke a failure from a XLAL routine returning a pointer.
Definition: XLALError.h:713
#define XLAL_CHECK(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns an integ...
Definition: XLALError.h:810
#define XLAL_CHECK_VOID(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns void.
Definition: XLALError.h:840
#define XLAL_CHECK_NULL(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns a pointe...
Definition: XLALError.h:825
@ XLAL_EBADLEN
Inconsistent or invalid length.
Definition: XLALError.h:419
@ XLAL_ENOMEM
Memory allocation error.
Definition: XLALError.h:407
@ XLAL_EFAULT
Invalid pointer.
Definition: XLALError.h:408
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
Definition: XLALError.h:462
@ XLAL_EINVAL
Invalid argument.
Definition: XLALError.h:409