The C language implements vector (dynamic array). __c language

Source: Internet
Author: User

The following is a 2 years ago when the project implementation of a dynamic array, successively joined a number of subsequent projects, under the sun below the code.


Header file:

# ifndef __cvector_h__ # define __CVECTOR_H__ # define Min_len 256 # define CVEFAILED-1 # define Cvesuccess 0 # define Cvepushback 1 # define Cvepopback 2 # define Cveinsert 3 # define CVERM 4 # define Expaned_val 1 # define Redused
_val 2 typedef void *citerator;

typedef struct _CVECTOR *cvector;   # ifdef _cplusplus # define extern_ extern ' C ' # else # define extern_ extern # endif Extern_ cvector 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);
 #endif/* EOF file Cvector.h * *



C File:

# include <stdio.h> # include <stdlib.h> # include <string.h> # include <unistd.h> # define Min_le  N 256 # define CVEFAILED-1 # define Cvesuccess 0 # define Cvepushback 1 # define Cvepopback 2 # define Cveinsert 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_ cvector C
Vector_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-&
		Gt;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 &GT;&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__, __lin
	E__);
	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->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 >&gt ;
		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 cvectorCV, 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;
 }




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.