#ifndef UCX_LIST_H
#define UCX_LIST_H
#include "common.h"
#include "collection.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct cx_list_class_s cx_list_class;
struct cx_list_s {
CX_COLLECTION_BASE;
const cx_list_class *cl;
const cx_list_class *climpl;
};
struct cx_list_class_s {
void (*destructor)(
struct cx_list_s *list);
int (*insert_element)(
struct cx_list_s *list,
size_t index,
const void *data
);
size_t (*insert_array)(
struct cx_list_s *list,
size_t index,
const void *data,
size_t n
);
size_t (*insert_sorted)(
struct cx_list_s *list,
const void *sorted_data,
size_t n
);
int (*insert_iter)(
struct cx_iterator_s *iter,
const void *elem,
int prepend
);
int (*remove)(
struct cx_list_s *list,
size_t index
);
void (*clear)(
struct cx_list_s *list);
int (*swap)(
struct cx_list_s *list,
size_t i,
size_t j
);
void *(*at)(
const struct cx_list_s *list,
size_t index
);
ssize_t (*find_remove)(
struct cx_list_s *list,
const void *elem,
bool remove
);
void (*sort)(
struct cx_list_s *list);
int (*compare)(
const struct cx_list_s *list,
const struct cx_list_s *other
);
void (*reverse)(
struct cx_list_s *list);
struct cx_iterator_s (*iterator)(
const struct cx_list_s *list,
size_t index,
bool backward
);
};
__attribute__((__nonnull__))
size_t cx_list_default_insert_array(
struct cx_list_s *list,
size_t index,
const void *data,
size_t n
);
__attribute__((__nonnull__))
size_t cx_list_default_insert_sorted(
struct cx_list_s *list,
const void *sorted_data,
size_t n
);
__attribute__((__nonnull__))
void cx_list_default_sort(
struct cx_list_s *list);
__attribute__((__nonnull__))
int cx_list_default_swap(
struct cx_list_s *list,
size_t i,
size_t j);
typedef struct cx_list_s CxList;
__attribute__((__nonnull__))
void cxListStoreObjects(CxList *list);
__attribute__((__nonnull__))
void cxListStorePointers(CxList *list);
__attribute__((__nonnull__))
static inline bool cxListIsStoringPointers(
const CxList *list) {
return list->collection.store_pointer;
}
__attribute__((__nonnull__))
static inline
size_t cxListSize(
const CxList *list) {
return list->collection.size;
}
__attribute__((__nonnull__))
static inline
int cxListAdd(
CxList *list,
const void *elem
) {
return list->cl->insert_element(list, list->collection.size, elem);
}
__attribute__((__nonnull__))
static inline
size_t cxListAddArray(
CxList *list,
const void *array,
size_t n
) {
return list->cl->insert_array(list, list->collection.size, array, n);
}
__attribute__((__nonnull__))
static inline
int cxListInsert(
CxList *list,
size_t index,
const void *elem
) {
return list->cl->insert_element(list, index, elem);
}
__attribute__((__nonnull__))
static inline
int cxListInsertSorted(
CxList *list,
const void *elem
) {
const void *data = list->collection.store_pointer ? &elem : elem;
return list->cl->insert_sorted(list, data,
1) ==
0;
}
__attribute__((__nonnull__))
static inline
size_t cxListInsertArray(
CxList *list,
size_t index,
const void *array,
size_t n
) {
return list->cl->insert_array(list, index, array, n);
}
__attribute__((__nonnull__))
static inline
size_t cxListInsertSortedArray(
CxList *list,
const void *array,
size_t n
) {
return list->cl->insert_sorted(list, array, n);
}
__attribute__((__nonnull__))
static inline
int cxListInsertAfter(
CxIterator *iter,
const void *elem
) {
return ((
struct cx_list_s *) iter->src_handle.m)->cl->insert_iter(iter, elem,
0);
}
__attribute__((__nonnull__))
static inline
int cxListInsertBefore(
CxIterator *iter,
const void *elem
) {
return ((
struct cx_list_s *) iter->src_handle.m)->cl->insert_iter(iter, elem,
1);
}
__attribute__((__nonnull__))
static inline
int cxListRemove(
CxList *list,
size_t index
) {
return list->cl->remove(list, index);
}
__attribute__((__nonnull__))
static inline
void cxListClear(CxList *list) {
list->cl->clear(list);
}
__attribute__((__nonnull__))
static inline
int cxListSwap(
CxList *list,
size_t i,
size_t j
) {
return list->cl->swap(list, i, j);
}
__attribute__((__nonnull__))
static inline
void *cxListAt(
CxList *list,
size_t index
) {
return list->cl->at(list, index);
}
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxIterator cxListIteratorAt(
const CxList *list,
size_t index
) {
return list->cl->iterator(list, index, false);
}
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxIterator cxListBackwardsIteratorAt(
const CxList *list,
size_t index
) {
return list->cl->iterator(list, index, true);
}
__attribute__((__nonnull__, __warn_unused_result__))
CxIterator cxListMutIteratorAt(
CxList *list,
size_t index
);
__attribute__((__nonnull__, __warn_unused_result__))
CxIterator cxListMutBackwardsIteratorAt(
CxList *list,
size_t index
);
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxIterator cxListIterator(
const CxList *list) {
return list->cl->iterator(list,
0, false);
}
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxIterator cxListMutIterator(CxList *list) {
return cxListMutIteratorAt(list,
0);
}
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxIterator cxListBackwardsIterator(
const CxList *list) {
return list->cl->iterator(list, list->collection.size -
1, true);
}
__attribute__((__nonnull__, __warn_unused_result__))
static inline CxIterator cxListMutBackwardsIterator(CxList *list) {
return cxListMutBackwardsIteratorAt(list, list->collection.size -
1);
}
__attribute__((__nonnull__))
static inline
ssize_t cxListFind(
const CxList *list,
const void *elem
) {
return list->cl->find_remove((CxList*)list, elem, false);
}
__attribute__((__nonnull__))
static inline
ssize_t cxListFindRemove(
CxList *list,
const void *elem
) {
return list->cl->find_remove(list, elem, true);
}
__attribute__((__nonnull__))
static inline
void cxListSort(CxList *list) {
list->cl->sort(list);
}
__attribute__((__nonnull__))
static inline
void cxListReverse(CxList *list) {
list->cl->reverse(list);
}
__attribute__((__nonnull__))
int cxListCompare(
const CxList *list,
const CxList *other
);
__attribute__((__nonnull__))
void cxListDestroy(CxList *list);
extern CxList *
const cxEmptyList;
#ifdef __cplusplus
}
#endif
#endif