1 /* EINA - EFL data type library
2 * Copyright (C) 2009 Gustavo Sverzut Barbieri
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
19 #ifndef EINA_MATRIXSPARSE_H_
20 #define EINA_MATRIXSPARSE_H_
24 #include "eina_config.h"
26 #include "eina_types.h"
27 #include "eina_iterator.h"
28 #include "eina_accessor.h"
31 * @addtogroup Eina_Matrixsparse_Group Sparse Matrix
33 * @brief These functions provide matrix sparse management.
35 * For more information, you can look at the @ref tutorial_matrixsparse_page.
39 * @addtogroup Eina_Data_Types_Group Data Types
45 * @addtogroup Eina_Containers_Group Containers
51 * @defgroup Eina_Matrixsparse_Group Sparse Matrix
57 * @typedef Eina_Matrixsparse
58 * Type for a generic sparse matrix.
60 typedef struct _Eina_Matrixsparse Eina_Matrixsparse;
63 * @typedef Eina_Matrixsparse_Row
64 * Type for a generic sparse matrix row, opaque for users.
66 typedef struct _Eina_Matrixsparse_Row Eina_Matrixsparse_Row;
69 * @typedef Eina_Matrixsparse_Cell
70 * Type for a generic sparse matrix cell, opaque for users.
72 typedef struct _Eina_Matrixsparse_Cell Eina_Matrixsparse_Cell;
74 /* constructors and destructors */
77 * @brief Create a new Sparse Matrix.
79 * @param rows number of rows in matrix. Operations with rows greater than this
81 * @param cols number of columns in matrix. Operations with columns greater
82 * than this value will fail.
83 * @param free_func used to delete cell data contents, used by
84 * eina_matrixsparse_free(), eina_matrixsparse_size_set(),
85 * eina_matrixsparse_row_idx_clear(),
86 * eina_matrixsparse_column_idx_clear(),
87 * eina_matrixsparse_cell_idx_clear() and possible others.
88 * @param user_data given to @a free_func as first parameter.
90 * @return Newly allocated matrix or @c NULL if allocation failed and eina_error
93 EAPI Eina_Matrixsparse *eina_matrixsparse_new(unsigned long rows,
95 void (*free_func)(void *user_data,
97 const void *user_data);
100 * @brief Free resources allocated to Sparse Matrix.
102 * @param m The Sparse Matrix instance to free, must @b not be @c NULL.
104 EAPI void eina_matrixsparse_free(Eina_Matrixsparse *m);
106 /* size manipulation */
109 * @brief Get the current size of Sparse Matrix.
111 * The given parameters are guaranteed to be set if they're not @c NULL,
112 * even if this function fails (ie: @a m is not a valid matrix instance).
114 * @param m the sparse matrix to operate on.
115 * @param rows returns the number of rows, may be @c NULL. If @a m is invalid,
116 * returned value is zero, otherwise it's a positive integer.
117 * @param cols returns the number of columns, may be @c NULL. If @a m is
118 * invalid, returned value is zero, otherwise it's a positive integer.
120 EAPI void eina_matrixsparse_size_get(const Eina_Matrixsparse *m,
122 unsigned long *cols);
125 * @brief Resize the Sparse Matrix.
127 * This will resize the sparse matrix, possibly freeing cells on rows
128 * and columns that will cease to exist.
130 * @param m the sparse matrix to operate on.
131 * @param rows the new number of rows, must be greater than zero.
132 * @param cols the new number of columns, must be greater than zero.
133 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
135 * @warning cells, rows or columns are not reference counted and thus
136 * after this call any reference might be invalid if instance were
139 EAPI Eina_Bool eina_matrixsparse_size_set(Eina_Matrixsparse *m,
146 * Get the cell reference inside Sparse Matrix.
148 * @param m the sparse matrix to operate on.
149 * @param row the new number of row to clear.
150 * @param col the new number of column to clear.
151 * @param cell pointer to return cell reference, if any exists.
153 * @return @c 1 on success, @c 0 on failure. It is considered success if did not
154 * exist but index is inside matrix size, in this case @c *cell == NULL
156 * @see eina_matrixsparse_cell_data_get()
157 * @see eina_matrixsparse_data_idx_get()
159 EAPI Eina_Bool eina_matrixsparse_cell_idx_get(const Eina_Matrixsparse *m, unsigned long row, unsigned long col, Eina_Matrixsparse_Cell **cell);
162 * Get data associated with given cell reference.
164 * @param cell given cell reference, must @b not be @c NULL.
166 * @return data associated with given cell.
168 * @see eina_matrixsparse_cell_idx_get()
169 * @see eina_matrixsparse_data_idx_get()
171 EAPI void *eina_matrixsparse_cell_data_get(const Eina_Matrixsparse_Cell *cell);
174 * Get data associated with given cell given its indexes.
176 * @param m the sparse matrix to operate on.
177 * @param row the new number of row to clear.
178 * @param col the new number of column to clear.
180 * @return Data associated with given cell or @c NULL if nothing is associated.
182 * @see eina_matrixsparse_cell_idx_get()
183 * @see eina_matrixsparse_cell_data_get()
185 EAPI void *eina_matrixsparse_data_idx_get(const Eina_Matrixsparse *m, unsigned long row, unsigned long col);
188 * Get position (indexes) of the given cell.
190 * @param cell the cell reference, must @b not be @c NULL.
191 * @param row where to store cell row number, may be @c NULL.
192 * @param col where to store cell column number, may be @c NULL.
194 * @return #EINA_TRUE on success, #EINA_FALSE otherwise (@c cell is @c NULL).
196 EAPI Eina_Bool eina_matrixsparse_cell_position_get(const Eina_Matrixsparse_Cell *cell, unsigned long *row, unsigned long *col);
201 * Change cell reference value without freeing the possibly existing old value.
203 * @param cell the cell reference, must @b not be @c NULL.
204 * @param data new data to set.
205 * @param p_old returns the old value intact (not freed).
207 * @return #EINA_TRUE on success, #EINA_FALSE otherwise (@a cell is @c NULL).
209 * @see eina_matrixsparse_cell_data_set()
210 * @see eina_matrixsparse_data_idx_replace()
212 EAPI Eina_Bool eina_matrixsparse_cell_data_replace(Eina_Matrixsparse_Cell *cell, const void *data, void **p_old);
215 * Change cell value freeing the possibly existing old value.
217 * In contrast to eina_matrixsparse_cell_data_replace(), this function will
218 * call @c free_func() on existing value.
220 * @param cell the cell reference, must @b not be @c NULL.
221 * @param data new data to set.
223 * @return #EINA_TRUE on success, #EINA_FALSE otherwise (@a cell is @c NULL).
225 * @see eina_matrixsparse_cell_data_replace()
226 * @see eina_matrixsparse_data_idx_set()
228 EAPI Eina_Bool eina_matrixsparse_cell_data_set(Eina_Matrixsparse_Cell *cell, const void *data);
231 * Change cell value without freeing the possibly existing old value, using
234 * @param m the sparse matrix, must @b not be @c NULL.
235 * @param row the row number to set the value.
236 * @param col the column number to set the value.
237 * @param data new data to set.
238 * @param p_old returns the old value intact (not freed).
240 * @return #EINA_TRUE on success, #EINA_FALSE otherwise (@a m is @c NULL, indexes are not valid).
242 * @see eina_matrixsparse_cell_data_replace()
243 * @see eina_matrixsparse_data_idx_set()
245 EAPI Eina_Bool eina_matrixsparse_data_idx_replace(Eina_Matrixsparse *m, unsigned long row, unsigned long col, const void *data, void **p_old);
248 * Change cell value freeing the possibly existing old value, using
251 * In contrast to eina_matrixsparse_data_idx_replace(), this function will
252 * call @c free_func() on existing value.
254 * @param m the sparse matrix, must @b not be @c NULL.
255 * @param row the row number to set the value.
256 * @param col the column number to set the value.
257 * @param data new data to set.
259 * @return #EINA_TRUE on success, #EINA_FALSE otherwise (@a m is @c NULL, indexes are not valid).
261 * @see eina_matrixsparse_cell_data_replace()
263 EAPI Eina_Bool eina_matrixsparse_data_idx_set(Eina_Matrixsparse *m, unsigned long row, unsigned long col, const void *data);
268 * Clear (erase all cells) of row given its index.
270 * Existing cells will be cleared with @c free_func() given to
271 * eina_matrixsparse_new().
273 * @param m the sparse matrix to operate on.
274 * @param row the new number of row to clear.
276 * @return #EINA_TRUE on success, #EINA_FALSE on failure. It is considered success if row
277 * had no cells filled. Failure is asking for clear row outside
280 * @warning cells, rows or columns are not reference counted and thus
281 * after this call any reference might be invalid if instance were
284 EAPI Eina_Bool eina_matrixsparse_row_idx_clear(Eina_Matrixsparse *m, unsigned long row);
287 * Clear (erase all cells) of column given its index.
289 * Existing cells will be cleared with @c free_func() given to
290 * eina_matrixsparse_new().
292 * @param m the sparse matrix to operate on.
293 * @param col the new number of column to clear.
295 * @return #EINA_TRUE on success, #EINA_FALSE on failure. It is considered success if column
296 * had no cells filled. Failure is asking for clear column outside
299 * @warning cells, rows or columns are not reference counted and thus
300 * after this call any reference might be invalid if instance were
303 EAPI Eina_Bool eina_matrixsparse_column_idx_clear(Eina_Matrixsparse *m, unsigned long col);
306 * Clear (erase) cell given its indexes.
308 * Existing cell will be cleared with @c free_func() given to
309 * eina_matrixsparse_new().
311 * @param m the sparse matrix to operate on.
312 * @param row the new number of row to clear.
313 * @param col the new number of column to clear.
315 * @return #EINA_TRUE on success, #EINA_FALSE on failure. It is considered success if did not
316 * exist but index is inside matrix size.
318 * @warning cells, rows or columns are not reference counted and thus
319 * after this call any reference might be invalid if instance were
322 * @note This call might delete container column and row if this cell was the
325 EAPI Eina_Bool eina_matrixsparse_cell_idx_clear(Eina_Matrixsparse *m, unsigned long row, unsigned long col);
328 * Clear (erase) cell given its reference.
330 * @param cell the cell reference, must @b not be @c NULL.
332 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
334 * @warning cells, rows or columns are not reference counted and thus
335 * after this call any reference might be invalid if instance were
338 * @note This call might delete container column and row if this cell was the
341 EAPI Eina_Bool eina_matrixsparse_cell_clear(Eina_Matrixsparse_Cell *cell);
346 * Creates a new iterator over existing matrix cells.
348 * This is a cheap walk, it will just report existing cells and holes
349 * in the sparse matrix will be ignored. That means the reported
350 * indexes will not be sequential.
352 * The iterator data will be the cell reference, one may query current
353 * position with eina_matrixsparse_cell_position_get() and cell value
354 * with eina_matrixsparse_cell_data_get().
356 * @param m The Sparse Matrix reference, must @b not be @c NULL.
357 * @return A new iterator.
359 * @warning if the matrix structure changes then the iterator becomes
360 * invalid! That is, if you add or remove cells this iterator
361 * behavior is undefined and your program may crash!
363 EAPI Eina_Iterator *eina_matrixsparse_iterator_new(const Eina_Matrixsparse *m);
366 * Creates a new iterator over all matrix cells.
368 * Unlike eina_matrixsparse_iterator_new() this one will report all
369 * matrix cells, even those that are still empty (holes). These will
370 * be reported as dummy cells that contains no data.
372 * Be aware that iterating a big matrix (1000x1000) will call your
373 * function that number of times (1000000 times in that case) even if
374 * your matrix have no elements at all!
376 * The iterator data will be the cell reference, one may query current
377 * position with eina_matrixsparse_cell_position_get() and cell value
378 * with eina_matrixsparse_cell_data_get(). If cell is empty then the
379 * reference will be a dummy/placeholder, thus setting value with
380 * eina_matrixsparse_cell_data_set() will leave pointer unreferenced.
382 * @param m The Sparse Matrix reference, must @b not be @c NULL.
383 * @return A new iterator.
385 * @warning if the matrix structure changes then the iterator becomes
386 * invalid! That is, if you add or remove cells this iterator
387 * behavior is undefined and your program may crash!
389 EAPI Eina_Iterator *eina_matrixsparse_iterator_complete_new(const Eina_Matrixsparse *m);
403 #endif /* EINA_MATRIXSPARSE_H_ */