1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
pub use super::linked_list_mapper::Iter;
use super::{LinkedListMapper, StorageClearable, StorageMapper};
use crate::abi::{TypeAbi, TypeDescriptionContainer, TypeName};
use crate::api::{EndpointFinishApi, ErrorApi, StorageReadApi, StorageWriteApi};
use crate::io::EndpointResult;
use crate::storage::{storage_get, storage_set};
use crate::types::{BoxedBytes, MultiResultVec};
use alloc::vec::Vec;
use dharitri_codec::{top_encode_to_vec, TopDecode, TopEncode};

const NULL_ENTRY: u32 = 0;
const NODE_ID_IDENTIFIER: &[u8] = b".node_id";

pub struct SetMapper<SA, T>
where
	SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
	T: TopEncode + TopDecode + 'static,
{
	api: SA,
	main_key: BoxedBytes,
	linked_list_mapper: LinkedListMapper<SA, T>,
}

impl<SA, T> StorageMapper<SA> for SetMapper<SA, T>
where
	SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
	T: TopEncode + TopDecode,
{
	fn new(api: SA, main_key: BoxedBytes) -> Self {
		SetMapper {
			api: api.clone(),
			main_key: main_key.clone(),
			linked_list_mapper: LinkedListMapper::<SA, T>::new(api, main_key),
		}
	}
}

impl<SA, T> StorageClearable for SetMapper<SA, T>
where
	SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
	T: TopEncode + TopDecode,
{
	fn clear(&mut self) {
		for value in self.linked_list_mapper.iter() {
			self.clear_node_id(&value);
		}
		self.linked_list_mapper.clear();
	}
}

impl<SA, T> SetMapper<SA, T>
where
	SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
	T: TopEncode + TopDecode,
{
	fn build_named_value_key(&self, name: &[u8], value: &T) -> BoxedBytes {
		let bytes = top_encode_to_vec(&value).unwrap();
		BoxedBytes::from_concat(&[self.main_key.as_slice(), name, &bytes])
	}

	fn get_node_id(&self, value: &T) -> u32 {
		storage_get(
			self.api.clone(),
			self.build_named_value_key(NODE_ID_IDENTIFIER, value)
				.as_slice(),
		)
	}

	fn set_node_id(&self, value: &T, node_id: u32) {
		storage_set(
			self.api.clone(),
			self.build_named_value_key(NODE_ID_IDENTIFIER, value)
				.as_slice(),
			&node_id,
		);
	}

	fn clear_node_id(&self, value: &T) {
		storage_set(
			self.api.clone(),
			self.build_named_value_key(NODE_ID_IDENTIFIER, value)
				.as_slice(),
			&BoxedBytes::empty(),
		);
	}

	/// Returns `true` if the set contains no elements.
	pub fn is_empty(&self) -> bool {
		self.linked_list_mapper.is_empty()
	}

	/// Returns the number of elements in the set.
	pub fn len(&self) -> usize {
		self.linked_list_mapper.len()
	}

	/// Returns `true` if the set contains a value.
	pub fn contains(&self, value: &T) -> bool {
		self.get_node_id(value) != NULL_ENTRY
	}

	/// Adds a value to the set.
	///
	/// If the set did not have this value present, `true` is returned.
	///
	/// If the set did have this value present, `false` is returned.
	pub fn insert(&mut self, value: T) -> bool {
		if self.contains(&value) {
			return false;
		}
		let new_node_id = self.linked_list_mapper.push_back_node_id(&value);
		self.set_node_id(&value, new_node_id);
		true
	}

	/// Removes a value from the set. Returns whether the value was
	/// present in the set.
	pub fn remove(&mut self, value: &T) -> bool {
		let node_id = self.get_node_id(value);
		if node_id == NULL_ENTRY {
			return false;
		}
		self.linked_list_mapper.remove_by_node_id(node_id);
		self.clear_node_id(value);
		true
	}

	/// An iterator visiting all elements in arbitrary order.
	/// The iterator element type is `&'a T`.
	pub fn iter(&self) -> Iter<SA, T> {
		self.linked_list_mapper.iter()
	}

	/// Checks the internal consistency of the collection. Used for unit tests.
	pub fn check_internal_consistency(&self) -> bool {
		self.linked_list_mapper.check_internal_consistency()
	}
}

/// Behaves like a MultiResultVec when an endpoint result.
impl<SA, FA, T> EndpointResult<FA> for SetMapper<SA, T>
where
	SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
	FA: EndpointFinishApi + Clone + 'static,
	T: TopEncode + TopDecode + EndpointResult<FA>,
{
	fn finish(&self, api: FA) {
		let v: Vec<T> = self.iter().collect();
		MultiResultVec::<T>::from(v).finish(api);
	}
}

/// Behaves like a MultiResultVec when an endpoint result.
impl<SA, T> TypeAbi for SetMapper<SA, T>
where
	SA: StorageReadApi + StorageWriteApi + ErrorApi + Clone + 'static,
	T: TopEncode + TopDecode + TypeAbi,
{
	fn type_name() -> TypeName {
		crate::types::MultiResultVec::<T>::type_name()
	}

	fn provide_type_descriptions<TDC: TypeDescriptionContainer>(accumulator: &mut TDC) {
		T::provide_type_descriptions(accumulator);
	}

	fn is_multi_arg_or_result() -> bool {
		true
	}
}