极速快速的dinamic数组实现 [英] Extremly fast dinamic array implementation

查看:90
本文介绍了极速快速的dinamic数组实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想和你分享一些代码,如果你想要的话,还有一些评论和

改进。

这个头文件分配,添加和删除任何项目来自一个非常快的阵列的数据类型



#include< stdlib.h>


# ifndef __LIST_H__

#define __LIST_H__


#define NEW(类型)(类型*)malloc(sizeof(type))


#define DEFAULT_SIZE 12

#define lNEW(type)({\

int * list =(int *)malloc((sizeof) (类型)* DEFAULT_SIZE)\

+(sizeof(int)* 3)); \

* list ++ = sizeof(type); \

* list ++ = DEFAULT_SIZE; \

* list ++ = 0; \

(type *)list;})

#define lSIZE(list)(*((int *)list-1))

#define lALLOCATED(list)(*((int *)list-2))

#define lOBJECTSIZE(list)(*((int *)list-3))

#define lNEWITEM(list)({\

if(lSIZE(列表)> = lALLOCATED(list)){\

list =(void *)((int *)realloc((int *)list-3,\

(lSIZE(list)+ DEFAULT_SIZE)* \

lOBJECTSIZE(list)+ \

sizeof(int)* 3)\

+ 3); \

lALLOCATED(list)+ = DEFAULT_SIZE; \

} \

& list [lSIZE(list)++]; \

})


#define lAPPEND(list,obj)* lNEWITEM(list)= obj;

#define lDELITEM(list,obj)* obj = list [ - lSIZE(list)]


#define lFREE(list)free((int *)list-3)


#endif


在我的系统中,每

秒可以添加超过1.000.000的物品,所以我认为它非常快。

有趣的是,您可以使用生成的数组作为

普通C数组,使用[]访问其所有数据。

示例:


typedef struct {

int onething;

char * another;

}对象;

对象* obj = lNEW(对象);

对象o = {12," haha​​"};

Object * oo = lNEWITEM(obj);

lAPPEND(obj,o);

oo-> another =" hehe";

printf("%i,%s",lSIZE(obj),obj [1]。另一个);


=打印" 2,hehe"


我正在使用GCC。如果有人想用其他编译器查看它,我会感谢


解决方案

ja ***** @ gmail.com 写道:


I只是想与你分享一些代码,并有一些评论和

改进如果你想。

这个头文件分配和添加和删除任何类型的项目数据:


#include< stdlib.h>


#ifndef __LIST_H __

#define __LIST_H__


#define新(类型)(类型*)malloc(sizeof(类型))


#define DEFAULT_SIZE 12

#define lNEW(type)({\

int * list =(int *)malloc((sizeof(type)* DEFAULT_SIZE)\

+(sizeof(int)* 3)); \

* list ++ = sizeof(type); \

* list ++ = DEFAULT_SIZE; \

* list ++ = 0; \

(类型* )list;})


#define lSIZE(list)(*((int *)list-1))

#define lALLOCATED(list)( *((int *)list-2))

#define lOBJECTSIZE(list)(*((int *)list-3))

#define lNEWITEM(list )({\

if(lSIZE(list)> = lALLOCATED(list)){\

list =(void *)((int *)realloc ((int *)list-3,\

(lSIZE(list)+ DEFAULT_SIZE)* \

lOBJECTSIZE(list)+ \

sizeof(int)* 3)\

+ 3); \

lALLOCATED(list)+ = DEFAULT_SIZE; \

} \

& list [lSIZE(list)++]; \

})


#define lAPPEND(list,obj)* lNEWITEM(list)= obj;

#define lDELITEM(list,obj)* obj = list [ - lSIZE(list)]


#define lFREE(list)free((int *)list-3)


#endif


在我的系统中,每

秒可以添加超过1.000.000的物品,所以我认为它非常快。

有趣的是,您可以使用生成的数组作为

普通C数组,使用[]访问其所有数据。

示例:


typedef struct {

int onething;

char * another;

}对象;

对象* obj = lNEW(对象);

对象o = {12," haha​​"};

Object * oo = lNEWITEM(obj);

lAPPEND(obj,o);

oo-> another =" hehe";

