1 use super::{VQElement, VQElementSum};
3 // very simple RNG for internal needs
9 fn new() -> Self { Self { seed: 0x1234 } }
10 fn next(&mut self) -> u8 {
11 if (self.seed & 0x8000) != 0 {
12 self.seed = (self.seed & 0x7FFF) * 2 ^ 0x1B2B;
25 struct Cluster<T: VQElement, TS: VQElementSum<T>> {
32 impl<T: VQElement, TS: VQElementSum<T>> Cluster<T, TS> {
33 fn new(centroid: T) -> Self {
43 self.sum = TS::zero();
46 fn add_point(&mut self, entry: &Entry<T>) {
47 self.sum.add(entry.val, entry.count);
48 self.count += entry.count;
50 fn add_dist(&mut self, entry: &Entry<T>) {
51 self.dist += u64::from(self.centroid.dist(entry.val)) * entry.count;
53 fn calc_centroid(&mut self) {
55 self.centroid = self.sum.get_centroid();
58 fn calc_dist(&mut self) {
62 pub struct ELBG<T: VQElement, TS: VQElementSum<T>> {
63 clusters: Vec<Cluster<T, TS>>,
66 impl<T: VQElement+Default, TS: VQElementSum<T>> ELBG<T, TS> {
67 pub fn new(initial_cb: &[T]) -> Self {
68 let mut clusters = Vec::with_capacity(initial_cb.len());
69 for elem in initial_cb.iter() {
70 let cluster = Cluster::new(*elem);
71 clusters.push(cluster);
77 fn new_split(old_index: usize, entries: &[Entry<T>], indices: &[usize]) -> Option<(T, T)> {
78 let mut max = T::min_cw();
79 let mut min = T::max_cw();
80 let mut found = false;
81 for (entry, idx) in entries.iter().zip(indices) {
82 if *idx == old_index {
83 max = max.max(entry.val);
84 min = min.min(entry.val);
91 let mut ts0 = TS::zero();
92 let mut ts1 = TS::zero();
93 ts0.add(min, 2); ts0.add(max, 1);
94 ts1.add(min, 1); ts1.add(max, 2);
95 Some((ts0.get_centroid(), ts1.get_centroid()))
97 fn old_centre(&self, old_index1: usize, old_index2: usize, entries: &[Entry<T>], indices: &[usize]) -> T {
98 let mut max = T::min_cw();
99 let mut min = T::max_cw();
100 let mut found = false;
101 for (entry, idx) in entries.iter().zip(indices) {
102 if *idx == old_index1 || *idx == old_index2 {
103 max = max.max(entry.val);
104 min = min.min(entry.val);
109 max = self.clusters[old_index1].centroid.max(self.clusters[old_index2].centroid);
110 min = self.clusters[old_index1].centroid.min(self.clusters[old_index2].centroid);
112 let mut ts = TS::zero();
113 ts.add(min, 2); ts.add(max, 1);
116 fn estimate_old(old_idx0: usize, old_idx1: usize, c: T, entries: &[Entry<T>], indices: &[usize]) -> u64 {
117 let mut clu: Cluster<T, TS> = Cluster::new(c);
119 for (entry, idx) in entries.iter().zip(indices) {
120 if *idx == old_idx0 || *idx == old_idx1 {
122 count += entry.count;
129 fn estimate_new(c0: T, c1: T, old_idx: usize, entries: &[Entry<T>], indices: &[usize]) -> u64 {
130 let mut clu0: Cluster<T, TS> = Cluster::new(c0);
131 let mut clu1: Cluster<T, TS> = Cluster::new(c1);
134 for (entry, idx) in entries.iter().zip(indices) {
136 if c0.dist(entry.val) < c1.dist(entry.val) {
137 clu0.add_dist(entry);
138 count0 += entry.count;
140 clu1.add_dist(entry);
141 count1 += entry.count;
149 clu0.dist + clu1.dist
151 pub fn quantise(&mut self, src: &[T], dst: &mut [T]) {
152 if src.len() < 1 || dst.len() != self.clusters.len() {
155 let mut old_cb = vec![T::default(); self.clusters.len()];
156 let mut prev_dist = std::u64::MAX;
157 let mut dist = std::u64::MAX / 2;
158 let mut indices = Vec::with_capacity(src.len());
159 let mut elements = Vec::with_capacity(src.len());
160 elements.extend_from_slice(src);
161 for comp in 0..T::num_components() {
162 T::sort_by_component(elements.as_mut_slice(), comp);
164 let mut entries = Vec::with_capacity(elements.len() / 2);
165 let mut lastval = elements[0];
167 for point in elements.iter().skip(1) {
168 if &lastval == point {
171 entries.push(Entry { val: lastval, count: run });
176 entries.push(Entry { val: lastval, count: run });
179 let mut low_u: Vec<usize> = Vec::with_capacity(self.clusters.len());
180 let mut high_u: Vec<usize> = Vec::with_capacity(self.clusters.len());
181 let mut rng = RNG::new();
182 let mut iterations = 0usize;
183 let mut do_elbg_step = true;
184 while (iterations < 20) && (dist < prev_dist - prev_dist / 1000) {
186 for i in 0..dst.len() {
187 old_cb[i] = self.clusters[i].centroid;
188 self.clusters[i].reset();
190 // put points into the nearest clusters
192 for entry in entries.iter() {
194 let mut bestdist = std::u32::MAX;
195 for (i, cluster) in self.clusters.iter().enumerate() {
196 let dist = entry.val.dist(cluster.centroid);
205 indices.push(bestidx);
206 self.clusters[bestidx].add_point(entry);
209 for cluster in self.clusters.iter_mut() {
210 cluster.calc_centroid();
213 for (idx, entry) in indices.iter().zip(entries.iter()) {
214 self.clusters[*idx].add_dist(entry);
216 for cluster in self.clusters.iter_mut() {
218 dist += cluster.dist;
221 let dmean = dist / (dst.len() as u64);
224 let mut used = vec![false; dst.len()];
225 for (i, cluster) in self.clusters.iter().enumerate() {
226 if cluster.dist < dmean {
228 } else if cluster.dist > dmean * 2 {
235 do_elbg_step = false;
236 for low_idx in low_u.iter() {
237 if high_u.len() == 0 {
240 let high_idx_idx = (rng.next() as usize) % high_u.len();
241 let high_idx = high_u[high_idx_idx];
242 let mut closest_idx = *low_idx;
243 let mut closest_dist = std::u32::MAX;
244 let low_centr = self.clusters[*low_idx].centroid;
245 for i in 0..dst.len() {//low_u.iter() {
246 if i == *low_idx || used[i] {
249 let dist = self.clusters[i].centroid.dist(low_centr);
250 if closest_dist > dist {
255 if closest_idx == *low_idx {
258 let old_dist = self.clusters[*low_idx].dist + self.clusters[closest_idx].dist + self.clusters[high_idx].dist;
259 let old_centr = self.old_centre(*low_idx, closest_idx, entries.as_slice(), indices.as_slice());
260 let ret = Self::new_split(high_idx, entries.as_slice(), indices.as_slice());
261 if let Some((centr0, centr1)) = ret {
262 let dist_o = if old_dist > self.clusters[high_idx].dist {
263 Self::estimate_old(*low_idx, closest_idx, old_centr, entries.as_slice(), indices.as_slice())
265 let dist_n = Self::estimate_new(centr0, centr1, high_idx, entries.as_slice(), indices.as_slice());
266 if dist_o + dist_n < old_dist {
267 self.clusters[*low_idx ].centroid = old_centr;
268 self.clusters[closest_idx].centroid = centr0;
269 self.clusters[high_idx ].centroid = centr1;
270 used[*low_idx] = true;
271 used[closest_idx] = true;
272 used[high_idx] = true;
273 high_u.remove(high_idx_idx);
281 if dist < prev_dist {
282 for i in 0..dst.len() {
283 old_cb[i] = self.clusters[i].centroid;
286 dst.copy_from_slice(&old_cb);