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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
use std::borrow::Borrow;
use std::marker::PhantomData;

use crate::cell::*;
use crate::util::*;
use crate::Error;

use super::raw::*;
use super::{dict_get, dict_insert, dict_load_from_root, serialize_entry, DictKey, SetMode};

/// Typed dictionary with fixed length keys.
pub struct Dict<C: CellFamily, K, V> {
    pub(crate) root: Option<CellContainer<C>>,
    _key: PhantomData<K>,
    _value: PhantomData<V>,
}

impl<'a, C: CellFamily, K, V> Load<'a, C> for Dict<C, K, V> {
    #[inline]
    fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
        Some(Self {
            root: <_>::load_from(slice)?,
            _key: PhantomData,
            _value: PhantomData,
        })
    }
}

impl<C: CellFamily, K, V> Store<C> for Dict<C, K, V> {
    #[inline]
    fn store_into(&self, builder: &mut CellBuilder<C>, finalizer: &mut dyn Finalizer<C>) -> bool {
        self.root.store_into(builder, finalizer)
    }
}

impl<C: CellFamily, K, V> Default for Dict<C, K, V> {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<C: CellFamily, K, V> Clone for Dict<C, K, V> {
    fn clone(&self) -> Self {
        Self {
            root: self.root.clone(),
            _key: PhantomData,
            _value: PhantomData,
        }
    }
}

impl<C: CellFamily, K, V> Eq for Dict<C, K, V> {}

impl<C: CellFamily, K, V> PartialEq for Dict<C, K, V> {
    fn eq(&self, other: &Self) -> bool {
        match (&self.root, &other.root) {
            (Some(this), Some(other)) => this.eq(other),
            (None, None) => true,
            _ => false,
        }
    }
}

impl<C: CellFamily, K, V> From<Option<CellContainer<C>>> for Dict<C, K, V> {
    #[inline]
    fn from(dict: Option<CellContainer<C>>) -> Self {
        Self {
            root: dict,
            _key: PhantomData,
            _value: PhantomData,
        }
    }
}

impl<C: CellFamily, K, V> std::fmt::Debug for Dict<C, K, V> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        debug_struct_field1_finish(f, "Dict", "root", &self.root)
    }
}

impl<C: CellFamily, K, V> Dict<C, K, V> {
    /// Creates an empty dictionary
    pub const fn new() -> Self {
        Self {
            root: None,
            _key: PhantomData,
            _value: PhantomData,
        }
    }

    /// Returns `true` if the dictionary contains no elements.
    pub const fn is_empty(&self) -> bool {
        self.root.is_none()
    }

    /// Returns the underlying root cell of the dictionary.
    #[inline]
    pub const fn root(&self) -> &Option<CellContainer<C>> {
        &self.root
    }
}

impl<C, K, V> Dict<C, K, V>
where
    for<'c> C: CellFamily + 'c,
    K: DictKey,
{
    /// Loads a non-empty dictionary from a root cell.
    pub fn load_from_root_ext(
        slice: &mut CellSlice<'_, C>,
        finalizer: &mut dyn Finalizer<C>,
    ) -> Option<Self> {
        Some(Self {
            root: Some(dict_load_from_root(slice, K::BITS, finalizer)?),
            _key: PhantomData,
            _value: PhantomData,
        })
    }
}

impl<C, K, V> Dict<C, K, V>
where
    for<'c> C: DefaultFinalizer + 'c,
    K: Store<C> + DictKey,
{
    /// Returns `true` if the dictionary contains a value for the specified key.
    pub fn contains_key<Q>(&self, key: Q) -> Result<bool, Error>
    where
        Q: Borrow<K>,
    {
        fn contains_key_impl<C, K>(root: &Option<CellContainer<C>>, key: &K) -> Result<bool, Error>
        where
            for<'c> C: DefaultFinalizer + 'c,
            K: Store<C> + DictKey,
        {
            let key = ok!(serialize_entry(key, &mut C::default_finalizer()));
            Ok(ok!(dict_get(root, K::BITS, key.as_ref().as_slice())).is_some())
        }
        contains_key_impl(&self.root, key.borrow())
    }
}

