[3705] | 1 | From https://github.com/ysdtkm/fortran_associative_array |
---|
| 2 | |
---|
| 3 | # Fortran associative array [![Build Status](https://travis-ci.org/ysdtkm/fortran_associative_array.svg?branch=master)](https://travis-ci.org/ysdtkm/fortran_associative_array) |
---|
| 4 | A scalable associative array (known as **hash table** or **dictionary**) for Fortran |
---|
| 5 | |
---|
| 6 | ## Specifications |
---|
| 7 | * Internal data structure is treap (randomized binary search tree) |
---|
| 8 | * Roughly corresponds to `std::map` (C++) or `dict` (Python) |
---|
| 9 | * A **key** can be `characters` (either fixed or arbitrary length), an `integer`, or a `real` |
---|
| 10 | * A **value** can be any fortran intrinsic data type (with fixed length or kind). A *copy* of the value is stored in the `dict` object |
---|
| 11 | * Does not affect Fortran's intrinsic random state |
---|
| 12 | * Implemented operations |
---|
| 13 | |
---|
| 14 | |Operation |Cost |Implementation | |
---|
| 15 | |---- |---- |---- | |
---|
| 16 | |Insertion/assignment |O(log n) |Subroutine `insert_or_assign(dict, key, val)` | |
---|
| 17 | |Deletion |O(log n) |Subroutine `remove(dict, key)`<br>(Error if not exist) | |
---|
| 18 | |Existence of a key |O(log n) |Logical function `exists(dict, key)` | |
---|
| 19 | |Reference |O(log n) |Valuetype function `get_val(dict, key)`<br>(Error if not exist) | |
---|
| 20 | |Get max/min/k-th key |O(log n) |Keytype function `get_kth_key(dict, k)`<br>(Error if out of bounds; 1-based)| |
---|
| 21 | |Count |O(1) |Integer function `get_size(dict)` | |
---|
| 22 | |Retrieve sorted array |O(n) |Subroutine `get_keys_vals(dict, keys, vals, n)`<br>(Not for arbitrary length keys)| |
---|
| 23 | |Clear |O(n) |Implicitly called as a destructor | |
---|
| 24 | |
---|
| 25 | * Other operations allowed by the data structure (not implemented) |
---|
| 26 | |
---|
| 27 | |Operation |Cost |Note | |
---|
| 28 | |---- |---- |---- | |
---|
| 29 | |Merge/split |O(log n) |Destructive | |
---|
| 30 | |lower_bound/upper_bound |O(log n) | | |
---|
| 31 | |Range search |O(log n + elements found)| | |
---|
| 32 | |Deep copy |O(n) |Preorder DFS | |
---|
| 33 | |
---|
| 34 | * Speed comparison with `gfortran`/`g++`, without compiler optimization |
---|
| 35 | |
---|
| 36 | <img src="test/speedtest/visual/out.png" width="50%"> |
---|
| 37 | |
---|
| 38 | ## Usage |
---|
| 39 | * See `sample.f90` for sample usage |
---|
| 40 | * Edit `dtypes.h` if using another data types |
---|
| 41 | * For string key (arbitrary length), `keytype1` should be `character(:),allocatable` and `keytype2` should be `character(*)` |
---|
| 42 | * For other key types, `keytype1` and `keytype2` are the same |
---|
| 43 | |
---|
| 44 | ## References |
---|
| 45 | * Treap https://en.wikipedia.org/wiki/Treap |
---|
| 46 | * Treap https://www.slideshare.net/iwiwi/2-12188757 |
---|
| 47 | |
---|