Skip to content

Commit

Permalink
ell 1.3.0: adjust APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyht committed May 6, 2019
1 parent 667f13a commit 00e77db
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 167 deletions.
52 changes: 25 additions & 27 deletions src/libs/etools/ell.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
///
/// Description: an easier double link list
///
/// Version: 1.0
/// Version: 1.3
/// Created: 05/04/2017 04:50:18 PM
/// Revision: none
/// Compiler: gcc
Expand All @@ -23,7 +23,7 @@
#include "ell.h"
#include "eobj_p.h"

#define ELL_VERSION "ell 1.2.1"
#define ELL_VERSION "ell 1.3.0" // adjust APIs

#pragma pack(1)

Expand Down Expand Up @@ -411,38 +411,36 @@ static eobj _ell_setRaw(ell l, int idx, eval v, uint need_len, _eotype type)
return _n_o(n);
}

eobj ell_setI(ell l, int idx, i64 val) { return _ell_setRaw(l, idx, *(eval*)&val, 8, _ELL_COE_NUM_I); }
eobj ell_setF(ell l, int idx, f64 val) { return _ell_setRaw(l, idx, *(eval*)&val, 8, _ELL_COE_NUM_F); }
eobj ell_setP(ell l, int idx, conptr val) { return _ell_setRaw(l, idx, *(eval*)&val, 8, _ELL_COE_PTR ); }
eobj ell_setS(ell l, int idx, constr str) { return _ell_setRaw(l, idx, *(eval*)&str, str ? strlen(str) : 0, _ELL_COE_STR ); }
eobj ell_setR(ell l, int idx, size_t len) { return _ell_setRaw(l, idx, *(eval*)&len, len, _ELL_COE_RAW ); }
eobj ell_isetI(ell l, int idx, i64 val) { return _ell_setRaw(l, idx, *(eval*)&val, 8, _ELL_COE_NUM_I); }
eobj ell_isetF(ell l, int idx, f64 val) { return _ell_setRaw(l, idx, *(eval*)&val, 8, _ELL_COE_NUM_F); }
eobj ell_isetP(ell l, int idx, conptr val) { return _ell_setRaw(l, idx, *(eval*)&val, 8, _ELL_COE_PTR ); }
eobj ell_isetS(ell l, int idx, constr str) { return _ell_setRaw(l, idx, *(eval*)&str, str ? strlen(str) : 0, _ELL_COE_STR ); }
eobj ell_isetR(ell l, int idx, size_t len) { return _ell_setRaw(l, idx, *(eval*)&len, len, _ELL_COE_RAW ); }


inline eobj ell_first(ell l) { return _c_head(l) ? _n_o(_c_head(l)) : 0; }
inline eobj ell_last (ell l) { return _c_tail(l) ? _n_o(_c_tail(l)) : 0; }
inline eobj ell_next (eobj obj) { return _n_next(_eo_dn(obj)) ? _n_o(_n_next(_eo_dn(obj))) : 0; }
inline eobj ell_prev (eobj obj) { return _n_prev(_eo_dn(obj)) ? _n_o(_n_prev(_eo_dn(obj))) : 0; }

eobj ell_takeH (ell l) { _elln n; is1_ret(!l || !_c_len(l), 0); _elist_takeH(l, n); return _n_o(n); }
eobj ell_takeT (ell l) { _elln n; is1_ret(!l || !_c_len(l), 0); _elist_takeT(l, n); return _n_o(n); }
eobj ell_takeAt(ell l, uint idx) { _elln n; is1_ret(!l || idx >= _c_len(l), 0); _elist_at (l, idx, n); _elist_take(l, n, idx); return _n_o(n); }
eobj ell_takeO (ell l, eobj obj) { _elln n; register uint i; is1_ret(!l || !obj, 0); _elist_find(l, obj, i); is1_elsret(n, _elist_take(l, n, i), 0); return _n_o(n); }

eobj ell_at (ell l, uint idx) { _elln n; is1_ret(!l || idx >= _c_len(l), 0); _elist_at(l, idx, n); return _n_o(n); }
eobj ell_val (ell l, uint idx) { _elln n; is1_ret(!l || idx >= _c_len(l), 0); _elist_at(l, idx, n); return _n_o(n); }
i64 ell_valI(ell l, uint idx) { eobj o = ell_at(l, idx); _eo_retI(o); }
f64 ell_valF(ell l, uint idx) { eobj o = ell_at(l, idx); _eo_retF(o); }
cstr ell_valS(ell l, uint idx) { eobj o = ell_at(l, idx); _eo_retS(o); }
cptr ell_valP(ell l, uint idx) { eobj o = ell_at(l, idx); _eo_retP(o); }
cptr ell_valR(ell l, uint idx) { eobj o = ell_at(l, idx); _eo_retR(o); }