impl<C, K, V> Dict<C, K, V>
where
    for<'c> C: DefaultFinalizer + 'c,
    K: Store<C> + DictKey,
{
    /// Returns the value corresponding to the key.
    ///
    /// Key is serialized using the default finalizer.
    pub fn get<'a: 'b, 'b, Q>(&'a self, key: Q) -> Result<Option<V>, Error>
    where
        Q: Borrow<K> + 'b,
        V: Load<'a, C>,
    {
        self.get_ext(key, &mut C::default_finalizer())
    }

    /// Returns the raw value corresponding to the key.
    ///
    /// Key is serialized using the default finalizer.
    pub fn get_raw<'a: 'b, 'b, Q>(&'a self, key: Q) -> Result<Option<CellSlice<'a, C>>, Error>
    where
        Q: Borrow<K> + 'b,
    {
        self.get_raw_ext(key, &mut C::default_finalizer())
    }
}

impl<C, K, V> Dict<C, K, V>
where
    for<'c> C: DefaultFinalizer + 'c,
    K: Store<C> + DictKey,
    V: Store<C>,
{
    /// Sets the value associated with the key in the dictionary.
    ///
    /// Use [`set_ext`] if you need to use a custom finalizer.
    ///
    /// [`set_ext`]: Dict::set_ext
    pub fn set<Q, T>(&mut self, key: Q, value: T) -> Result<(), Error>
    where
        Q: Borrow<K>,
        T: Borrow<V>,
    {
        self.set_ext(key, value, &mut C::default_finalizer())
    }

    /// Sets the value associated with the key in the dictionary
    /// only if the key was already present in it.
    ///
    /// Use [`replace_ext`] if you need to use a custom finalizer.
    ///
    /// [`replace_ext`]: Dict::replace_ext
    pub fn replace<Q, T>(&mut self, key: Q, value: T) -> Result<(), Error>
    where
        Q: Borrow<K>,
        T: Borrow<V>,
    {
        self.replace_ext(key, value, &mut C::default_finalizer())
    }

    /// Sets the value associated with key in dictionary,
    /// but only if it is not already present.
    ///
    /// Use [`add_ext`] if you need to use a custom finalizer.
    ///
    /// [`add_ext`]: Dict::add_ext
    pub fn add<Q, T>(&mut self, key: Q, value: T) -> Result<(), Error>
    where
        Q: Borrow<K>,
        T: Borrow<V>,
    {
        self.add_ext(key, value, &mut C::default_finalizer())
    }
}

impl<C: CellFamily, K, V> Dict<C, K, V>
where
    K: Store<C> + DictKey,
{
    /// Gets an iterator over the entries of the dictionary, sorted by key.
    /// The iterator element type is `Result<(K, V)>`.
    ///
    /// If the dictionary is invalid, finishes after the first invalid element,
    /// returning an error.
    ///
    /// # Performance
    ///
    /// In the current implementation, iterating over dictionary builds a key
    /// for each element. Use [`values`] or [`raw_values`] if you don't need keys from an iterator.
    ///
    /// [`values`]: Dict::values
    /// [`raw_values`]: Dict::raw_values
    pub fn iter<'a>(&'a self) -> Iter<'_, C, K, V>
    where
        V: Load<'a, C>,
    {
        Iter::new(&self.root)
    }

    /// Gets an iterator over the keys of the dictionary, in sorted order.
    /// The iterator element type is `Result<K>`.
    ///
    /// If the dictionary is invalid, finishes after the first invalid element,
    /// returning an error.
    ///
    /// # Performance
    ///
    /// In the current implementation, iterating over dictionary builds a key
    /// for each element. Use [`values`] if you don't need keys from an iterator.
    ///
    /// [`values`]: Dict::values
    pub fn keys(&'_ self) -> Keys<'_, C, K> {
        Keys::new(&self.root)
    }
}

impl<C, K, V> Dict<C, K, V>
where
    for<'c> C: DefaultFinalizer + 'c,
    K: DictKey,
{
    /// Gets an iterator over the values of the dictionary, in order by key.
    /// The iterator element type is `Result<V>`.
    ///
    /// If the dictionary is invalid, finishes after the first invalid element,
    /// returning an error.
    pub fn values<'a>(&'a self) -> Values<'a, C, V>
    where
        V: Load<'a, C>,
    {
        Values::new(&self.root, K::BITS)
    }
}

