PHP HashTable是什么?-PHP问题

资源魔 32 0

PHP HashTable是甚么?

PHP HashTable是指散列表,是依据要害码值而间接进行拜访的数据构造,也就是它能够经过把要害码值映照到表中一个地位来拜访记载,从而能够放慢查找的速率,此中寄存记载的数组就是散列表。

新版本的HashTable

与老版本的hashtable相比改动仍是挺年夜的

  • 老版本的元素存储是扩散的,Bucket **arBuckets 外面存储的是指针 指向bucket的地点,新版的的元素存储是延续的 Bucket *arData;

  • 老版本bucket中有4个指针 新版版中的bucket中只有一个指针,而且只有正在hash碰撞的时分才会用到

  • 少了三个指针,看下新版本的hashtable 若何做好依照拔出程序遍历息争决hash抵触

看下hashtable的界说

   typedef struct _zend_array HashTable;
   
	struct _zend_array {
		zend_refcounted_h gc;        //  gc 相干
		union {                           //  联结体 
			struct { 
				ZEND_ENDIAN_LOHI_4(
					zend_uchar    flags,
					zend_uchar    nApplyCount,
					zend_uchar    nIteratorsCount,
					zend_uchar    consistency)
			} v;
			uint32_t flags;
		} u;
		uint32_t          nTableMask;          //  hash表的掩码 用来确定hsh
		Bucket           *arData;                    // bucket数组
		uint32_t         *arHash;              // hashtable 查找  巨细为nTableMask 寄存指向bucket的指针(疑难正在源码界说中未看到)
		uint32_t          nNumUsed;            //  元素个数 蕴含已删除了的元素
		uint32_t          nNumOfElements;    //  无效的元素个数
		uint32_t          nTableSize;      //  hash表的巨细
		uint32_t          nInternalPointer;     
		zend_long         nNextFreeElement;
		dtor_func_t       pDestructor;
	};

bucket的界说

typedef struct _Bucket {
	zval              val;           
	zend_ulong        h;         //存的hash 值 用来寻觅比照key
	zend_string      *key;           // 假如key是string 则寄存key 假如是数字 则为空
} Bucket;

typedef struct _zval_struct     zval;
struct _zval_struct {
	zend_value        value;			// value 真实的构造
	union {
		struct {
			ZEND_ENDIAN_LOHI_4(
				zend_uchar    type,			/* active type */
				zend_uchar    type_flags,
				zend_uchar    const_flags,
				zend_uchar    reserved)	    /* call info for EX(This) */
		} v;
		uint32_t type_info;
	} u1;
	union {
		uint32_t     next;                   // 重点存眷这个  寄存hash 抵触下一个元素的地位
		uint32_t     cache_slot;           /* literal cache slot */
		uint32_t     lineno;               /* line number (for ast nodes) */
		uint32_t     num_args;             /* arguments number for EX(This) */
		uint32_t     fe_pos;               /* foreach position */
		uint32_t     fe_iter_idx;          /* foreach iterator index */
		uint32_t     access_flags;         /* class constant access flags */
		uint32_t     property_guard;       /* single property guard */
		uint32_t     extra;                /* not further specified */
	} u2;

构造图

Snipaste_2020-06-24_14-23-46.png

保举教程:《PHP》

以上就是PHP HashTable是甚么?的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 php故障解决 php使用问题

抱歉,评论功能暂时关闭!