⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yc_dymemarr.h

📁 一个类STL的多平台可移植的算法容器库,主要用于嵌入式系统编程时的内存管理等方面
💻 H
字号:
/*
 * The young Library
 * Copyright (c) 2005 by Yang Huan(杨桓)

 * Permission to use, copy, modify, distribute and sell this software for any
 * purpose is hereby granted without fee, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and this
 * permission notice appear in supporting documentation.
 * The author make no representations about the suitability of this software
 * for any purpose. It is provided "as is" without express or implied warranty.
 */

/******************************************************************************/
/******************************************************************************/
#ifndef __MACRO_C_YOUNG_LIBRARY_DYNAMIC_MEMORY_ARRAY_HEADER_FILE__
#define __MACRO_C_YOUNG_LIBRARY_DYNAMIC_MEMORY_ARRAY_HEADER_FILE__
/******************************************************************************/
#include "yc_definition.h"

#ifdef __cplusplus
    namespace youngc {  extern "C" {
#endif
/******************************************************************************/
/******************************************************************************/

typedef  struct dynamic_memory_array_object
{
    void*              m_start;
    void*              m_finish;
    void*              m_utmost;
    size_t             m_element_size;
    ylib_fp_alloc_t    m_alloc;   /* safe function */
    ylib_fp_dealloc_t  m_dealloc; /* safe function */
}  dymemarray;

/******************************************************************************/

#define DYMEMARR_BEGIN( ARRAY, TYPE )  ( (TYPE*)((ARRAY).m_start) )

#define DYMEMARR_END( ARRAY, TYPE )    ( (TYPE*)((ARRAY).m_finish) )

#define DYMEMARR_FRONT( ARRAY, TYPE )  ( *((TYPE*)((ARRAY).m_start)) )

#define DYMEMARR_BACK( ARRAY, TYPE )   ( *((TYPE*)((ARRAY).m_finish) - 1) )

#define DYMEMARR_INDEX( ARRAY, INDEX, TYPE ) \
        ( ((TYPE*)((ARRAY).m_start))[INDEX] )

#define DYMEMARR_SIZE( ARRAY, TYPE ) \
        ( ((TYPE*)((ARRAY).m_finish)) - ((TYPE*)((ARRAY).m_start)) )

#define DYMEMARR_CAPACITY( ARRAY, TYPE ) \
        ( ((TYPE*)((ARRAY).m_utmost)) - ((TYPE*)((ARRAY).m_start)) )

#define DYMEMARR_SPACE( ARRAY, TYPE ) \
        ( ((TYPE*)((ARRAY).m_utmost)) - ((TYPE*)((ARRAY).m_finish)) )

#define DYMEMARR_TO_STRING( ARRAY, END_OF_STR, TYPE )                         \
        if( (ARRAY).m_start && (ARRAY).m_element_size <= CHARACTER_SIZE_MAX ) \
            *( (TYPE*)((ARRAY).m_finish) ) = (END_OF_STR)

#define DYMEMARR_POP_BACK( ARRAY, TYPE )         \
        if( (ARRAY).m_finish > (ARRAY).m_start ) \
            (ARRAY).m_finish = (TYPE*)((ARRAY).m_finish) - 1

#define DYMEMARR_PUSH_BACK( ARRAY, VAL, TYPE, BOOLVAL )                    \
        if( (TYPE*)((ARRAY).m_finish) + 1 <= (TYPE*)((ARRAY).m_utmost) ) { \
            *( (TYPE*)((ARRAY).m_finish) ) = (VAL);                        \
            (ARRAY).m_finish = (TYPE*)((ARRAY).m_finish) + 1;              \
            (BOOLVAL) = true; }                                            \
        else                                                               \
            (BOOLVAL) = dymemarr_insert_value( &(ARRAY),                   \
            (TYPE*)((ARRAY).m_finish) - (TYPE*)((ARRAY).m_start), &(VAL), 1 )

/******************************************************************************/

void dymemarr_init( dymemarray* uninit_self,
                    size_t element_size,
                    ylib_fp_alloc_t alloc,
                    ylib_fp_dealloc_t dealloc );

void dymemarr_reinit( dymemarray* self,
                      size_t element_size );

void dymemarr_destroy( dymemarray* self );

/* (1) < 0: uninit_self = src;  (2) = 0: fail;  (3) > 0: succeed */
int dymemarr_init_copy( dymemarray* uninit_self,
                        const dymemarray* src );

/* (1) < 0: self = src or can't matching;  (2) = 0: fail;  (3) > 0: succeed */
int dymemarr_assign_copy( dymemarray* self,
                          const dymemarray* src );

void dymemarr_init_move( dymemarray* uninit_self,
                         dymemarray* src );

void dymemarr_assign_move( dymemarray* self,
                           dymemarray* src );

/* converts the contents of a string as a C-style, null-terminated string */
void* dymemarr_to_string( dymemarray* self,
                          const void* end_of_str );

/* reserves new_capacity minimum length of storage for a dymemarray object,
 * if succeeded returns true, otherwise returns false */
bool dymemarr_reserve( dymemarray* self,
                       size_t new_capacity );

/* removes the element of the dymemarray */
void dymemarr_erase_pos( dymemarray* self,
                         size_t index );

/* removes the elements of the dymemarray in the range [first_index, last_index) */
void dymemarr_erase_range( dymemarray* self,
                           size_t first_index,
                           size_t last_index );

/* inserts a repetition of count elements of value before index,
 * if succeeded returns true, otherwise returns false */
bool dymemarr_insert_value( dymemarray* self,
                            size_t index,
                            const void* value,
                            size_t count );

/* inserts count elements of src before index, if succeeded returns true,
 * otherwise returns false */
bool dymemarr_insert_array( dymemarray* self,
                            size_t index,
                            const void* src,
                            size_t count );

/* replaces a repetition of new_count elements of value from index,
 * if succeeded returns true, otherwise returns false */
bool dymemarr_replace_fill( dymemarray* self,
                            size_t index,
                            size_t old_count,
                            const void* value,
                            size_t new_count );

/* replaces new_count elements of src from index, if succeeded returns true,
 * otherwise returns false */
bool dymemarr_replace_array( dymemarray* self,
                             size_t index,
                             size_t old_count,
                             const void* src,
                             size_t new_count );

/* inserts a copy of value to the end of self, if succeeded returns true,
 * otherwise returns false */
bool dymemarr_push_back( dymemarray* self,
                         const void* value );

/* alters the size of self; if the new_length is greater than the current size,
 * then new_length - dymemarr_size() instances of the default value are
 * inserted at the end of the dymemarray; if the new size is smaller than the
 * current capacity, then the dymemarray is truncated by erasing
 * dymemarr_size() - new_length elements off the end; if new_length is equal
 * to dymemarr_size() then no action is taken.
 * If succeeded returns true, otherwise returns false */
bool dymemarr_resize( dymemarray* self,
                      size_t new_length,
                      const void* value );

#ifndef __MACRO_C_YOUNG_LIBRARY_COMPILER_SUPPORT_INLINE_FUNCTION__
    bool dymemarr_empty( dymemarray* self );

    size_t dymemarr_max_size( dymemarray* self );

    size_t dymemarr_size( dymemarray* self );

    size_t dymemarr_capacity( dymemarray* self );

    size_t dymemarr_space( dymemarray* self );

    void* dymemarr_begin( dymemarray* self );

    void* dymemarr_end( dymemarray* self );

    void* dymemarr_front( dymemarray* self );

    void* dymemarr_back( dymemarray* self );

    void* dymemarr_index( dymemarray* self,
                          size_t index );

    /* if index < dymemarr_size() returns dymemarr_index(),
     * otherwise returns NULL */
    void* dymemarr_at( dymemarray* self,
                       size_t index );

    void dymemarr_pop_back( dymemarray* self );
#else
    inline bool dymemarr_empty( dymemarray* self )
    {
        return self->m_start == self->m_finish ? true : false;
    }

    inline size_t dymemarr_max_size( dymemarray* self )
    {
        size_t max = SIZE_MAX / self->m_element_size;
        return self->m_element_size > CHARACTER_SIZE_MAX ? max : --max;
    }

    inline size_t dymemarr_size( dymemarray* self )
    {
        return ((ylib_byte_t*)(self->m_finish) - (ylib_byte_t*)(self->m_start))
               / self->m_element_size;
    }

    inline size_t dymemarr_capacity( dymemarray* self )
    {
        return ((ylib_byte_t*)(self->m_utmost) - (ylib_byte_t*)(self->m_start))
               / self->m_element_size;
    }

    inline size_t dymemarr_space( dymemarray* self )
    {
        return ((ylib_byte_t*)(self->m_utmost) - (ylib_byte_t*)(self->m_finish))
               / self->m_element_size;
    }

    inline void* dymemarr_begin( dymemarray* self )
    {
        return self->m_start;
    }

    inline void* dymemarr_end( dymemarray* self )
    {
        return self->m_finish;
    }

    inline void* dymemarr_front( dymemarray* self )
    {
        return self->m_start;
    }

    inline void* dymemarr_back( dymemarray* self )
    {
        return (ylib_byte_t*)(self->m_finish) - self->m_element_size;
    }

    inline void* dymemarr_index( dymemarray* self, size_t index )
    {
        return (ylib_byte_t*)(self->m_start) + index * self->m_element_size;
    }

    inline void* dymemarr_at( dymemarray* self, size_t index )
    {
        void* pos = (ylib_byte_t*)(self->m_start)
                    + index * self->m_element_size;
        return pos < self->m_finish ? pos : NULL;
    }

    inline void dymemarr_pop_back( dymemarray* self )
    {
        if( self->m_finish > self->m_start )
            self->m_finish = (ylib_byte_t*)(self->m_finish)
                             - self->m_element_size;
    }
#endif

/******************************************************************************/
/******************************************************************************/
#ifdef __cplusplus
    }  }
#endif
#endif
/******************************************************************************/
/******************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -