container 是轻量级 Fortran 字符串哈希表。它使用字符串键和 class(*) 值,适合保存配置、元数据及小规模动态映射。
需要 Fortran 2008 编译器和 fpm。
git clone https://github.com/FlexCTM/container.git
cd container
fpm run --example configuration示例位于 example/configuration.f90,演示整数和字符串值的写入与读取。也可以使用 Make:
make run在项目的 fpm.toml 中添加:
[dependencies]
container = { git = "https://github.com/FlexCTM/container.git" }use container, only: string_hash_map
type(string_hash_map) :: map
call map%set("answer", 42)
call map%set("threshold", 0.25)
call map%set("enabled", .true.)
call map%set("species", "PM2.5")
print *, map%get("answer")
print *, map%get_real("threshold")
print *, map%get_logical("enabled")
print *, map%get_char("species")
call map%clear()使用 get()、get_real()、get_logical() 和 get_char() 分别读取整数、实数、逻辑值和字符串。
get_real() 接受存储的 real32 或 real64,返回默认 kind 的 real。
多态指针仅用于内部实现,不属于公共 API。
键使用动态长度字符串,没有固定的字符数上限。键区分大小写并保留开头空格,但忽略末尾空格;例如
"species" 和 "species " 表示同一个键。
读取接口可用可选参数 found 检查结果:
logical :: found
integer :: interval
interval = map%get("output_interval", found)
if (.not. found) then
! The key is missing or its value is not an integer.
end if键不存在、哈希表为空或值类型不匹配时,found 为 .false.。整数和实数返回零,逻辑值返回
.false.,字符串返回空字符串。确定键和值类型正确时可以省略 found。
reserve(nsize) 只在首次分配时使用 nsize 选择桶数,后续调用保持现有内容和桶数不变。未显式调用
reserve() 时,第一次插入默认创建 199 个桶。哈希冲突通过链表处理。
哈希表拥有 set() 创建的值副本。覆盖值或调用 clear() 时会释放相应存储;对象离开作用域前应调用
clear()。
| 工具 | Release | Debug | 运行案例 | 运行测试 |
|---|---|---|---|---|
| Make | make release |
make debug |
make run |
make test |
| CMake | 见下方命令 | 见下方命令 | 直接运行生成的程序 | 暂未集成 |
| fpm | fpm build --profile release |
fpm build --profile debug |
fpm run --example configuration |
fpm test --profile debug |
Make 默认使用 Release;Debug 案例使用 make BUILD=debug run。产物分别位于 build/release/ 和 build/debug/。
cmake -S . -B build/cmake-release -DCMAKE_BUILD_TYPE=Release
cmake --build build/cmake-release
./build/cmake-release/example/configuration.exe
cmake -S . -B build/cmake-debug -DCMAKE_BUILD_TYPE=Debug
cmake --build build/cmake-debug
./build/cmake-debug/example/configuration.exemake test 内部调用 fpm Debug 测试。CMake 当前只编译库和案例,不注册测试。
Make 的 Release 库为 build/release/libcontainer.a,模块文件在 build/make/release/,案例程序在 build/release/example/。Debug 路径中的 release 替换为 debug。 make release 和 make debug 只构建库;make examples 构建全部案例。
CMake 的 Release 库为 build/cmake-release/lib/libcontainer.a,模块文件在 build/cmake-release/include/,案例程序在 build/cmake-release/example/。Debug 构建使用 build/cmake-debug/。
cmake --build build/cmake-release --target container 只构建库;不指定 --target 时同时构建库和全部案例。
gfortran main.f90 -Ibuild/cmake-release/include \
build/cmake-release/lib/libcontainer.a -o main.exe每个 example/*.f90 都独立构建。make run 运行全部案例,make run EXAMPLE=configuration 只运行指定案例;CMake 自动发现新增案例;fpm 使用 fpm run --example <案例名>。
Fortran 的 .mod 文件与编译器及其版本相关。下游软件应使用与本库相同的编译器系列和精度配置;链接静态库时,应把库文件放在使用它的源文件或目标文件之后。