Skip to content

Commit c0aab8f

Browse files
committed
Initial commit
0 parents  commit c0aab8f

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
/Cargo.lock

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
language: rust

Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
3+
name = "ids_container"
4+
version = "0.1.0"
5+
authors = ["Pierre Krieger <[email protected]>"]
6+
description = "Container which automatically assigns keys when you insert in it"
7+
repository = "https://github.com/tomaka/rust-ids-container"
8+
license = "Apache-2.0"

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# ids_container
2+
3+
```rust
4+
[dependencies]
5+
ids_container = "*"
6+
```

src/lib.rs

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
use std::collections::hash_map::{self, HashMap};
2+
3+
pub struct IdsContainer<T> {
4+
data: HashMap<u32, T>,
5+
next_id: u32
6+
}
7+
8+
#[derive(Copy, Clone, Show, PartialEq, Eq, Hash)]
9+
pub struct Id {
10+
value: u32,
11+
}
12+
13+
pub struct Iter<'a, T: 'a> {
14+
iter: hash_map::Iter<'a, u32, T>,
15+
}
16+
17+
pub struct IterMut<'a, T: 'a> {
18+
iter: hash_map::IterMut<'a, u32, T>,
19+
}
20+
21+
impl<T> IdsContainer<T> {
22+
pub fn new() -> IdsContainer<T> {
23+
IdsContainer {
24+
data: HashMap::new(),
25+
next_id: 1,
26+
}
27+
}
28+
29+
/// Returns an iterator for all the keys and values in the container.
30+
pub fn get<'a>(&'a self, id: &Id) -> Option<&'a T> {
31+
self.data.get(&id.value)
32+
}
33+
34+
/// Returns an iterator for all the keys and values in the container.
35+
pub fn get_mut<'a>(&'a mut self, id: &Id) -> Option<&'a mut T> {
36+
self.data.get_mut(&id.value)
37+
}
38+
39+
/// Returns an iterator for all the keys and values in the container.
40+
pub fn iter(&self) -> Iter<T> {
41+
Iter {
42+
iter: self.data.iter()
43+
}
44+
}
45+
46+
/// Returns an iterator for all the keys and values in the container.
47+
pub fn iter_mut(&mut self) -> IterMut<T> {
48+
IterMut {
49+
iter: self.data.iter_mut()
50+
}
51+
}
52+
53+
/// Returns an iterator for all the values in the container.
54+
pub fn values(&self) -> hash_map::Values<u32, T> {
55+
self.data.values()
56+
}
57+
58+
/// Returns the number of elements.
59+
pub fn len(&self) -> usize {
60+
self.data.len()
61+
}
62+
63+
/// Returns true if the container is empty.
64+
pub fn is_empty(&self) -> bool {
65+
self.data.is_empty()
66+
}
67+
68+
/// Inserts a new element and returns its identifier.
69+
pub fn insert(&mut self, value: T) -> Id {
70+
let new_id = self.next_id.clone();
71+
self.next_id += 1;
72+
73+
self.data.insert(new_id, value);
74+
75+
Id {
76+
value: new_id
77+
}
78+
}
79+
80+
/// Removes an element from the container.
81+
///
82+
/// # Panic
83+
///
84+
/// Panics if the `id` was already removed.
85+
pub fn remove(&mut self, id: Id) {
86+
self.data.remove(&id.value).unwrap();
87+
}
88+
}
89+
90+
impl<'a, T: 'a> Iterator for Iter<'a, T> {
91+
type Item = (Id, &'a T);
92+
93+
fn next(&mut self) -> Option<(Id, &'a T)> {
94+
self.iter.next().map(|(id, value)| (Id { value: id.clone() }, value))
95+
}
96+
}
97+
98+
impl<'a, T: 'a> Iterator for IterMut<'a, T> {
99+
type Item = (Id, &'a mut T);
100+
101+
fn next(&mut self) -> Option<(Id, &'a mut T)> {
102+
self.iter.next().map(|(id, value)| (Id { value: id.clone() }, value))
103+
}
104+
}

tests/tests.rs

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
extern crate ids_container;
2+
3+
use ids_container::IdsContainer;
4+
5+
#[test]
6+
fn different_ids() {
7+
let mut container = IdsContainer::new();
8+
9+
let id1 = container.insert(5);
10+
let id2 = container.insert(5);
11+
let id3 = container.insert(7);
12+
let id4 = container.insert(12);
13+
14+
assert!(id1 != id2);
15+
assert!(id2 != id3);
16+
assert!(id3 != id4);
17+
}
18+
19+
#[test]
20+
fn get() {
21+
let mut container = IdsContainer::new();
22+
23+
let id1 = container.insert(5);
24+
let id2 = container.insert(8);
25+
26+
assert_eq!(container.get(&id1), Some(&5));
27+
assert_eq!(container.get(&id2), Some(&8));
28+
29+
let id3 = container.insert(12);
30+
container.remove(id3.clone());
31+
assert_eq!(container.get(&id3), None);
32+
}
33+
34+
#[test]
35+
fn len() {
36+
let mut container = IdsContainer::new();
37+
assert_eq!(container.len(), 0);
38+
39+
let id1 = container.insert(5);
40+
assert_eq!(container.len(), 1);
41+
42+
let id2 = container.insert(5);
43+
assert_eq!(container.len(), 2);
44+
45+
let id3 = container.insert(7);
46+
assert_eq!(container.len(), 3);
47+
48+
let id4 = container.insert(12);
49+
assert_eq!(container.len(), 4);
50+
51+
container.remove(id2);
52+
assert_eq!(container.len(), 3);
53+
54+
container.remove(id1);
55+
assert_eq!(container.len(), 2);
56+
57+
container.remove(id4);
58+
assert_eq!(container.len(), 1);
59+
60+
container.remove(id3);
61+
assert_eq!(container.len(), 0);
62+
}
63+
64+
#[test]
65+
fn is_empty() {
66+
let mut container = IdsContainer::new();
67+
assert!(container.is_empty());
68+
69+
let id1 = container.insert(5);
70+
assert!(!container.is_empty());
71+
72+
let id2 = container.insert(5);
73+
assert!(!container.is_empty());
74+
75+
let id3 = container.insert(7);
76+
assert!(!container.is_empty());
77+
78+
let id4 = container.insert(12);
79+
assert!(!container.is_empty());
80+
81+
container.remove(id2);
82+
assert!(!container.is_empty());
83+
84+
container.remove(id1);
85+
assert!(!container.is_empty());
86+
87+
container.remove(id4);
88+
assert!(!container.is_empty());
89+
90+
container.remove(id3);
91+
assert!(container.is_empty());
92+
}

0 commit comments

Comments
 (0)