etypeo ell_valType (ell l, uint idx) { eobj o = ell_at(l, idx); _eo_retT(o); }
uint ell_valLen (ell l, uint idx) { eobj o = ell_at(l, idx); _eo_retL(o); }
bool ell_valIsTrue(ell l, uint idx) { return __eobj_isTrue(ell_at(l, idx)); }

int ell_freeH(ell l) { cptr o = ell_takeH (l ); if(o) { _n_free(_eo_dn(o)); return 1; } return 0; }
int ell_freeT(ell l) { cptr o = ell_takeT (l ); if(o) { _n_free(_eo_dn(o)); return 1; } return 0; }
int ell_freeI(ell l, uint idx){ cptr o = ell_takeAt(l, idx); if(o) { _n_free(_eo_dn(o)); return 1; } return 0; }
eobj ell_takeH(ell l) { _elln n; is1_ret(!l || !_c_len(l), 0); _elist_takeH(l, n); return _n_o(n); }
eobj ell_takeT(ell l) { _elln n; is1_ret(!l || !_c_len(l), 0); _elist_takeT(l, n); return _n_o(n); }
eobj ell_takeI(ell l, uint idx) { _elln n; is1_ret(!l || idx >= _c_len(l), 0); _elist_at (l, idx, n); _elist_take(l, n, idx); return _n_o(n); }
eobj ell_takeO(ell l, eobj obj) { _elln n; register uint i; is1_ret(!l || !obj, 0); _elist_find(l, obj, i); is1_elsret(n, _elist_take(l, n, i), 0); return _n_o(n); }

eobj ell_i (ell l, uint idx) { _elln n; is1_ret(!l || idx >= _c_len(l), 0); _elist_at(l, idx, n); return _n_o(n); }
i64 ell_ivalI (ell l, uint idx) { eobj o = ell_i(l, idx); _eo_retI(o); }
f64 ell_ivalF (ell l, uint idx) { eobj o = ell_i(l, idx); _eo_retF(o); }
cstr ell_ivalS (ell l, uint idx) { eobj o = ell_i(l, idx); _eo_retS(o); }
cptr ell_ivalP (ell l, uint idx) { eobj o = ell_i(l, idx); _eo_retP(o); }
cptr ell_ivalR (ell l, uint idx) { eobj o = ell_i(l, idx); _eo_retR(o); }
etypeo ell_itype (ell l, uint idx) { eobj o = ell_i(l, idx); _eo_retT(o); }
uint ell_ilen (ell l, uint idx) { eobj o = ell_i(l, idx); _eo_retL(o); }
bool ell_iisTrue(ell l, uint idx) { return __eobj_isTrue(ell_i(l, idx)); }

