Xorn
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
selection.cc
Go to the documentation of this file.
1 /* Copyright (C) 2013-2016 Roland Lutz
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; either version 2 of the License, or
6  (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software Foundation,
15  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
16 
17 #include "internal.h"
18 #include "key_iterator.h"
19 #include <algorithm>
20 
21 
25 {
26  try {
27  return new xorn_selection();
28  } catch (std::bad_alloc const &) {
29  return NULL;
30  }
31 }
32 
36 {
37  xorn_selection_t rsel;
38  try {
39  rsel = new xorn_selection();
40  } catch (std::bad_alloc const &) {
41  return NULL;
42  }
43  try {
44  rsel->insert(ob);
45  } catch (std::bad_alloc const &) {
46  delete rsel;
47  return NULL;
48  }
49  return rsel;
50 }
51 
62 {
63  if (ob != NULL && rev->obstates.find(ob) == rev->obstates.end())
64  return NULL;
65 
66  xorn_selection_t rsel;
67  try {
68  rsel = new xorn_selection();
69  } catch (std::bad_alloc const &) {
70  return NULL;
71  }
72 
73  std::map<xorn_object_t, std::vector<xorn_object_t> >::const_iterator i
74  = rev->children.find(ob);
75  if (i == rev->children.end())
76  return rsel;
77 
78  try {
79  copy(i->second.begin(), i->second.end(),
80  inserter(*rsel, rsel->begin()));
81  } catch (std::bad_alloc const &) {
82  delete rsel;
83  return NULL;
84  }
85  return rsel;
86 }
87 
91 {
92  xorn_selection_t rsel;
93  try {
94  rsel = new xorn_selection();
95  } catch (std::bad_alloc const &) {
96  return NULL;
97  }
98  try {
99  copy(iterate_keys(rev->obstates.begin()),
100  iterate_keys(rev->obstates.end()),
101  inserter(*rsel, rsel->begin()));
102  } catch (std::bad_alloc const &) {
103  delete rsel;
104  return NULL;
105  }
106  return rsel;
107 }
108 
114 {
115  xorn_selection_t rsel;
116  try {
117  rsel = new xorn_selection();
118  } catch (std::bad_alloc const &) {
119  return NULL;
120  }
121  try {
122  set_difference(iterate_keys(rev->obstates.begin()),
123  iterate_keys(rev->obstates.end()),
124  sel->begin(), sel->end(),
125  inserter(*rsel, rsel->begin()));
126  } catch (std::bad_alloc const &) {
127  delete rsel;
128  return NULL;
129  }
130  return rsel;
131 }
132 
137 {
138  xorn_selection_t rsel;
139  try {
140  rsel = new xorn_selection(*sel);
141  } catch (std::bad_alloc const &) {
142  return NULL;
143  }
144  try {
145  rsel->insert(ob);
146  } catch (std::bad_alloc const &) {
147  delete rsel;
148  return NULL;
149  }
150  return rsel;
151 }
152 
157 {
158  xorn_selection_t rsel;
159  try {
160  rsel = new xorn_selection(*sel);
161  } catch (std::bad_alloc const &) {
162  return NULL;
163  }
164  try {
165  xorn_selection::const_iterator i = rsel->find(ob);
166  if (i != rsel->end())
167  rsel->erase(i);
168  } catch (std::bad_alloc const &) {
169  delete rsel;
170  return NULL;
171  }
172  return rsel;
173 }
174 
180 {
181  xorn_selection_t rsel;
182  try {
183  rsel = new xorn_selection();
184  } catch (std::bad_alloc const &) {
185  return NULL;
186  }
187  try {
188  set_union(sel0->begin(), sel0->end(),
189  sel1->begin(), sel1->end(),
190  inserter(*rsel, rsel->begin()));
191  } catch (std::bad_alloc const &) {
192  delete rsel;
193  return NULL;
194  }
195  return rsel;
196 }
197 
203 {
204  xorn_selection_t rsel;
205  try {
206  rsel = new xorn_selection();
207  } catch (std::bad_alloc const &) {
208  return NULL;
209  }
210  try {
211  set_intersection(sel0->begin(), sel0->end(),
212  sel1->begin(), sel1->end(),
213  inserter(*rsel, rsel->begin()));
214  } catch (std::bad_alloc const &) {
215  delete rsel;
216  return NULL;
217  }
218  return rsel;
219 }
220 
226 {
227  xorn_selection_t rsel;
228  try {
229  rsel = new xorn_selection();
230  } catch (std::bad_alloc const &) {
231  return NULL;
232  }
233  try {
234  set_difference(sel0->begin(), sel0->end(),
235  sel1->begin(), sel1->end(),
236  inserter(*rsel, rsel->begin()));
237  } catch (std::bad_alloc const &) {
238  delete rsel;
239  return NULL;
240  }
241  return rsel;
242 }
243 
247 {
248  std::map<xorn_object_t, obstate *>::const_iterator i
249  = rev->obstates.begin();
250  std::set<xorn_object_t>::const_iterator j = sel->begin();
251 
252  while (i != rev->obstates.end() && j != sel->end())
253  if (i->first < *j)
254  ++i;
255  else if (i->first > *j)
256  ++j;
257  else
258  return false;
259 
260  return true;
261 }
262 
268 {
269  return rev->obstates.find(ob) != rev->obstates.end() &&
270  sel->find(ob) != sel->end();
271 }
272 
278 {
279  delete sel;
280 }
xorn_selection_t xorn_select_excluding(xorn_selection_t sel, xorn_object_t ob)
Create a selection which contains all the objects in an existing selection minus a given object...
Definition: selection.cc:156
struct xorn_revision * xorn_revision_t
Opaque type representing the contents of a file.
Definition: xornstorage.h:54
bool xorn_selection_is_empty(xorn_revision_t rev, xorn_selection_t sel)
Return whether a selection is empty in a given revision.
Definition: selection.cc:246
xorn_selection_t xorn_select_attached_to(xorn_revision_t rev, xorn_object_t ob)
Create a selection containing all objects in a revision attached to a given object.
Definition: selection.cc:61
xorn_selection_t xorn_select_none()
Create an empty selection.
Definition: selection.cc:24
bool xorn_object_is_selected(xorn_revision_t rev, xorn_selection_t sel, xorn_object_t ob)
Return whether an object exists in a revision and is selected in a selection.
Definition: selection.cc:266
struct xorn_selection * xorn_selection_t
Opaque type representing the identity of a set of objects.
Definition: xornstorage.h:56
xorn_selection_t xorn_select_all_except(xorn_revision_t rev, xorn_selection_t sel)
Create a selection containing all objects in a revision except those in a given selection.
Definition: selection.cc:112
xorn_selection_t xorn_select_object(xorn_object_t ob)
Create a selection containing a single object.
Definition: selection.cc:35
xorn_selection_t xorn_select_including(xorn_selection_t sel, xorn_object_t ob)
Create a selection which contains all the objects in an existing selection plus a given object...
Definition: selection.cc:136
xorn_selection_t xorn_select_difference(xorn_selection_t sel0, xorn_selection_t sel1)
Create a selection containing the objects contained in one given selection, but not the other...
Definition: selection.cc:224
void xorn_free_selection(xorn_selection_t sel)
Free the memory used for storing a selection.
Definition: selection.cc:277
xorn_selection_t xorn_select_all(xorn_revision_t rev)
Create a selection containing all objects in a revision.
Definition: selection.cc:90
xorn_selection_t xorn_select_union(xorn_selection_t sel0, xorn_selection_t sel1)
Create a selection containing the objects in either given selection.
Definition: selection.cc:178
struct xorn_object * xorn_object_t
Opaque type representing the identity of an object.
Definition: xornstorage.h:55
xorn_selection_t xorn_select_intersection(xorn_selection_t sel0, xorn_selection_t sel1)
Create a selection containing the objects in both given selections.
Definition: selection.cc:201