Using C language to realize vector dynamic array instance sharing _c language

Source: Internet
Author: User

The following is the implementation of the project when a dynamic array, successively added several after the project, under the sun below the code.

Header file:

# ifndef __cvector_h__ # define __CVECTOR_H__ # define Min_len 256 # define CVEFAILED-1 # define cvesuccess 0 # def INE Cvepushback 1 # define Cvepopback 2 # define Cveinsert 3 # define CVERM 4 # define Expaned_val 1 # define RED 
Used_val 2 typedef void *citerator; 
 
typedef struct _CVECTOR *cvector; # ifdef _cplusplus # define extern_ extern ' C ' # else # define extern_ extern # endif Extern_ cvector Cvector_crea 
TE (const size_t size); 
extern_ void Cvector_destroy (const cvector CV); 
Extern_ size_t cvector_length (const cvector CV); 
Extern_ int Cvector_pushback (const cvector CV, void *memb); 
Extern_ int Cvector_popback (const cvector CV, void *memb); 
Extern_ size_t cvector_iter_at (const cvector CV, citerator ITER); 
Extern_ int Cvector_iter_val (const cvector CV, Citerator iter, void *memb); 
Extern_ citerator cvector_begin (const cvector CV); Extern_ Citerator cvector_end (const cvector CV); 
Extern_ citerator cvector_next (const cvector CV, citerator ITER); 
Extern_ int cvector_val_at (const cvector CV, size_t index, void *memb); 
Extern_ int Cvector_insert (const cvector CV, Citerator iter, void *memb); 
Extern_ int cvector_insert_at (const cvector CV, size_t index, void *memb); 
Extern_ int cvector_rm (const cvector CV, citerator ITER);  
 
Extern_ int cvector_rm_at (const cvector CV, size_t index); 
/* for Test */extern_ void Cv_info (const cvector CV); 
extern_ void Cv_print (const cvector CV); 

 #endif/* EOF file Cvector.h * *


C File:

# include <stdio.h> # include <stdlib.h> # include <string.h> # include <unistd.h> # define M In_len 256 # define CVEFAILED-1 # define Cvesuccess 0 # define Cvepushback 1 # define Cvepopback 2 # define Cveinser 
T 3 # define CVERM 4 # define Expaned_val 1 # define Redused_val 2 typedef void *citerator; 
  typedef struct _cvector {void *cv_pdata; 
size_t Cv_len, Cv_tot_len, cv_size; 
 
} *cvector; # define Cwarning_iter (CV, ITER, file, func, line) \ Do {\ if (Cvector_begin (CV) > ITER) | | (Cvector_end (CV) <= iter)) {\ fprintf (stderr, var ("#iter") warng out of range, "\" at file:%s func:%s line:%d!! /n ", file, Func, line); \ Return cvefailed;\} \} while (0) # ifdef _cplusplus # define extern_ extern ' C ' # else # define Extern_ EXTERN # endif Extern_ C 
Vector cvector_create (const size_t size); 
extern_ void Cvector_destroy (const cvector CV); ExteRn_ size_t cvector_length (const cvector CV); 
Extern_ int Cvector_pushback (const cvector CV, void *memb); 
Extern_ int Cvector_popback (const cvector CV, void *memb); 
Extern_ size_t cvector_iter_at (const cvector CV, citerator ITER); 
Extern_ int Cvector_iter_val (const cvector CV, Citerator iter, void *memb); 
Extern_ citerator cvector_begin (const cvector CV); 
Extern_ citerator cvector_end (const cvector CV); 
Extern_ citerator cvector_next (const cvector CV, citerator ITER); 
Extern_ int cvector_val_at (const cvector CV, size_t index, void *memb); 
Extern_ int Cvector_insert (const cvector CV, Citerator iter, void *memb); 
Extern_ int cvector_insert_at (const cvector CV, size_t index, void *memb); 
Extern_ int cvector_rm (const cvector CV, citerator ITER); 
 
Extern_ int cvector_rm_at (const cvector CV, size_t index);  /* for Test */extern_ void Cv_info   (const cvector CV); 
 