int ell_freeH(ell l) { cptr o = ell_takeH(l ); if(o) { _n_free(_eo_dn(o)); return 1; } return 0; }
int ell_freeT(ell l) { cptr o = ell_takeT(l ); if(o) { _n_free(_eo_dn(o)); return 1; } return 0; }
int ell_freeI(ell l, uint idx){ cptr o = ell_takeI(l, idx); if(o) { _n_free(_eo_dn(o)); return 1; } return 0; }
int ell_freeO(ell l, eobj obj)
{
is0_ret(obj, 0);
Expand Down
161 changes: 98 additions & 63 deletions src/libs/etools/ell.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
///
/// Description: double link list of etools
///
/// Version: 1.2
/// Version: 1.3
/// Created: 05/04/2017 04:50:18 PM
/// Revision: none
/// Compiler: gcc
Expand All @@ -25,6 +25,12 @@
extern "C" {
#endif

/** -----------------------------------------------------
*
* ell basic
*
*/

ell ell_new();

eobj ell_newO(etypeo t, uint len); // Returns a new eobj used by elist, len will have effect only when @param type is ESTR, ERAW
Expand All @@ -42,115 +48,144 @@ int ell_freeEx(ell l, eobj_rls_cb rls);
void ell_show(ell l, uint max); // todo
void ell_showSpan(ell l, uint begin, uint end); // todo

/// -- ell push operation --
///
/// create or add a obj in front of the list
///
/// returns the new obj or added obj if succeed, otherwise return 0
///
/** -----------------------------------------------------
*
* ell add
*
* add a val or obj to ell
*
* @fn
*
* push: create or add a obj in front of the list
* appd: create or add a obj in last of the list
*
* @todo:
* add : insert an obj at the idx of list
*
* @return
*
* returns the new obj or added obj if succeed, otherwise return 0
*
*/

eobj ell_pushI(ell l, i64 val); // ENUM i64
eobj ell_pushF(ell l, f64 val); // ENUM f64
eobj ell_pushS(ell l, constr str); // ESTR
eobj ell_pushP(ell l, conptr ptr); // EPTR
eobj ell_pushR(ell l, size_t len); // ERAW
eobj ell_pushO(ell l, eobj obj); // EOBJ

/// -- ell appd operation --
///
/// create or add a obj in last of the list
///
/// returns the new obj or added obj if succeed, otherwise return 0
///
eobj ell_appdI(ell l, i64 val); // ENUM i64
eobj ell_appdF(ell l, f64 val); // ENUM f64
eobj ell_appdS(ell l, constr str); // ESTR
eobj ell_appdP(ell l, conptr ptr); // EPTR
eobj ell_appdR(ell l, size_t len); // ERAW
eobj ell_appdO(ell l, eobj obj); // EOBJ

/// -- ell insert operation --
///
/// insert an obj at the idx of list
///
/// returns the created obj if succeed, otherwise return 0
///
eobj ell_addI(ell l, int idx, i64 val); // todo
eobj ell_addF(ell l, int idx, f64 val); // todo
eobj ell_addS(ell l, int idx, constr str); // todo
eobj ell_addP(ell l, int idx, conptr ptr); // todo
eobj ell_addR(ell l, int idx, size_t len); // todo
eobj ell_addO(ell l, int idx, eobj obj); // todo

/// -- ell set operation --
///
/// reset(replace) the value of obj in @param idx of
/// the inner list:
///
/// 1. if idx > 0, we search from the beginning
/// 2. if idx < 0, we search from thr end
///
/// [] [] [] [] [] [] [] [] [] []
/// 0 1 2 3 4 5 6 7 8 9
/// -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 idx = idx + ${len}
///
/// 3. if idx can not be found, the operation will failed
/// 4. it may do realloc opearation, be careful
///
/// returns the setted obj if succeed, otherwise return 0
///
eobj ell_setI(ell l, int idx, i64 val); // ENUM i64
eobj ell_setF(ell l, int idx, f64 val); // ENUM f64
eobj ell_setP(ell l, int idx, conptr ptr); // ESTR
eobj ell_setS(ell l, int idx, constr str); // EPTR
eobj ell_setR(ell l, int idx, size_t len); // ERAW
eobj ell_setO(ell l, int idx, eobj obj); // todo

/// -- ell get operation --
///
///
///
eobj ell_at (ell l, uint idx); // Returns the eobj at pos idx, see ell_val()

eobj ell_val (ell l, uint idx); // Returns the eobj at pos idx, see ell_at()
i64 ell_valI(ell l, uint idx); // Returns the value i64 of eobj if exist and type matchs ENUM, else return 0
f64 ell_valF(ell l, uint idx); // Returns the value f64 of eobj if exist and type matchs ENUM, else return 0
cptr ell_valP(ell l, uint idx); // Returns the ptr of eobj if exist and type matchs EPTR, else return 0
cstr ell_valS(ell l, uint idx); // Returns the cstr of eobj if exist and type matchs ESTR, else return 0
cptr ell_valR(ell l, uint idx); // Returns the ptr of raw in eobj if exist and type matchs ERAW, else return 0

etypeo ell_valType(ell l, uint idx); // Returns eobj's type if exist, else return EOBJ_UNKNOWN
uint ell_valLen (ell l, uint idx); // Returns eobj's len if exist and type matchs ESTR, ERAW, EOBJ, else return 0

bool ell_valIsTrue(ell l, uint idx); // Returns true if the val in eobj is likely true:
/** -----------------------------------------------------
*
* ell set
*
* reset the value of specific obj in ell
*
* @note:
*
* 1. if idx > 0, we search from the beginning
* 2. if idx < 0, we search from thr end
*
* [ * * * * * * * * * *]
* 0 1 2 3 4 5 6 7 8 9
* -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 idx = idx + ${len}
*
* 3. if idx can not be found, the operation will failed
* else the operaion will success always
* 4. it may do realloc opearation, be careful
*
* @return
*
* returns the setted obj if succeed, otherwise return 0
*
*/
eobj ell_isetI(ell l, int idx, i64 val); // ENUM i64
eobj ell_isetF(ell l, int idx, f64 val); // ENUM f64
eobj ell_isetP(ell l, int idx, conptr ptr); // ESTR
eobj ell_isetS(ell l, int idx, constr str); // EPTR
eobj ell_isetR(ell l, int idx, size_t len); // ERAW
eobj ell_isetO(ell l, int idx, eobj obj); // todo

/** -----------------------------------------------------
*
* ell val
*
* get obj or val from ell
*
*/
eobj ell_i (ell l, uint idx); // Returns the ptr of eobj if exist
i64 ell_ivalI (ell l, uint idx); // Returns the value i64 of eobj if exist and type matchs ENUM, else return 0
f64 ell_ivalF (ell l, uint idx); // Returns the value f64 of eobj if exist and type matchs ENUM, else return 0
cptr ell_ivalP (ell l, uint idx); // Returns the ptr of eobj if exist and type matchs EPTR, else return 0
cstr ell_ivalS (ell l, uint idx); // Returns the cstr of eobj if exist and type matchs ESTR, else return 0
cptr ell_ivalR (ell l, uint idx); // Returns the ptr of raw in eobj if exist and type matchs ERAW, else return 0
etypeo ell_itype (ell l, uint idx); // Returns eobj's type if exist, else return EOBJ_UNKNOWN
uint ell_ilen (ell l, uint idx); // Returns eobj's len if exist and type matchs ESTR, ERAW, EOBJ, else return 0
bool ell_iisTrue(ell l, uint idx); // Returns true if the val in eobj is likely true:
// 1. the type of obj is ETRUE
// 2. the val of i64 or f64 is not 0
// 3. the ptr val is not 0
// 4. the str val is not empty
// 5. the len of raw is not 0


/** -----------------------------------------------------
*
* ell iterating
*
* @note
* for perfomance, we do not check type of o in
* ell_next() and ell_next()
*
*/
eobj ell_first(ell l); // Returns the first item of list if have; otherwise returns 0.
eobj ell_last (ell l); // Returns the last item of list if have; otherwise returns 0.
eobj ell_next (eobj obj); // Returns the next item of eobj if have; otherwise returns 0. For performance, we do not check the obj type, be careful.
eobj ell_prev (eobj obj); // Returns the prev item of eobj if have; otherwise returns 0. For performance, we do not check the obj type, be careful.

/// ell_foreach(ell l, eobj itr)
#define ell_foreach(l, itr) for(eobj itr = ell_first(l); (itr); itr = ell_next(itr))
#define ell_foreach_s(l, itr) for(eobj itr, __INNER__ = ell_first(l); ((itr) = __INNER__, __INNER__ = ell_next(__INNER__), (itr));)

int ell_swap(ell l, uint idx1, uint idx2); // todo

/** -----------------------------------------------------
*
* ell take
*
* take one or multi objs from ell
*
*/
eobj ell_takeH(ell l); // Takes the head obj
eobj ell_takeT(ell l); // Takes the tail obj
eobj ell_takeO(ell l, eobj obj); // Takes the obj which passed in, it will success only when the obj is in this list
eobj ell_takeAt(ell l, uint idx); // Takes the obj at the idx if exist
eobj ell_takeI(ell l, uint idx); // Takes the obj at the idx if exist

ell ell_takeAll(ell l, eobj val); // todo
eobj ell_takeOne(ell l, eobj val); // todo

ell ell_takeAllEx(ell l, eobj val, eobj_cmp_cb cmp); // todo
ell ell_takeOneEx(ell l, eobj val, eobj_cmp_cb cmp); // todo

/** -----------------------------------------------------
*
* ell free
*
* release one or multi objs in ell
*
*/
int ell_freeH(ell l); // Release the head obj
int ell_freeT(ell l); // Release the tail obj
int ell_freeO(ell l, eobj obj); // Release the obj which passed in, it will success only when the obj is in this list or the pass in list is null and the obj is independent and a ell obj
Expand Down
Loading

0 comments on commit 00e77db

Please sign in to comment.