impl<C, K, V> Dict<C, K, V>
where
    for<'c> C: CellFamily + 'c,
    K: Store<C> + DictKey,
{
    /// Returns the value corresponding to the key.
    ///
    /// Key is serialized using the provided finalizer.
    pub fn get_ext<'a: 'b, 'b, Q>(
        &'a self,
        key: Q,
        finalizer: &mut dyn Finalizer<C>,
    ) -> Result<Option<V>, Error>
    where
        Q: Borrow<K> + 'b,
        V: Load<'a, C>,
    {
        pub fn get_ext_impl<'a: 'b, 'b, C, K, V>(
            root: &'a Option<CellContainer<C>>,
            key: &'b K,
            finalizer: &mut dyn Finalizer<C>,
        ) -> Result<Option<V>, Error>
        where
            for<'c> C: CellFamily + 'c,
            K: Store<C> + DictKey,
            V: Load<'a, C>,
        {
            let key = ok!(serialize_entry(key, finalizer));
            let Some(mut value) = ok!(dict_get(root, K::BITS, key.as_ref().as_slice())) else {
                return Ok(None);
            };

            match V::load_from(&mut value) {
                Some(value) => Ok(Some(value)),
                None => Err(Error::CellUnderflow),
            }
        }

        get_ext_impl(&self.root, key.borrow(), finalizer)
    }

    /// Returns the value corresponding to the key.
    ///
    /// Key is serialized using the provided finalizer.
    pub fn get_raw_ext<'a: 'b, 'b, Q>(
        &'a self,
        key: Q,
        finalizer: &mut dyn Finalizer<C>,
    ) -> Result<Option<CellSlice<'a, C>>, Error>
    where
        Q: Borrow<K> + 'b,
    {
        pub fn get_raw_ext_impl<'a: 'b, 'b, C, K>(
            root: &'a Option<CellContainer<C>>,
            key: &'b K,
            finalizer: &mut dyn Finalizer<C>,
        ) -> Result<Option<CellSlice<'a, C>>, Error>
        where
            for<'c> C: CellFamily + 'c,
            K: Store<C> + DictKey,
        {
            let key = ok!(serialize_entry(key, finalizer));
            dict_get(root, K::BITS, key.as_ref().as_slice())
        }

        get_raw_ext_impl(&self.root, key.borrow(), finalizer)
    }

    /// Gets an iterator over the raw entries of the dictionary, sorted by key.
    /// The iterator element type is `Result<(CellBuilder<C>, CellSlice<C>)>`.
    ///
    /// If the dictionary is invalid, finishes after the first invalid element,
    /// returning an error.
    ///
    /// # Performance
    ///
    /// In the current implementation, iterating over dictionary builds a key
    /// for each element. Use [`values`] or [`raw_values`] if you don't need keys from an iterator.
    ///
    /// [`values`]: Dict::values
    /// [`raw_values`]: Dict::raw_values
    pub fn raw_iter(&'_ self) -> RawIter<'_, C> {
        RawIter::new(&self.root, K::BITS)
    }

    /// Gets an iterator over the raw keys of the dictionary, in sorted order.
    /// The iterator element type is `Result<CellBuilder<C>>`.
    ///
    /// If the dictionary is invalid, finishes after the first invalid element,
    /// returning an error.
    ///
    /// # Performance
    ///
    /// In the current implementation, iterating over dictionary builds a key
    /// for each element. Use [`values`] or [`raw_values`] if you don't need keys from an iterator.
    ///
    /// [`values`]: Dict::values
    /// [`raw_values`]: Dict::raw_values
    pub fn raw_keys(&'_ self) -> RawKeys<'_, C> {
        RawKeys::new(&self.root, K::BITS)
    }
}

impl<C, K, V> Dict<C, K, V>
where
    for<'c> C: CellFamily + 'c,
    K: DictKey,
{
    /// Gets an iterator over the raw values of the dictionary, in order by key.
    /// The iterator element type is `Result<CellSlice<C>>`.
    ///
    /// If the dictionary is invalid, finishes after the first invalid element,
    /// returning an error.
    pub fn raw_values(&'_ self) -> RawValues<'_, C> {
        RawValues::new(&self.root, K::BITS)
    }
}

impl<C, K, V> Dict<C, K, V>
where
    for<'c> C: CellFamily + 'c,
    K: Store<C> + DictKey,
    V: Store<C>,
{
    /// Sets the value associated with the key in the dictionary.
    pub fn set_ext<Q, T>(
        &mut self,
        key: Q,
        value: T,
        finalizer: &mut dyn Finalizer<C>,
    ) -> Result<(), Error>
    where
        Q: Borrow<K>,
        T: Borrow<V>,
    {
        self.insert_impl(key.borrow(), value.borrow(), SetMode::Set, finalizer)
    }

    /// Sets the value associated with the key in the dictionary
    /// only if the key was already present in it.
    pub fn replace_ext<Q, T>(
        &mut self,
        key: Q,
        value: T,
        finalizer: &mut dyn Finalizer<C>,
    ) -> Result<(), Error>
    where
        Q: Borrow<K>,
        T: Borrow<V>,
    {
        self.insert_impl(key.borrow(), value.borrow(), SetMode::Replace, finalizer)
    }

    /// Sets the value associated with key in dictionary,
    /// but only if it is not already present.
    pub fn add_ext<Q, T>(
        &mut self,
        key: Q,
        value: T,
        finalizer: &mut dyn Finalizer<C>,
    ) -> Result<(), Error>
    where
        Q: Borrow<K>,
        T: Borrow<V>,
    {
        self.insert_impl(key.borrow(), value.borrow(), SetMode::Add, finalizer)
    }

    fn insert_impl(
        &mut self,
        key: &K,
        value: &V,
        mode: SetMode,
        finalizer: &mut dyn Finalizer<C>,
    ) -> Result<(), Error>
    where
        for<'c> C: CellFamily + 'c,
        K: Store<C> + DictKey,
        V: Store<C>,
    {
        let key = ok!(serialize_entry(key, finalizer));
        let value = ok!(serialize_entry(value, finalizer));
        self.root = ok!(dict_insert(
            &self.root,
            &mut key.as_ref().as_slice(),
            K::BITS,
            &value.as_ref().as_slice(),
            mode,
            finalizer
        ));
        Ok(())
    }
}

/// An iterator over the entries of a [`Dict`].
///
/// This struct is created by the [`iter`] method on [`Dict`]. See its documentation for more.
///
/// [`iter`]: Dict::iter
pub struct Iter<'a, C: CellFamily, K, V> {
    inner: RawIter<'a, C>,
    _key: PhantomData<K>,
    _value: PhantomData<V>,
}

impl<C: CellFamily, K, V> Clone for Iter<'_, C, K, V> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            _key: PhantomData,
            _value: PhantomData,
        }
    }
}