extern_ void Cv_print (const cvector CV); 
 
  Cvector cvector_create (const size_t size) {cvector CV = (cvector) malloc (sizeof (struct _cvector)); 
 
  if (!CV) return NULL; 
 
  Cv->cv_pdata = malloc (Min_len * size); 
    if (!cv->cv_pdata) {free (CV); 
  return NULL; 
  } cv->cv_size = size; 
  Cv->cv_tot_len = Min_len; 
 
  Cv->cv_len = 0; 
return CV; 
  } void Cvector_destroy (const cvector CV) {free (cv->cv_pdata); 
  Free (CV); 
Return 
} size_t cvector_length (const cvector CV) {return cv->cv_len; int Cvector_pushback (const cvector CV, void *memb) {if (Cv->cv_len >= cv->cv_tot_len) {void * 
    Pd_sav = cv->cv_pdata; 
    Cv->cv_tot_len <<= Expaned_val; 
 
    Cv->cv_pdata = ReAlloc (Cv->cv_pdata, Cv->cv_tot_len * cv->cv_size); 
      if (!cv->cv_pdata) {cv->cv_pdata = Pd_sav; Cv->cv_tot_len "; >= Expaned_val; 
    return cvepushback; 
  } memcpy (Cv->cv_pdata + cv->cv_len * cv->cv_size, MEMB, cv->cv_size); 
 
  cv->cv_len++; 
return cvesuccess; 
 
  int Cvector_popback (const cvector CV, void *memb) {if (cv->cv_len <= 0) return cvepopback; 
  cv->cv_len--; 
 
  memcpy (memb, Cv->cv_pdata + Cv->cv_len * cv->cv_size, cv->cv_size); if (Cv->cv_tot_len >= (Min_len << redused_val)) && Cv->cv_len <= (Cv->cv_tot_len &G 
    T;> redused_val)) {void *pd_sav = cv->cv_pdata; 
    Cv->cv_tot_len >>= Expaned_val; 
 
    Cv->cv_pdata = ReAlloc (Cv->cv_pdata, Cv->cv_tot_len * cv->cv_size); 
      if (!cv->cv_pdata) {Cv->cv_tot_len <<= expaned_val; 
      Cv->cv_pdata = Pd_sav; 
    return cvepopback; 
} return cvesuccess; } size_t cvector_iter_at (const cvector CV, Citerator iter) {cwarning_iter (CV, ITER, __file__, __func__, __line__); 
Return (iter-cv->cv_pdata)/cv->cv_size; int Cvector_iter_val (const cvector CV, Citerator iter, void *memb) {cwarning_iter (CV, ITER, __file__, __func__, 
  __LINE__); 
  memcpy (Memb, ITER, cv->cv_size); 
return 0; 
} citerator cvector_begin (const cvector CV) {return cv->cv_pdata; 
} citerator cvector_end (const cvector CV) {return cv->cv_pdata + (cv->cv_size * cv->cv_len); 
  } static inline void Cvmemove_foreward (const cvector CV, void *from, void *to) {size_t size = cv->cv_size; 
  void *p; 
  for (p = to; P >= from; p-= size) memcpy (p + size, p, size); 
Return } static inline void Cvmemove_backward (const cvector CV, void *from, void *to) {memcpy (from, from + cv->cv_size 
  , To-from); 
Return int Cvector_insert (const cvector CV, Citerator iter, void *memb) {cwarning_iter (CV, ITER, __file__, __func__, __ 
 
  LINE__); if (Cv->cv_len >= cv->cv_tot_len) {
    void *pd_sav = cv->cv_pdata; 
    Cv->cv_tot_len <<= Expaned_val; 
 
    Cv->cv_pdata = ReAlloc (Cv->cv_pdata, Cv->cv_tot_len * cv->cv_size); 
      if (!cv->cv_pdata) {cv->cv_pdata = Pd_sav; 
      Cv->cv_tot_len >>= Expaned_val; 
    return cveinsert; 
  } cvmemove_foreward (CV, ITER, Cv->cv_pdata + Cv->cv_len * cv->cv_size); 
  memcpy (ITER, MEMB, cv->cv_size); 
 
  cv->cv_len++; 
return cvesuccess; 
 
  int Cvector_insert_at (const cvector CV, size_t index, void *memb) {citerator iter; 
    if (index >= cv->cv_tot_len) {Cv->cv_len = index + 1; 
    while (Cv->cv_len >= cv->cv_tot_len) Cv->cv_tot_len <<= expaned_val; 
    Cv->cv_pdata = ReAlloc (Cv->cv_pdata, Cv->cv_tot_len * cv->cv_size); 
    ITER = cv->cv_pdata + cv->cv_size * INDEX; 
  memcpy (ITER, MEMB, cv->cv_size); else {iter = Cv->cv_pdata + cv->cv_size *Index 
  Cvector_insert (CV, ITER, MEMB); 
return 0; 
} citerator cvector_next (const cvector CV, Citerator iter) {return iter + cv->cv_size; 
  int Cvector_val (const cvector CV, Citerator iter, void *memb) {memcpy (memb, ITER, cv->cv_size); 
return 0; int Cvector_val_at (const cvector CV, size_t index, void *memb) {memcpy (memb, Cv->cv_pdata + index * CV-&GT;CV 
  _size, cv->cv_size); 
return 0; 
  } int cvector_rm (const cvector CV, Citerator iter) {citerator from; 
  Citerator end; 
  Cwarning_iter (CV, ITER, __file__, __func__, __line__); 
  from = iter; 
  End = Cvector_end (CV); 
  memcpy (from, from + cv->cv_size, end-from); 
 
  cv->cv_len--; if (Cv->cv_tot_len >= (Min_len << redused_val)) && (Cv->cv_len <= (Cv->cv_tot_len) 
    ;> redused_val)) {void *pd_sav = cv->cv_pdata; 
    Cv->cv_tot_len >>= Expaned_val; Cv->cv_pdata = ReAlloc (Cv->cv_pdata, CV->cv_tot_len * cv->cv_size); 
      if (!cv->cv_pdata) {Cv->cv_tot_len <<= expaned_val; 
      Cv->cv_pdata = Pd_sav; 
    return cverm; 
} return cvesuccess; 
  int Cvector_rm_at (const cvector CV, size_t index) {citerator iter; 
  ITER = cv->cv_pdata + cv->cv_size * INDEX; 
  Cwarning_iter (CV, ITER, __file__, __func__, __line__); 
Return CVECTOR_RM (CV, ITER); 
  } void Cv_info (const cvector CV) {printf ("/n/ntot:%s:%d/n", __func__, Cv->cv_tot_len); 
  printf ("Len:%s:%d/n", __func__, Cv->cv_len); 
  printf ("size:%s:%d/n/n", __func__, cv->cv_size); 
Return 
  } void Cv_print (const cvector CV) {int num; 
 
  Citerator ITER; if (cvector_length (CV) = = 0) fprintf (stderr, "file:%s func:%s line:%d error, NULL length cvector!! 
 
  /n ", __file__, __func__, __line__);  
      for (iter = Cvector_begin (CV); 
      ITER!= Cvector_end (CV); ITER = Cvector_next (CV, ITER)) {Cvector_iTer_val (CV, ITER, &num); 
  printf ("var:%d at:%d/n", NUM, Cvector_iter_at (CV, ITER)); 
} return; 

 }

Improved version
The above code is written in Linux, if it is in the Windows Visul C + + environment, the compiler seems to have some problems, so specifically made an improved version:
Here is the updated code:

Cvector.h

# ifndef __cvector_h__ # define __CVECTOR_H__ # include <stdio.h> # include <stdlib.h> # include < String.h> # define Min_len 256 # define CVEFAILED-1 # define Cvesuccess 0 # define Cvepushback 1 # define CVEP Opback 2 # define Cveinsert 3 # define CVERM 4 # define Expaned_val 1 # define Redused_val 2 typedef void *cite 
Rator; 
 
typedef struct _CVECTOR *cvector; 
  # ifdef __cplusplus extern "C" {# endif Cvector cvector_create (const size_t size); 
  void Cvector_destroy (const cvector CV); 
  size_t cvector_length (const cvector CV); 
  int Cvector_pushback (const cvector CV, void *memb); 
  int Cvector_popback (const cvector CV, void *memb); 
  size_t cvector_iter_at (const cvector CV, citerator ITER); 
  int Cvector_iter_val (const cvector CV, Citerator iter, void *memb); 
  Citerator cvector_begin (const cvector CV); Citerator Cvector_End (const cvector CV); 
  Citerator cvector_next (const cvector CV, citerator ITER); 
  int Cvector_val_at (const cvector CV, size_t index, void *memb); 
  int Cvector_insert (const cvector CV, Citerator iter, void *memb); 
  int Cvector_insert_at (const cvector CV, size_t index, void *memb); 
  int CVECTOR_RM (const cvector CV, citerator ITER); 
 
  int Cvector_rm_at (const cvector CV, size_t index); 
  /* for Test */void Cv_info (const cvector CV); 
 
void Cv_print (const cvector CV); 

 # ifdef __cplusplus} # endif #endif/* EOF file Cvector.h * *

Cvector.c

#include "cvector.h" #ifndef __gnu_linux__ #define __FUNC__ "Unknown" #define Inline __forceinline #endif # Defi Ne cwarning_iter (CV, ITER, file, func, line) \ Do {\ if (Cvector_begin (CV) > ITER) | | (Cvector_end (CV) <= iter)) {\ fprintf (stderr, var ("#iter") warng out of range, "\" at file:%s func:%s line:%d!! \ n ", file, Func, line); \ Return cvefailed;\} 
  \} while (0) struct _cvector {void *cv_pdata; 
size_t Cv_len, Cv_tot_len, cv_size; 
 
}; 
 
  Cvector cvector_create (const size_t size) {cvector CV = (cvector) malloc (sizeof (struct _cvector)); 
 
  if (!CV) return NULL; 
 
  Cv->cv_pdata = malloc (Min_len * size); 
    if (!cv->cv_pdata) {free (CV); 
  return NULL; 
  } cv->cv_size = size; 
  Cv->cv_tot_len = Min_len; 
 
  Cv->cv_len = 0; 
return CV;  
  } void Cvector_destroy (const cvector CV) {free (cv->cv_pdata);  
  Free (CV);  
Return  
 } size_t cvector_length (const cvector CV) { Return cv->cv_len; int Cvector_pushback (const cvector CV, void *memb) {if (Cv->cv_len >= cv->cv_tot_len) {V  
    OID *pd_sav = cv->cv_pdata;  
    Cv->cv_tot_len <<= Expaned_val;  
 
    Cv->cv_pdata = ReAlloc (Cv->cv_pdata, Cv->cv_tot_len * cv->cv_size);  
      if (!cv->cv_pdata) {cv->cv_pdata = Pd_sav;  
      Cv->cv_tot_len >>= Expaned_val;  
    return cvepushback;  
  } memcpy ((char *) Cv->cv_pdata + Cv->cv_len * cv->cv_size, MEMB, cv->cv_size);  
 
  cv->cv_len++;  
return cvesuccess;  
 
  int Cvector_popback (const cvector CV, void *memb) {if (cv->cv_len <= 0) return cvepopback;  
  cv->cv_len--;  
 
  memcpy (MEMB, (char *) Cv->cv_pdata + Cv->cv_len * cv->cv_size, cv->cv_size); if (Cv->cv_tot_len >= (Min_len << redused_val)) && (Cv->cv_len <= (Cv->cv_tot_len)  
;> redused_val))) {    void *pd_sav = cv->cv_pdata;  
    Cv->cv_tot_len >>= Expaned_val;  
 
    Cv->cv_pdata = ReAlloc (Cv->cv_pdata, Cv->cv_tot_len * cv->cv_size);  
      if (!cv->cv_pdata) {Cv->cv_tot_len <<= expaned_val;  
      Cv->cv_pdata = Pd_sav;  
    return cvepopback;  
} return cvesuccess; } size_t cvector_iter_at (const cvector CV, Citerator iter) {cwarning_iter (CV, ITER, __file__, __func__, __line_  
  _);  
Return ((char *) ITER-(char *) cv->cv_pdata)/cv->cv_size; int Cvector_iter_val (const cvector CV, Citerator iter, void *memb) {cwarning_iter (CV, ITER, __file__, __func_  
  _, __line__);  
  memcpy (Memb, ITER, cv->cv_size);  
return 0;  
} citerator cvector_begin (const cvector CV) {return cv->cv_pdata;  
} citerator cvector_end (const cvector CV) {return (char *) Cv->cv_pdata + (cv->cv_size * cv->cv_len); } static inline void Cvmemove_foreward (conSt Cvector CV, void *from, void *to) {size_t size = cv->cv_size;  
  Char *p;  
  for (p = (char *) to; P->= (char *) from P-= size) memcpy (p + size, p, size);  
Return } static inline void Cvmemove_backward (const cvector CV, void *from, void *to) {memcpy (from, (char *) from + cv-  
  >cv_size, (char *) to-(char *) from);  
Return  int Cvector_insert (const cvector CV, Citerator iter, void *memb) {cwarning_iter (CV, ITER, __file__, __func__,  
 
  __LINE__);  
    if (Cv->cv_len >= cv->cv_tot_len) {void *pd_sav = cv->cv_pdata;  
    Cv->cv_tot_len <<= Expaned_val;  
 
    Cv->cv_pdata = ReAlloc (Cv->cv_pdata, Cv->cv_tot_len * cv->cv_size);  
      if (!cv->cv_pdata) {cv->cv_pdata = Pd_sav;  
      Cv->cv_tot_len >>= Expaned_val;  
    return cveinsert;  
  } cvmemove_foreward (CV, ITER, (char *) Cv->cv_pdata + Cv->cv_len * cv->cv_size); memcpy (ITER, memb, Cv->cv_size);  
 
  cv->cv_len++;  
return cvesuccess;  
 
  int Cvector_insert_at (const cvector CV, size_t index, void *memb) {citerator iter;  
    if (index >= cv->cv_tot_len) {Cv->cv_len = index + 1;  
    while (Cv->cv_len >= cv->cv_tot_len) Cv->cv_tot_len <<= expaned_val;  
    Cv->cv_pdata = ReAlloc (Cv->cv_pdata, Cv->cv_tot_len * cv->cv_size);  
    ITER = (char *) Cv->cv_pdata + cv->cv_size * INDEX;  
  memcpy (ITER, MEMB, cv->cv_size);  
    else {iter = (char *) Cv->cv_pdata + cv->cv_size * INDEX;  
  Cvector_insert (CV, ITER, MEMB);  
return 0;  
} citerator cvector_next (const cvector CV, Citerator iter) {return (char *) ITER + cv->cv_size;  
  int Cvector_val (const cvector CV, Citerator iter, void *memb) {memcpy (memb, ITER, cv->cv_size);  
return 0; int Cvector_val_at (const cvector CV, size_t index, void *memb) {memcpy (MEMB, (char *) Cv->cv_pdata + index * cv->cv_size, cv->cv_size);  
return 0;  
  } int cvector_rm (const cvector CV, Citerator iter) {citerator from;  
  Citerator end;  
  Cwarning_iter (CV, ITER, __file__, __func__, __line__);  
  from = iter;  
  End = Cvector_end (CV);  
  memcpy (from, (char *) to + cv->cv_size, (char *) End-(char *) from);  
 
  cv->cv_len--; if (Cv->cv_tot_len >= (Min_len << redused_val)) && (Cv->cv_len <= (Cv->cv_tot_len >  
    > Redused_val)) {void *pd_sav = cv->cv_pdata;  
    Cv->cv_tot_len >>= Expaned_val;  
 
    Cv->cv_pdata = ReAlloc (Cv->cv_pdata, Cv->cv_tot_len * cv->cv_size);  
      if (!cv->cv_pdata) {Cv->cv_tot_len <<= expaned_val;  
      Cv->cv_pdata = Pd_sav;  
    return cverm;  
} return cvesuccess;  
  int Cvector_rm_at (const cvector CV, size_t index) {citerator iter; ITER = (char *) cv->cv_pdata + cv->cv_size * INDEX;  
  Cwarning_iter (CV, ITER, __file__, __func__, __line__);  
Return CVECTOR_RM (CV, ITER);  
  } void Cv_info (const cvector CV) {printf ("\n\ntot:%s:%d\n", __func__, Cv->cv_tot_len);  
  printf ("Len:%s:%d\n", __func__, Cv->cv_len);  
  printf ("size:%s:%d\n\n", __func__, cv->cv_size);  
Return  
  } void Cv_print (const cvector CV) {int num;  
 
  Citerator ITER; if (cvector_length (CV) = = 0) fprintf (stderr, "file:%s func:%s line:%d error, NULL length cvector!!  
 
  \ n ", __file__, __func__, __line__);   
    for (iter = Cvector_begin (CV);  
    ITER!= Cvector_end (CV);  
    ITER = Cvector_next (CV, ITER)) {Cvector_iter_val (CV, ITER, &num);  
  printf ("var:%d at:%d\n", NUM, Cvector_iter_at (CV, ITER));  
} return;  

 }

Main.cpp

#include "cvector.h" 
 
int main () 
{ 
  int i = 1; 
  Cvector CV = cvector_create (sizeof (int)); 
  Cvector_pushback (CV, &i); 
  Cvector_pushback (CV, &i); 
  Cvector_pushback (CV, &i); 
  Cvector_pushback (CV, &i); 
  Cv_print (CV); 
  Cvector_destroy (CV); 
  return 0; 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.