![]() |
ooc
1.3c
Object Oriented tollkit fo ANSI C
|
Vector class - a standard ooc container. More...
#include "ooc.h"
Macros | |
#define | vector_new_type(chunk_size, pClass, manage) |
Vector constructor. More... | |
#define | vector_use_type_with_store(vector, size, pClass, manage, store) |
Vector constructor. More... | |
Typedefs | |
typedef struct VectorObject * | Vector |
Vector class declaration. | |
typedef size_t | VectorIndex |
Index for the Vector. More... | |
typedef void(* | vector_item_destroyer )(void *item) |
Vector Item Destroyer. More... | |
typedef void(* | vector_item_executor )(void *item, void *param) |
Vector Item Executor. More... | |
typedef int(* | vector_item_checker )(void *item, void *param) |
Vector Item Checker. More... | |
Functions | |
Vector | vector_new (VectorIndex chunk_size, vector_item_destroyer destroyer) |
Vector constructor. More... | |
Vector | _vector_new_type (VectorIndex chunk_size, Class type, int manage) |
Vector constructor. More... | |
Vector | vector_new_from_table (void *table, size_t record_size, VectorIndex table_size) |
Vector constructor. More... | |
void | vector_use_with_store (Vector vector, VectorIndex size, vector_item_destroyer destroyer, void *store[]) |
Vector constructor. More... | |
void | _vector_use_type_with_store (Vector vector, VectorIndex size, Class type, int manage, void *store[]) |
Vector constructor. More... | |
VectorIndex | vector_push_back (Vector vector, void *item) |
Put an item at the end of the Vector. More... | |
VectorIndex | vector_insert (Vector vector, VectorIndex index, void *item) |
Inserts an item into the vector. More... | |
void | vector_delete_item (Vector vector, VectorIndex index) |
Deletes an item from the vector. More... | |
VectorIndex | vector_items (Vector vector) |
Numbers stored in the Vector. More... | |
void | vector_set_item (Vector vector, VectorIndex index, void *item) |
Overwrites an item. More... | |
void * | vector_get_item (Vector vector, VectorIndex index) |
Retrieves an item. More... | |
void | vector_swap (Vector vector, VectorIndex index1, VectorIndex index2) |
Swaps two items in the Vector. More... | |
void | vector_foreach (Vector vector, vector_item_executor task, void *param) |
Executes a task for each item in the Vector. More... | |
VectorIndex | vector_foreach_until_true (Vector vector, vector_item_checker checker, void *param) |
Executes a task for each item until TRUE . More... | |
VectorIndex | vector_find_item (Vector vector, VectorIndex index, vector_item_checker checker, void *param) |
Finds an item in the Vector. More... | |
VectorIndex | vector_find_item_reverse (Vector vector, VectorIndex index, vector_item_checker checker, void *param) |
Finds an item in the Vector in reverse order. More... | |
Class | vector_get_type (Vector vector) |
Get the type of the vector. More... | |
int | vector_get_managed (Vector vector) |
Check if the vector is managed. More... | |
Vector class - a standard ooc container.
Vector is a container class, that can hold pointers to any kind of data. Every item stored in the list must work perfectly with the supplied list item destroyer! In practice this means, that you can only store items in the vector that has the same deletion method. There are two types of Vector: typed and non-typed vector. Typed Vector can hold items of a given type or its subtype only, while non-typed Vector can hold any type of data, that conforms to the supplied object destructor. When creating a Vector, a given amount of space, the chunk size (n*sizeof(void*)) is allocated. If the Vector grows above this size, the Vector automatically reallocates the store, increasing by the chunk size.
foreach
and find
methods may behave unexpectedly if an other thread is modifying the Vector! Make your own locking if needed! The same applies to the ooc_duplicate() method for Vector (uses vector_foreach() internally)! #define vector_new_type | ( | chunk_size, | |
pClass, | |||
manage | |||
) |
Vector constructor.
Creates a new typed vector.
chunk_size | The chunk size of the Vector. This is used as the initial size of the vector, and as a size increment for each reallocation. |
pClass | The type of the items in the Vector. (Name of the class.) |
manage | OOC_MANAGE is the Vector must manage the stored items (delete when destroying the Vector), !OOC_MANAGE if you manage those other way. |
#define vector_use_type_with_store | ( | vector, | |
size, | |||
pClass, | |||
manage, | |||
store | |||
) |
Vector constructor.
Builds a new typed vector from a preallocated block of memory and assigns a static store to it.
vector | The location of the VectorObject to be constructed. |
size | The size of the Vector. This is used as the maximal size of the vector. |
pClass | The type of the items in the Vector. (Name of the class.) |
manage | TRUE is the Vector must manage the stored items (release when destroying the Vector), FALSE if you manage those other way. |
store | The memory block assigned to the Vector that will hold the pointers to the VectorItems. The strore must be statically allocated, and must be large enough to store size pieces of VectorItem pointers. |
typedef int(* vector_item_checker)(void *item, void *param) |
Vector Item Checker.
typedef void(* vector_item_destroyer)(void *item) |
typedef void(* vector_item_executor)(void *item, void *param) |
Vector Item Executor.
typedef size_t VectorIndex |
Vector _vector_new_type | ( | VectorIndex | chunk_size, |
Class | type, | ||
int | manage | ||
) |
Vector constructor.
Creates a new typed vector.
chunk_size | The chunk size of the Vector. This is used as the initial size of the vector, and as a size increment for each reallocation. |
type | The type of the items in the Vector. (Class Table pointer.) |
manage | OOC_MANAGE is the Vector must manage the stored items (delete when destroying the Vector), !OOC_MANAGE if you manage those other way. |
void _vector_use_type_with_store | ( | Vector | vector, |
VectorIndex | size, | ||
Class | type, | ||
int | manage, | ||
void * | store[] | ||
) |
Vector constructor.
Builds a new typed vector from a preallocated block of memory and assigns a static store to it.
vector | The location of the VectorObject to be constructed. |
size | The size of the Vector. This is used as the maximal size of the vector. |
type | The type of the items in the Vector. (Class Table pointer.) |
manage | TRUE is the Vector must manage the stored items (release when destroying the Vector), FALSE if you manage those other way. |
store | The memory block assigned to the Vector that will hold the pointers to the VectorItems. The strore must be statically allocated, and must be large enough to store size pieces of VectorItem pointers. |
void vector_delete_item | ( | Vector | vector, |
VectorIndex | index | ||
) |
Deletes an item from the vector.
Deletes an item from the Vector at the index. The rest of the Vector is shifted.
vector | The vector. |
index | The point where to delete the item from the Vector. For managed vectors or if destroyer supplied, the item is destroyed as well. |
VectorIndex vector_find_item | ( | Vector | vector, |
VectorIndex | index, | ||
vector_item_checker | checker, | ||
void * | param | ||
) |
Finds an item in the Vector.
Executes the checker
task for each item in the vector
, starting from position index
, until the checker
returns TRUE
. (If checker
returns FALSE
then will continue with the next item in the vector
, otherwise the execution will stop.)
vector | The Vector |
index | The starting position of the search. |
checker | The checker to be executed. The checker is a C function, and must have two parameters: the first will point to the item, the second holpd the parameter. This is a normal way of calling a class method too. The checker must return TRUE if we found the item or FALSE otherwise. |
param | The second parameter to be passed to the checker . |
checker
returned TRUE
). If the checker
did not returned TRUE
, the return value is an index value pointing behind the last item in the vector (== vector_items(), an invalid index!) VectorIndex vector_find_item_reverse | ( | Vector | vector, |
VectorIndex | index, | ||
vector_item_checker | checker, | ||
void * | param | ||
) |
Finds an item in the Vector in reverse order.
Executes the checker
task for each item in the vector
in reverse order, starting from position index
, until the checker
returns TRUE
. (If checker
returns FALSE
then will continue with the next item in the vector
, otherwise the execution will stop.)
vector | The Vector |
index | The starting position of the search. |
checker | The checker to be executed. The checker is a C function, and must have two parameters: the first will point to the item, the second holds the parameter. This is a normal way of calling a class method too. The checker must return TRUE if we found the item or FALSE otherwise. |
param | The second parameter to be passed to the checker . |
checker
returned TRUE
). If the checker
did not returned TRUE
, the return value is an index value pointing behind the last item in the vector (== vector_items(), an invalid index!) void vector_foreach | ( | Vector | vector, |
vector_item_executor | task, | ||
void * | param | ||
) |
Executes a task for each item in the Vector.
vector | The Vector |
task | The task to be executed. The task is a C function, and must have two parameters: the first will point to the item, the second holpd the parameter. This is a normal way of calling a class method too. |
param | The second parameter to be passed to the task . |
VectorIndex vector_foreach_until_true | ( | Vector | vector, |
vector_item_checker | checker, | ||
void * | param | ||
) |
Executes a task for each item until TRUE
.
Executes the checker
task for each item in the vector
, until the checker
returns TRUE
. (If checker
returns FALSE
then will continue with the next item in the vector
, otherwise the execution will stop.)
vector | The Vector |
checker | The task to be executed. The checker is a C function, and must have two parameters: the first will point to the item, the second holpd the parameter. This is a normal way of calling a class method too. The checker must return TRUE or @ FALSE. |
param | The second parameter to be passed to the checker . |
checker
returned TRUE
). If the checker
did not returned TRUE
, the return value is an index value pointing behind the last item in the vector (== vector_items(), an invalid index!) void* vector_get_item | ( | Vector | vector, |
VectorIndex | index | ||
) |
int vector_get_managed | ( | Vector | vector | ) |
Check if the vector is managed.
Check is the items in the vector are managed by the vector.
vector | The vector |
Get the type of the vector.
Returns the type of the vector in case of a typed vector.
vector | The vector |
VectorIndex vector_insert | ( | Vector | vector, |
VectorIndex | index, | ||
void * | item | ||
) |
Inserts an item into the vector.
Inserts an item into the Vector at the index. The rest of the Vector is shifted.
vector | The vector. |
index | The point to insert the item. The new item will be accessed at index. |
item | The item to insert. |
Exception(err_bad_cast)
) exception is thrown. VectorIndex vector_items | ( | Vector | vector | ) |
Vector vector_new | ( | VectorIndex | chunk_size, |
vector_item_destroyer | destroyer | ||
) |
Vector constructor.
Creates a new non-typed vector.
chunk_size | The chunk size of the Vector. This is used as the initial size of the vector, and as a size increment for each reallocation. |
destroyer | The destructor for the items stored in the Vector (usually ooc_free() or ooc_delete()). You can pass NULL if you store static items in the Vector, or you take care of the item's deletion other way. |
Vector vector_new_from_table | ( | void * | table, |
size_t | record_size, | ||
VectorIndex | table_size | ||
) |
Vector constructor.
Creates a new non-typed Vector from a data table. All items in the Vector are treated static, they are not freed when destroying Vector.
table | The source table. |
record_size | The record size in the table. |
table_size | The number of records in the table. |
VectorIndex vector_push_back | ( | Vector | vector, |
void * | item | ||
) |
Put an item at the end of the Vector.
vector | The vector. |
item | The item to be put. |
Exception(err_bad_cast)
) exception is thrown. void vector_set_item | ( | Vector | vector, |
VectorIndex | index, | ||
void * | item | ||
) |
Overwrites an item.
Overwrites an item at index.
vector | The vector. |
index | The position in the vector. |
item | The item to put in the vector. |
void vector_swap | ( | Vector | vector, |
VectorIndex | index1, | ||
VectorIndex | index2 | ||
) |
void vector_use_with_store | ( | Vector | vector, |
VectorIndex | size, | ||
vector_item_destroyer | destroyer, | ||
void * | store[] | ||
) |
Vector constructor.
Builds a new non-typed vector from a preallocated block of memory and assigns a static store to it.
vector | The location of the VectorObject to be constructed. |
size | The size of the Vector. This is used as the maximal size of the vector. |
destroyer | The destructor for the items stored in the Vector (usually ooc_release()). You can pass NULL if you store static items in the Vector, or you take care of releasing the item's other way. |
store | The memory block assigned to the Vector that will hold the pointers to the VectorItems. The strore must be statically allocated, and must be large enough to store size pieces of void* . #define MY_VECTOR_SIZE 13
VectoItem myVectorStore[ MY_VECTOR_SIZE ];
struct VectorObject myVector;
vector_use_with_store( & myVector, MY_VECTOR_SIZE, NULL, myVectorStore );
|