impl<'a, C: CellFamily, K, V> Iter<'a, C, K, V>
where
    K: DictKey,
{
    /// Creates an iterator over the entries of a dictionary.
    pub fn new(root: &'a Option<CellContainer<C>>) -> Self {
        Self {
            inner: RawIter::new(root, K::BITS),
            _key: PhantomData,
            _value: PhantomData,
        }
    }
}

impl<'a, C, K, V> Iterator for Iter<'a, C, K, V>
where
    for<'c> C: DefaultFinalizer + 'c,
    K: DictKey,
    V: Load<'a, C>,
{
    type Item = Result<(K, V), Error>;

    fn next(&mut self) -> Option<Self::Item> {
        Some(match self.inner.next()? {
            Ok((key, mut value)) => {
                if let Some(key) = K::from_raw_data(key.raw_data()) {
                    if let Some(value) = V::load_from(&mut value) {
                        return Some(Ok((key, value)));
                    }
                }
                Err(self.inner.finish(Error::CellUnderflow))
            }
            Err(e) => Err(e),
        })
    }
}

/// An iterator over the keys of a [`Dict`].
///
/// This struct is created by the [`keys`] method on [`Dict`]. See its
/// documentation for more.
///
/// [`keys`]: Dict::keys
pub struct Keys<'a, C: CellFamily, K> {
    inner: RawIter<'a, C>,
    _key: PhantomData<K>,
}

impl<'a, C: CellFamily, K> Clone for Keys<'a, C, K> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            _key: PhantomData,
        }
    }
}

