读书笔记--C语言接口与实现--atom(原子)

本文介绍了C语言中的原子(Atom),它是一个指向唯一、不可变字节序列的指针,通常用于数据结构的键,以节省空间并简化字节序列的比较。原子可以是字符串或其他字节序列,且比较原子通过比较指针即可。文中还涵盖了原子的应用场景、原子类型的结构以及原子表的实现。
摘要由CSDN通过智能技术生成

概念理解

An atom is a pointer to a unique,immutable sequence of zero or more arbitrary bytes. Most atoms are pointers to null-terminated strings, but a pointer to any sequence of bytes can be an atom. There is only a single occurrence of any atom, which is why it’s called an atom. Two atoms are identical if they point to the same location. Comparing two byte sequences for equality by simply comparing pointers is one of the advantages of atoms. Another advantage is that using atoms saves space because there’s only one occurrence of each sequence. Atoms are often used as keys in data structures that are indexed by sequences of arbitrary bytes instead of by integers. The tables and sets described in Chapters 8 and 9 are examples.

翻译:

一个原子是一个指向一个独特的,不可改变的零或更多的字节序列的指针。大多数原子是指向以空为结束的字符串,但是一个指向任何字节序列的指针都可以是一个原子。任何原子只能出现一次。通过简单的比较指针,比较两个字节序列,如果它们指向相同的位置,则是相同的,这就是原子的优点之一。另一个优点是,使用原子节省空间,因为每个序列只会出现一次。
总结:指向一个不可改变的零或更多字节序列的指针,并且该指针是唯一的,就是原子。优点:1.比较指针可以判断字节序列是否相同;2.节省空间。

画图表示:

这里写图片描述

根据概念来理解,原子就是一个指针,它可以是指向一块任何字节序列,上面以字符指针来说,指向的常量区,不能更改,字节序列;而在指向堆中的可以改变某一个字节的内容。就是原子例子。原子类型,应该包括了所有指针类型。

应用:原子经常被用作数据结构中的键,而在数据结构中,用任意的字节序列来表示,而不是用整数来表示。

代码实现

atom.h

#ifndef ATOM_INCLUDED
#define ATOM_INCLUDED
extern int Atom_length(const char *str);
extern const char *Atom_new (const char *str, int len);
extern const char *Atom_string(const char *str);
extern const char *Atom_int (long n);
#endif

atom.c

#include<stdio.h>
#include<assert.h>
#define _CRT_SECURE_NO_WARNINGS
#include<string.h>
#include<stdlib.h>
#define LONG_MAX1 1000
#define LONG_MIN1 100
#define NELEMS(x) ((sizeof (x))/(sizeof ((x)[0])))
static struct atom 
{
    struct atom *link;
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值