#unique-id #ids #resize #financial-data #structure #integer #vec

resizing-vec

用于具有线性增长 id 的数据的自动调整大小的向量

4 个版本

0.1.4 2023 年 11 月 27 日
0.1.3 2023 年 11 月 26 日
0.1.1 2023 年 11 月 26 日
0.1.0 2023 年 11 月 25 日

571数据结构

每月下载 24

MIT 许可证

13KB
191

Resizing Vec

ResizingVec 是当

  • 每个数据都有一个唯一的整数 id 时使用的
  • id(数字)是成簇的

底层数据结构如下

struct ResizingVec<T> {
    vec: Vec<Option<T>>,
    active: usize,
}

插入索引为 5 的项目

use resizing_vec::ResizingVec;

let mut r_vec = ResizingVec::new();
r_vec.insert(5, "6th elem".to_string());
// ResizingVec { vec: [None, None, None, None, None, Some("5th elem")], active: 1 }

由于元素被插入到第 5 个索引处,但在插入之前没有其他元素存在,因此向量为索引 0-4 的索引填充了 None 值。

插入操作的时间复杂度取决于向量是否需要调整大小。

用例

这可以用于具有唯一整数 id 且需要依赖快速读取操作的数据。

例如,这可以用于金融数据

use resizing_vec::ResizingVec;
use std::collections::HashMap;

struct IdAssigner {
	map: HashMap<String, usize>,
}

impl IdAssigner {
	pub fn new() -> Self { Self {map: HashMap::new()} }
	pub fn get(&mut self, ticker: &str) -> usize {
		match self.map.get(ticker) {
		    Some(id) => *id,
		    None => {
		        let next = self.map.len()+1;
		        self.map.insert(ticker.to_string(), next);
		        next
		    }
		}
	}
}

struct Price {
	ticker: String,
	last: usize
}

let mut id_assigner = IdAssigner::new();
let mut r_vec = ResizingVec::new();

let id = id_assigner.get("INTC");
r_vec.insert(id, "INTEL CORP");

// Now that we have a unique id for every ticker we can use that to easily info about each ticker and enrich our price data quickly with it.
let price = Price {ticker: "INTC".to_string(), last: 52};
let full_name = r_vec[id];
// println!("{} is trading at {}$", full_name, price.last);

另一个应用是,一些金融数据提供商不会为每次交易/nbbo 发送股票/期权合约,而是每天早上发送一次标识符,该标识符将在整个交易日中类似于股票/合约。每个定位在每个通道中都是唯一的

股票: AAPL,定位: 10,通道: 5

现在当你得到一笔交易执行

通道: 5,定位: 10,大小: 10,价格: 120

您可以通过以下方式找到相应的股票

struct Trade {
	channel: usize,
	size: usize,
	price: usize
}

// Initialize in the morning
use resizing_vec::ResizingVec;
let mut channel_five = ResizingVec::new();
channel_five.insert(10, "AAPL".to_string());

// When you get a trade execution
let trade = Trade {channel: 10, size: 100, price: 120};
let ticker = channel_five.get(trade.channel).unwrap();
println!("{} {} @ ${}", ticker, trade.size, trade.price);

无运行时依赖