impl<'a, C: CellFamily, K> Keys<'a, C, K>
where
    K: DictKey,
{
    /// Creates an iterator over the keys of a dictionary.
    pub fn new(root: &'a Option<CellContainer<C>>) -> Self {
        Self {
            inner: RawIter::new(root, K::BITS),
            _key: PhantomData,
        }
    }
}

impl<'a, C, K> Iterator for Keys<'a, C, K>
where
    for<'c> C: DefaultFinalizer + 'c,
    K: DictKey,
{
    type Item = Result<K, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        Some(match self.inner.next()? {
            Ok((key, _)) => match K::from_raw_data(key.raw_data()) {
                Some(key) => Ok(key),
                None => Err(self.inner.finish(Error::CellUnderflow)),
            },
            Err(e) => Err(e),
        })
    }
}

/// An iterator over the values of a [`Dict`].
///
/// This struct is created by the [`values`] method on [`Dict`]. See its documentation for more.
///
/// [`values`]: Dict::values
pub struct Values<'a, C: CellFamily, V> {
    inner: RawValues<'a, C>,
    _value: PhantomData<V>,
}

impl<C: CellFamily, V> Clone for Values<'_, C, V> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            _value: PhantomData,
        }
    }
}

impl<'a, C: CellFamily, V> Values<'a, C, V> {
    /// Creates an iterator over the values of a dictionary.
    pub fn new(root: &'a Option<CellContainer<C>>, bit_len: u16) -> Self {
        Self {
            inner: RawValues::new(root, bit_len),
            _value: PhantomData,
        }
    }
}

impl<'a, C, V> Iterator for Values<'a, C, V>
where
    for<'c> C: CellFamily + 'c,
    V: Load<'a, C>,
{
    type Item = Result<V, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.inner.next()? {
            Ok(mut value) => match V::load_from(&mut value) {
                Some(value) => Some(Ok(value)),
                None => Some(Err(self.inner.finish(Error::CellUnderflow))),
            },
            Err(e) => Some(Err(e)),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{RcBoc, RcCellFamily};

    #[test]
    fn dict_set() {
        let mut dict = Dict::<RcCellFamily, u32, u16>::new();
        dict.set(123, 0xffff).unwrap();
        assert_eq!(dict.get(123).unwrap(), Some(0xffff));

        dict.set(123, 0xcafe).unwrap();
        assert_eq!(dict.get(123).unwrap(), Some(0xcafe));
    }

    #[test]
    fn dict_set_complex() {
        let mut dict = Dict::<RcCellFamily, u32, bool>::new();
        for i in 0..520 {
            dict.set(i, true).unwrap();
        }
    }

    #[test]
    fn dict_replace() {
        let mut dict = Dict::<RcCellFamily, u32, bool>::new();
        dict.replace(123, false).unwrap();
        assert!(!dict.contains_key(123).unwrap());

        dict.set(123, false).unwrap();
        assert_eq!(dict.get(123).unwrap(), Some(false));
        dict.replace(123, true).unwrap();
        assert_eq!(dict.get(123).unwrap(), Some(true));
    }

    #[test]
    fn dict_add() {
        let mut dict = Dict::<RcCellFamily, u32, bool>::new();

        dict.add(123, false).unwrap();
        assert_eq!(dict.get(123).unwrap(), Some(false));

        dict.add(123, true).unwrap();
        assert_eq!(dict.get(123).unwrap(), Some(false));
    }

    #[test]
    fn dict_iter() {
        let boc = RcBoc::decode_base64("te6ccgEBFAEAeAABAcABAgPOQAUCAgHUBAMACQAAAI3gAAkAAACjoAIBIA0GAgEgCgcCASAJCAAJAAAAciAACQAAAIfgAgEgDAsACQAAAFZgAAkAAABsIAIBIBEOAgEgEA8ACQAAADqgAAkAAABQYAIBIBMSAAkAAAAe4AAJAAAAv2A=").unwrap();
        let dict = boc.parse::<Dict<_, u32, u32>>().unwrap();

        let size = dict.values().count();
        assert_eq!(size, 10);

        for (i, entry) in dict.iter().enumerate() {
            let (key, _) = entry.unwrap();
            assert_eq!(key, i as u32);
        }
    }
}