pallet_parachain_staking/
set.rs1use frame_support::traits::Get;
18use parity_scale_codec::{Decode, Encode};
19use scale_info::TypeInfo;
20#[cfg(feature = "std")]
21use serde::{Deserialize, Serialize};
22use sp_runtime::{BoundedVec, RuntimeDebug};
23use sp_std::prelude::*;
24
25#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
27#[derive(RuntimeDebug, PartialEq, Eq, Encode, Decode, Default, Clone, TypeInfo)]
28pub struct OrderedSet<T>(pub Vec<T>);
29
30impl<T: Ord> OrderedSet<T> {
31 pub fn new() -> Self {
33 Self(Vec::new())
34 }
35
36 pub fn from(mut v: Vec<T>) -> Self {
39 v.sort();
40 v.dedup();
41 Self::from_sorted_set(v)
42 }
43
44 pub fn from_sorted_set(v: Vec<T>) -> Self {
47 Self(v)
48 }
49
50 pub fn insert(&mut self, value: T) -> bool {
53 match self.0.binary_search(&value) {
54 Ok(_) => false,
55 Err(loc) => {
56 self.0.insert(loc, value);
57 true
58 }
59 }
60 }
61
62 pub fn remove(&mut self, value: &T) -> bool {
65 match self.0.binary_search(value) {
66 Ok(loc) => {
67 self.0.remove(loc);
68 true
69 }
70 Err(_) => false,
71 }
72 }
73
74 pub fn contains(&self, value: &T) -> bool {
76 self.0.binary_search(value).is_ok()
77 }
78
79 pub fn clear(&mut self) {
81 self.0.clear();
82 }
83}
84
85impl<T: Ord> From<Vec<T>> for OrderedSet<T> {
86 fn from(v: Vec<T>) -> Self {
87 Self::from(v)
88 }
89}
90
91#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
93#[derive(RuntimeDebug, PartialEq, Eq, Encode, Decode, Clone, TypeInfo)]
94#[scale_info(skip_type_params(S))]
95pub struct BoundedOrderedSet<T, S: Get<u32>>(pub BoundedVec<T, S>);
96
97impl<T, S: Get<u32>> sp_std::default::Default for BoundedOrderedSet<T, S> {
98 fn default() -> Self {
99 BoundedOrderedSet(BoundedVec::default())
100 }
101}
102
103impl<T: Ord, S: Get<u32>> BoundedOrderedSet<T, S> {
104 pub fn new() -> Self {
106 Self(BoundedVec::default())
107 }
108
109 pub fn from(mut v: BoundedVec<T, S>) -> Self {
112 v.sort();
113 let v = v.try_mutate(|inner| inner.dedup()).expect(
114 "input is a valid BoundedVec and deduping can only reduce the number of entires; qed",
115 );
116 Self::from_sorted_set(v)
117 }
118
119 pub fn from_sorted_set(v: BoundedVec<T, S>) -> Self {
122 Self(v)
123 }
124
125 pub fn try_insert(&mut self, value: T) -> Result<bool, ()> {
128 match self.0.binary_search(&value) {
129 Ok(_) => Ok(false),
130 Err(loc) => self.0.try_insert(loc, value).map(|_| true).map_err(|_| ()),
131 }
132 }
133
134 pub fn remove(&mut self, value: &T) -> bool {
137 match self.0.binary_search(value) {
138 Ok(loc) => {
139 self.0.remove(loc);
140 true
141 }
142 Err(_) => false,
143 }
144 }
145
146 pub fn contains(&self, value: &T) -> bool {
148 self.0.binary_search(value).is_ok()
149 }
150
151 pub fn clear(mut self) {
153 let v = self.0.try_mutate(|inner| inner.clear()).expect(
154 "input is a valid BoundedVec and clearing can only reduce the number of entires; qed",
155 );
156 self.0 = v;
157 }
158}
159
160impl<T: Ord, S: Get<u32>> From<BoundedVec<T, S>> for BoundedOrderedSet<T, S> {
161 fn from(v: BoundedVec<T, S>) -> Self {
162 Self::from(v)
163 }
164}