printf("%i,%s",lSIZE(obj),obj [1]。另一个);


=打印" 2,hehe"


我正在使用GCC。如果有人想用其他编译器检查它,

我会感谢。



在我的系统上,Object * obj = lNEW(Object);扩展为......


对象* obj =({int list =(int)malloc((sizeof(Object)* 12)+

(sizeof (int)* 3)); * list ++ = sizeof(Object); * list ++ = 12; * list ++ = 0;

(Object *)list;});


应该从哪里开始 -

赋值中使用的带括号的复合语句?

-

==== ==========

不是学生

==============



ja*****@gmail.com 写道:


我只是想和你分享一些代码,如果你愿意的话,还有一些评论和

改进。



一些评论:

(1)

使用宏通常是个坏主意产生大量金额

的代码。只需轻轻一按,穷人就会收到大量的错误消息,显示出大量神秘破碎的代码。

如何使用一些不错的功能或方法?


(2)当

复杂代码中没有单一错误检查时,这通常是一个不好的迹象可能会出错很多。


(3)你正在努力将所有艰苦的工作转移到你的特定的
malloc和realloc()的实现上。


否则好吧




Ancient_Hacker写道:
< blockquote class =post_quotes> ja*****@gmail.com 写道:


我只是想和你分享一些代码,如果你愿意的话,还有一些评论和

改进。



一些评论:

(1)

使用宏通常是个坏主意产生大量金额

的代码。只需轻轻一按,穷人就会收到大量的错误消息,显示出大量神秘破碎的代码。

如何使用一些不错的功能或方法?



我使用宏,因为它可以轻松地进行类型转换和透明地b / b
也因为它们很快。 (是的,我可以使用内联

函数,但是仍然存在管理类型大小等问题。)


(2)它''当一个复杂的代码中没有一个错误检查可能会出错很多时,通常是一个不好的迹象。



进行错误检查是违反快速的初衷。

使用当前的计算机,能够分配4Gb或更多的内存,

在大多数情况下不需要这样的复杂情况。在其他地方,

如果使用dinamic数组的程序内存不足,系统本身

将停止应用程序,所以我不同意内存检查

在这里是必要的。


(3)你正在努力将所有艰苦的工作交给你的特定的

实现了malloc和realloc()。


否则没关系



感谢你的评价。


I just want to share some code with you, and have some comments and
improvements if you want.
This header file allocates and add and delete items of any kind of data
from a very fast array:

#include <stdlib.h>

#ifndef __LIST_H__
#define __LIST_H__

#define NEW(type) (type*)malloc(sizeof(type))

#define DEFAULT_SIZE 12

#define lNEW(type) ({ \
int *list = (int*)malloc( (sizeof(type)*DEFAULT_SIZE) \
+ (sizeof(int)*3)); \
*list++=sizeof(type); \
*list++=DEFAULT_SIZE; \
*list++=0; \
(type*)list; })

#define lSIZE(list) (*((int*)list-1))
#define lALLOCATED(list) (*((int*)list-2))
#define lOBJECTSIZE(list) (*((int*)list-3))
#define lNEWITEM(list) ({ \
if (lSIZE(list)>=lALLOCATED(list)) { \
list = (void*) ((int*)realloc((int*)list-3, \
(lSIZE(list) + DEFAULT_SIZE) * \
lOBJECTSIZE(list) + \
sizeof(int)*3) \
+ 3); \
lALLOCATED(list)+=DEFAULT_SIZE; \
} \
&list[lSIZE(list)++]; \
})

#define lAPPEND(list,obj) *lNEWITEM(list)=obj;
#define lDELITEM(list,obj) *obj=list[--lSIZE(list)]

#define lFREE(list) free((int*)list-3)

#endif

In my system, it is able to add more than 1.000.000 of items per
second, so I think it is quite fast.
And the interesting thing is that you can use the resulting array as a
normal C array, using [] to access all of its data.
Example:

typedef struct {
int onething;
char *another;
} Object;
Object *obj = lNEW(Object);
Object o = {12, "haha"};
Object *oo = lNEWITEM(obj);
lAPPEND(obj, o);
oo->another="hehe";
printf ("%i, %s", lSIZE(obj), obj[1].another);

