Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
packed_arrays.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef GEOGRAM_BASIC_PACKED_ARRAYS
41 #define GEOGRAM_BASIC_PACKED_ARRAYS
42 
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/numeric.h>
45 #include <geogram/basic/assert.h>
46 #include <geogram/basic/process.h>
47 
53 namespace GEO {
54 
88  class GEOGRAM_API PackedArrays {
89  public:
94 
99 
105  bool thread_safe() const {
106  return thread_safe_;
107  }
108 
113  void set_thread_safe(bool flag);
114 
127  void init(
128  index_t nb_arrays,
129  index_t Z1_block_size,
130  bool static_mode = false
131  );
132 
139  void clear();
140 
145  index_t nb_arrays() const {
146  return nb_arrays_;
147  }
148 
159  index_t array_size(index_t array_index) const {
160  geo_debug_assert(array_index < nb_arrays_);
161  return Z1_[array_index * Z1_stride_];
162  }
163 
177  void get_array(
178  index_t array_index, vector<index_t>& array, bool lock = true
179  ) const {
180  if(lock) {
181  lock_array(array_index);
182  }
183  array.resize(array_size(array_index));
184  if(array.size() != 0) {
185  get_array(array_index, &array[0], false);
186  }
187  if(lock) {
188  unlock_array(array_index);
189  }
190  }
191 
207  void get_array(
208  index_t array_index, index_t* array, bool lock = true
209  ) const;
210 
231  void set_array(
232  index_t array_index,
233  index_t array_size, const index_t* array_elements,
234  bool lock = true
235  );
236 
256  void set_array(
257  index_t array_index,
258  const vector<index_t>& array,
259  bool lock = true
260  ) {
261  if(array.size() == 0) {
262  set_array(array_index, 0, nullptr, lock);
263  } else {
264  set_array(
265  array_index, index_t(array.size()), &array[0], lock
266  );
267  }
268  }
269 
290  index_t array_index, index_t array_size, bool lock
291  );
292 
301  void lock_array(index_t array_index) const {
302  if(thread_safe_) {
303  Z1_spinlocks_.acquire_spinlock(array_index);
304  }
305  }
306 
314  void unlock_array(index_t array_index) const {
315  if(thread_safe_) {
316  Z1_spinlocks_.release_spinlock(array_index);
317  }
318  }
319 
326  void show_stats();
327 
328  protected:
334  bool static_mode() const {
335  return ZV_ == nullptr;
336  }
337 
338  private:
340  PackedArrays(const PackedArrays& rhs);
341 
343  PackedArrays& operator= (const PackedArrays& rhs);
344 
345  private:
346  index_t nb_arrays_;
347  index_t Z1_block_size_;
348  index_t Z1_stride_;
349  index_t* Z1_;
350  index_t** ZV_;
351  bool thread_safe_;
352  mutable Process::SpinLockArray Z1_spinlocks_;
353  };
354 }
355 
356 #endif
Assertion checking mechanism.
#define geo_debug_assert(x)
Verifies that a condition is met.
Definition: assert.h:196
Efficient storage for array of arrays.
Definition: packed_arrays.h:88
index_t array_size(index_t array_index) const
Gets the size of a sub-array.
void get_array(index_t array_index, vector< index_t > &array, bool lock=true) const
Gets a sub-array as a vector.
void show_stats()
Displays array statistics.
void set_array(index_t array_index, const vector< index_t > &array, bool lock=true)
Sets a sub-array from a vector.
void get_array(index_t array_index, index_t *array, bool lock=true) const
Gets a sub-array.
void lock_array(index_t array_index) const
Locks a sub-array.
void clear()
Clears the packed array.
void init(index_t nb_arrays, index_t Z1_block_size, bool static_mode=false)
Initializes a packed array.
PackedArrays()
Creates a new empty packed array.
void unlock_array(index_t array_index) const
Unlocks a sub-array.
void set_thread_safe(bool flag)
Sets the thread-safety mode.
void resize_array(index_t array_index, index_t array_size, bool lock)
Resizes a sub-array.
index_t nb_arrays() const
Get the number of arrays.
bool static_mode() const
Checks if the array is in static mode.
~PackedArrays()
Deletes a packed array.
bool thread_safe() const
Checks the thread-safety mode.
void set_array(index_t array_index, index_t array_size, const index_t *array_elements, bool lock=true)
Sets a sub-array.
An array of light-weight synchronisation primitives (spinlocks).
Definition: thread_sync.h:301
index_t size() const
Gets the number of elements.
Definition: memory.h:674
Common include file, providing basic definitions. Should be included before anything else by all head...
Global Vorpaline namespace.
Definition: basic.h:55
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
Types and functions for numbers manipulation.
Function and classes for process manipulation.