# fast-able

High-performance synchronization library / 高性能同步数据结构库

## Features / 特性

* **SyncHashMap**: Thread-safe HashMap / 线程安全的 HashMap
* **SyncVec**: Thread-safe Vec / 线程安全的 Vec
* **SyncBtreeMap**: Thread-safe BtreeMap / 线程安全的 BtreeMap
* **WaitGroup**: Async/blocking WaitGroup / 支持异步/同步的 WaitGroup

## Usage / 使用方法

### SyncHashMap

`SyncHashMap` provides a thread-safe hash map implementation.

```rust
use fast_able::SyncHashMap;
use std::sync::Arc;
use std::thread;

fn main() {
    // Create a new SyncHashMap
    let map = Arc::new(SyncHashMap::<String, String>::new());

    // Insert data
    map.insert("/".to_string(), "1".to_string());
    map.insert("/js".to_string(), "2".to_string());

    // Concurrent access
    let map_clone = map.clone();
    let handle = thread::spawn(move || {
        map_clone.insert("/fn".to_string(), "3".to_string());
        if let Some(val) = map_clone.get("/js") {
            println!("In thread: /js = {}", *val);
        }
    });

    handle.join().unwrap();

    // Get data
    assert_eq!("1", *map.get("/").unwrap());
    assert_eq!("3", *map.get("/fn").unwrap());
    
    println!("Map content: {:?}", map);
}
```

### SyncVec

`SyncVec` provides a thread-safe vector implementation.

```rust
use fast_able::SyncVec;
use std::sync::Arc;
use std::thread;

fn main() {
    // Create a new SyncVec
    let vec = Arc::new(SyncVec::<i32>::new());

    // Push data
    vec.push(1);
    vec.push(2);

    // Concurrent access
    let vec_clone = vec.clone();
    let handle = thread::spawn(move || {
        vec_clone.push(3);
        // Get and modify data
        if let Some(mut val) = vec_clone.get_mut(0) {
            *val = 100;
        }
    });

    handle.join().unwrap();

    // Read data
    println!("Vec len: {}", vec.len());
    println!("Item at 0: {:?}", *vec.get(0).unwrap()); // Should be 100
    println!("Item at 2: {:?}", *vec.get(2).unwrap()); // Should be 3
}
```

### WaitGroup

`WaitGroup` supports both synchronous and asynchronous waiting.

```rust
use fast_able::wg::WaitGroup;
use std::time::Duration;
use tokio::time::sleep;

#[tokio::main]
async fn main() {
    let wg = WaitGroup::new();
    
    let wg1 = wg.clone();
    tokio::spawn(async move {
        sleep(Duration::from_secs(1)).await;
        println!("Task 1 done");
        drop(wg1); // Drop to signal completion
    });

    let wg2 = wg.clone();
    tokio::spawn(async move {
        sleep(Duration::from_secs(1)).await;
        println!("Task 2 done");
        drop(wg2);
    });

    // Wait for all tasks to complete
    wg.wait_async().await;
    println!("All tasks done");
}
```