=prints "2, hehe"

I''m using GCC. If anyone wants to check out it using other compilers, I
will thank.

解决方案

ja*****@gmail.com wrote:

I just want to share some code with you, and have some comments and
improvements if you want.
This header file allocates and add and delete items of any kind of
data from a very fast array:

#include <stdlib.h>

#ifndef __LIST_H__
#define __LIST_H__

#define NEW(type) (type*)malloc(sizeof(type))

#define DEFAULT_SIZE 12

#define lNEW(type) ({ \
int *list = (int*)malloc( (sizeof(type)*DEFAULT_SIZE) \
+ (sizeof(int)*3)); \
*list++=sizeof(type); \
*list++=DEFAULT_SIZE; \
*list++=0; \
(type*)list; })

#define lSIZE(list) (*((int*)list-1))
#define lALLOCATED(list) (*((int*)list-2))
#define lOBJECTSIZE(list) (*((int*)list-3))
#define lNEWITEM(list) ({ \
if (lSIZE(list)>=lALLOCATED(list)) { \
list = (void*) ((int*)realloc((int*)list-3, \
(lSIZE(list) + DEFAULT_SIZE) * \
lOBJECTSIZE(list) + \
sizeof(int)*3) \
+ 3); \
lALLOCATED(list)+=DEFAULT_SIZE; \
} \
&list[lSIZE(list)++]; \
})

#define lAPPEND(list,obj) *lNEWITEM(list)=obj;
#define lDELITEM(list,obj) *obj=list[--lSIZE(list)]

#define lFREE(list) free((int*)list-3)

#endif

In my system, it is able to add more than 1.000.000 of items per
second, so I think it is quite fast.
And the interesting thing is that you can use the resulting array as a
normal C array, using [] to access all of its data.
Example:

typedef struct {
int onething;
char *another;
} Object;
Object *obj = lNEW(Object);
Object o = {12, "haha"};
Object *oo = lNEWITEM(obj);
lAPPEND(obj, o);
oo->another="hehe";
printf ("%i, %s", lSIZE(obj), obj[1].another);

=prints "2, hehe"

I''m using GCC. If anyone wants to check out it using other compilers,
I will thank.


On my system, Object *obj = lNEW(Object); expands to ...

Object *obj = ({ int list = (int)malloc( (sizeof(Object)*12) +
(sizeof(int)*3)); *list++=sizeof(Object); *list++=12; *list++=0;
(Object*)list; });

Where should one start - the parenthesied compound statement used in the
assignment perhaps?
--
==============
Not a pedant
==============



ja*****@gmail.com wrote:

I just want to share some code with you, and have some comments and
improvements if you want.

Some comments:
(1)
It''s usually a poor idea to use macros to generate substantial amounts
of code. Just one slip of the finger and the poor user will get tons
of error messages, showing tons of lines of mysterious fractured code.
How about you use some nice functions or methods?

(2) It''s usually a bad sign when there''s not a single error check in a
piece of complex code that can go wrong so many ways.

(3) You''re schlepping off all the hard work to your particular
implementation of malloc and realloc().

otherwise okay



Ancient_Hacker wrote:

ja*****@gmail.com wrote:

I just want to share some code with you, and have some comments and
improvements if you want.


Some comments:
(1)
It''s usually a poor idea to use macros to generate substantial amounts
of code. Just one slip of the finger and the poor user will get tons
of error messages, showing tons of lines of mysterious fractured code.
How about you use some nice functions or methods?

I use macros because it is posible to typecast easily and
transparently, and also because they are fast. (Yes, I could use inline
functions, but still have problems managin type sizes and more.)

(2) It''s usually a bad sign when there''s not a single error check in a
piece of complex code that can go wrong so many ways.

Having error check is against the original intention of being fast.
With current computers, being able to allocate 4Gb or more of memory,
there is no need for such comprobations in most situations. Elsewhere,
if a program using dinamic arrays gets out of memory, the system itself
will halt down de application, so I don''t agree that a memory check
would be necesary here.

(3) You''re schlepping off all the hard work to your particular
implementation of malloc and realloc().

otherwise okay

Thanks for your coments.


这篇关于极速快速的dinamic数组实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