hash_cons 0.2.0

A type-safe hash-cons library.
Documentation
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
#[cfg(not(feature = "single-threaded"))]
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, RwLock, Weak};

/// # `Hc<T>`
/// A thread-safe custom smart pointer type for managing the lifecycle of consed values.
///
/// ## Type Parameters
/// * `T` - The type of values managed by this smart pointer. Must implement `Hash` and `Eq`.
///
/// ## Fields
/// * `inner`: `Arc<Inner<T>>` - Atomically reference counted pointer to the inner value.
///
/// ## Example
/// ```
/// use hash_cons::Hc;
/// use hash_cons::HcTable;
///
/// let table = HcTable::new();
/// let hc_pointer = table.hashcons(42);
///
/// assert_eq!(*hc_pointer.get(), 42);
/// ```
pub struct Hc<T>
where
    T: Hash + Eq,
{
    // This is the reference to the underlying value.
    inner: Arc<Inner<T>>,
}

// Implementing the traits for the custom smart pointer type.
impl<T> Hc<T>
where
    T: Hash + Eq,
{
    /// Retrieves a reference to the value stored in this `Hc<T>`.
    ///
    /// ## Returns
    /// A reference to the stored value.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    ///
    /// let table = HcTable::new();
    /// let my_value = table.hashcons(10);
    ///
    /// assert_eq!(*my_value.get(), 10);
    /// ```
    // Retrieve the inner value
    pub fn get(&self) -> &T {
        &self.inner.elem
    }
}

impl<T: PartialEq> PartialEq for Hc<T>
where
    T: Hash + Eq,
{
    /// Provides the functionality to compare two `Hc<T>` instances for equality.
    ///
    /// ## Parameters
    /// * `other`: Another `Hc<T>` instance to compare with.
    ///
    /// ## Returns
    /// `true` if the two instances are equal, `false` otherwise.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    ///
    /// let table = HcTable::new();
    /// let value1 = table.hashcons(5);
    /// let value2 = table.hashcons(5);
    /// let value3 = table.hashcons(10);
    ///
    /// assert_eq!(value1, value2);
    /// assert_ne!(value1, value3);
    /// ```
    fn eq(&self, other: &Self) -> bool {
        self.inner.elem == other.inner.elem
    }
}

impl<T> Eq for Hc<T> where T: Hash + Eq {}

impl<T> Hash for Hc<T>
where
    T: Hash + Eq,
{
    /// Provides the functionality to hash `Hc<T>` instances.
    /// This method is useful for storing `Hc<T>` instances in a `HashMap`.
    /// It is also used internally by the `HcTable` to manage the storage of
    /// `Hc<T>` instances.
    ///
    /// ## Parameters
    /// * `state`: The `Hasher` instance to use for hashing.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    /// use std::collections::hash_map::DefaultHasher;
    /// use std::hash::{Hash, Hasher};
    ///
    /// let table = HcTable::new();
    /// let value = table.hashcons(5);
    ///
    /// let mut hasher = DefaultHasher::new();
    /// value.hash(&mut hasher);
    /// let hash = hasher.finish();
    /// ```
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.inner.elem.hash(state);
    }
}

impl<T> Clone for Hc<T>
where
    T: Hash + Eq,
{
    /// Provides the functionality to clone `Hc<T>` instances.
    ///
    /// ## Returns
    /// A new `Hc<T>` instance with the same value as the original.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    ///
    /// let table = HcTable::new();
    /// let value = table.hashcons(5);
    /// let value_clone = value.clone();
    ///
    /// assert_eq!(value, value_clone);
    /// ```
    ///
    /// ## Note
    /// This method is implemented using `Arc::clone()`.
    /// This method does not actually clone the underlying value. Instead, it
    /// creates a new `Hc<T>` instance that points to the same value.
    /// This is the desired behavior for hash consing.
    /// If you need to clone the underlying value, you can use the `get()` method
    /// to retrieve a reference to the value and clone it.
    ///
    fn clone(&self) -> Self {
        Hc {
            inner: self.inner.clone(),
        }
    }
}

impl<T: std::fmt::Debug> std::fmt::Debug for Hc<T>
where
    T: Hash + Eq,
{
    /// Provides the functionality to print `Hc<T>` instances.
    /// This method is useful for debugging.
    /// ## Parameters
    /// * `f`: The `Formatter` instance to use for printing.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    ///
    /// let table = HcTable::new();
    /// let value = table.hashcons(5);
    ///
    /// println!("{:?}", value);
    /// ```
    ///
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.inner.elem.fmt(f)
    }
}

impl<T: std::fmt::Display> std::fmt::Display for Hc<T>
where
    T: Hash + Eq,
{
    /// Provides the functionality to print `Hc<T>` instances.
    /// This method is useful for debugging.
    ///
    /// ## Parameters
    /// * `f`: The `Formatter` instance to use for printing.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    ///
    /// let table = HcTable::new();
    /// let value = table.hashcons(5);
    ///
    /// println!("{}", value);
    /// ```
    ///
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.inner.elem.fmt(f)
    }
}

impl<T> std::ops::Deref for Hc<T>
where
    T: Hash + Eq,
{
    type Target = T;

    /// Provides the functionality to dereference `Hc<T>` instances.
    /// This method is useful for accessing the underlying value.
    ///
    /// ## Returns
    /// A reference to the underlying value.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    /// let table = HcTable::new();
    /// let value = table.hashcons(5);
    /// assert_eq!(*value, 5);
    /// ```
    ///
    /// ## Note
    /// This method is implemented using `Arc::deref()`.
    /// This method does not actually dereference the underlying value. Instead, it
    /// returns a reference to the value.
    /// This is the desired behavior for hash consing.
    /// If you need to dereference the underlying value, you can use the `get()` method
    /// to retrieve a reference to the value.
    ///

    fn deref(&self) -> &Self::Target {
        &self.inner.elem
    }
}

impl<T> AsRef<T> for Hc<T>
where
    T: Hash + Eq,
{
    /// Provides the functionality to convert `Hc<T>` instances to references.
    /// This method is useful for accessing the underlying value.
    ///
    /// ## Returns
    /// A reference to the underlying value.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    ///
    /// let table = HcTable::new();
    /// let value = table.hashcons(5);
    ///
    /// assert_eq!(value.as_ref(), &5);
    /// ```
    ///
    /// ## Note
    /// This method is implemented using `Arc::as_ref()`.
    /// This method does not actually convert the underlying value to a reference. Instead, it
    /// returns a reference to the value.
    /// This is the desired behavior for hash consing.
    /// If you need to convert the underlying value to a reference, you can use the `get()` method
    /// to retrieve a reference to the value.
    ///
    fn as_ref(&self) -> &T {
        &self.inner.elem
    }
}

impl<T: PartialOrd> PartialOrd for Hc<T>
where
    T: Hash + Eq,
{
    /// Provides the functionality to compare two `Hc<T>` instances.
    /// This method is useful for sorting `Hc<T>` instances.
    /// ## Parameters
    /// * `other`: Another `Hc<T>` instance to compare with.
    /// ## Returns
    /// `Some(std::cmp::Ordering)` if the two instances are comparable, `None` otherwise.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    ///
    /// let table = HcTable::new();
    /// let value1 = table.hashcons(5);
    /// let value2 = table.hashcons(10);
    ///
    /// assert!(value1 < value2);
    /// ```
    ///
    /// ## Note
    /// This method is implemented using `Arc::partial_cmp()`.
    /// This method does not actually compare the underlying values. Instead, it
    /// compares the `Hc<T>` instances.
    ///
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.inner.elem.partial_cmp(&other.inner.elem)
    }
}

impl<T> Ord for Hc<T>
where
    T: Ord + Hash + Eq,
{
    /// Provides the functionality to compare two `Hc<T>` instances.
    /// This method is useful for sorting `Hc<T>` instances.
    /// ## Parameters
    /// * `other`: Another `Hc<T>` instance to compare with.
    /// ## Returns
    /// `std::cmp::Ordering` if the two instances are comparable.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    ///
    /// let table = HcTable::new();
    /// let value1 = table.hashcons(5);
    /// let value2 = table.hashcons(10);
    ///
    /// assert!(value1 < value2);
    /// ```
    ///
    /// ## Note
    /// This method is implemented using `Arc::cmp()`.
    /// This method does not actually compare the underlying values. Instead, it
    /// compares the `Hc<T>` instances.
    ///
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.inner.elem.cmp(&other.inner.elem)
    }
}

///  # `HcTable<T>`
/// A table structure for efficiently managing `Hc<T>` instances.
/// This struct hides the underlying table and its reference count management.
///
/// This structure utilizes a HashMap to store `Hc<T>` instances, offering
/// quick retrieval and management capabilities.
///
/// ## Type Parameters
/// * `T` - The type of values managed by the `Hc<T>` instances within this table.
///
/// ## Fields
/// * `inner`: HashMap - The underlying data structure storing `Hc<T>` instances.
///
pub struct HcTable<T>
where
    T: Hash + Eq,
{
    inner: Arc<InnerTable<T>>,
}

// Implementing the traits for the custom smart pointer type.
impl<T> HcTable<T>
where
    T: Hash + Eq,
{
    /// Creates a new `HcTable`.
    ///
    /// ## Returns
    /// A new instance of `HcTable<T>`.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    ///
    /// let table: HcTable<i32> = HcTable::new();
    /// ```
    pub fn new() -> Self {
        HcTable {
            inner: Arc::new(InnerTable::new()),
        }
    }

    /// Simplifies object retrieval or creation with an intuitive interface.
    ///
    /// ## Parameters
    /// * `value`: The value to be managed.
    ///
    /// ## Returns
    /// A `Hc<T>` instance corresponding to the given value.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    ///
    /// let table = HcTable::new();
    /// let value = table.hashcons(5);
    /// ```
    ///
    pub fn hashcons(&self, value: T) -> Hc<T> {
        Hc {
            inner: self.intern(value),
        }
    }

    /// Internal method to manage the storage of values in `HcTable`.
    /// It ensures that each value is stored only once, providing a shared
    /// reference to the stored value.
    ///
    /// ## Parameters
    /// * `value`: The value to be stored or retrieved.
    ///
    /// ## Returns
    /// A `Arc<Inner<T>>` pointer to the stored value.
    ///
    ///
    fn intern(&self, value: T) -> Arc<Inner<T>> {
        let arc_table = self.inner.clone();
        let arc_table_dup = arc_table.clone();

        let mut_table_result = arc_table_dup.table.write();

        let mut mut_table = match mut_table_result {
            Ok(guard) => guard,
            Err(poisoned) => {
                eprintln!("Mutex is poisoned. Continuing with the poisoned lock.");
                poisoned.into_inner() // continues, because we will add a new value
            }
        };

        let rc_value = Arc::new(value);
        let rc_val_dup = rc_value.clone();

        match mut_table.entry(rc_val_dup) {
            Entry::Occupied(mut o) => {
                let weak_hc = o.get();

                if let Some(rc_hc) = weak_hc.upgrade() {
                    return rc_hc;
                }

                let elem = rc_value;

                let _table = arc_table;
                let new_elem = Arc::new(Inner { elem, _table });
                o.insert(Arc::downgrade(&new_elem));
                new_elem
            }

            Entry::Vacant(v) => {
                let _table = arc_table;
                let elem = rc_value;
                let new_elem = Arc::new(Inner { elem, _table });
                v.insert(Arc::downgrade(&new_elem));
                new_elem
            }
        }
    }

    #[cfg(not(feature = "auto-cleanup"))]
    /// Cleans up the `HcTable`, removing any values that are no longer in use.
    /// This method is useful for managing memory and ensuring that unused
    /// values are not unnecessarily kept in the table.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    ///
    /// let table = HcTable::new();
    /// let value = table.hashcons(5);
    ///
    /// drop(value);
    /// table.cleanup();
    /// ```
    ///
    pub fn cleanup(&self) {
        self.inner.cleanup();
    }

    /// Returns the number of elements currently stored in the `HcTable`.
    ///
    /// ## Returns
    /// The number of elements in the `HcTable`.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    ///
    /// let table = HcTable::new();
    /// let value = table.hashcons(5);
    ///
    /// assert_eq!(table.len(), 1);
    /// ```
    ///
    pub fn len(&self) -> usize {
        self.inner.len()
    }
}

impl<T> Clone for HcTable<T>
where
    T: Hash + Eq,
{
    /// Provides the functionality to clone `HcTable<T>` instances.
    ///
    /// ## Returns
    /// A new `HcTable<T>` instance with the same values as the original.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    ///
    /// let table = HcTable::new();
    /// let value = table.hashcons(5);
    /// let table_clone = table.clone();
    ///
    /// assert_eq!(table.len(), table_clone.len());
    /// ```
    ///
    fn clone(&self) -> Self {
        HcTable {
            inner: self.inner.clone(),
        }
    }
}

/// # Inner<T>
/// A struct to encapsulate the inner workings of `Hc<T>`.
/// It holds the actual value and a weak reference to its containing table.
///
/// ## Type Parameters
/// * `T` - The type of the encapsulated value.
///
/// ## Fields
/// * `elem`: The actual stored value.
/// * `_table`: An atomic reference counted pointer to the `HcTable` that contains this value.
///
struct Inner<T>
where
    T: Hash + Eq,
{
    elem: Arc<T>,

    _table: Arc<InnerTable<T>>,
}

#[cfg(feature = "auto-cleanup")]
impl<T> Drop for Inner<T>
where
    T: Hash + Eq,
{
    /// Provides the functionality to drop `Inner<T>` instances.
    /// This method is useful for managing the lifecycle of `Hc<T>` instances.
    ///
    /// ## Example
    /// ```
    /// use hash_cons::HcTable;
    ///
    /// let table = HcTable::new();
    /// let value = table.hashcons(5);
    ///
    /// drop(value);
    /// assert_eq!(table.len(), 0);
    /// ```
    ///
    fn drop(&mut self) {
        let arc_table = self._table.clone();

        let key = self.elem.clone();
        let mut_table_result = arc_table.table.write();
        let mut mut_table = match mut_table_result {
            Ok(guard) => guard,
            Err(poisoned) => {
                eprintln!("Mutex is poisoned. Continuing with the poisoned lock.");
                poisoned.into_inner() // continues, because we are not using
                                      // any inconsistent value(if any)
            }
        };
        mut_table.remove_entry(&key);
    }
}

/// # InnerTable<T>
/// A helper struct to manage the internal storage of `HcTable`.
/// It provides mechanisms to manage and access stored `Hc<T>` instances.
///
/// ## Type Parameters
/// * `T` - The type of values stored in the `HcTable`.
///
/// ## Fields
/// * `table`: The actual HashMap that stores the `Hc<T>` instances.
///
struct InnerTable<T>
where
    T: Hash + Eq,
{
    table: RwLock<HashMap<Arc<T>, Weak<Inner<T>>>>,
}

impl<T> InnerTable<T>
where
    T: Hash + Eq,
{
    /// Creates a new `InnerTable<T>`.
    ///
    /// ## Returns
    /// A new instance of `InnerTable<T>`.
    ///
    fn new() -> Self {
        InnerTable {
            table: RwLock::new(HashMap::new()),
        }
    }

    /// Returns the number of elements currently stored in the `InnerTable`.
    ///
    /// ## Returns
    /// The number of elements in the `InnerTable`.
    ///
    fn len(&self) -> usize {
        let table_result = self.table.read();
        let table = match table_result {
            Ok(guard) => guard,
            Err(poisoned) => {
                eprintln!("Mutex is poisoned. Continuing with the poisoned lock.");
                poisoned.into_inner() // continues, because we don't need the value(even if inconsistent)
            }
        };
        table.len()
    }

    #[cfg(not(feature = "auto-cleanup"))]
    /// Cleans up the `InnerTable`, removing any values that are no longer in use.
    /// This method is useful for managing memory and ensuring that unused
    /// values are not unnecessarily kept in the table.
    ///
    /// ## Note
    /// This method is implemented using `Weak::strong_count()`.
    /// It removes any values that have a `strong_count()` of 0.
    /// This is the desired behavior for hash consing.
    ///
    fn cleanup(&self) {
        loop {
            let mut_table_result = self.table.write();

            let mut mut_table = match mut_table_result {
                Ok(guard) => guard,
                Err(poisoned) => {
                    eprintln!("Mutex is poisoned. Continuing with the poisoned lock.");
                    poisoned.into_inner() // continues, because we are removing the value
                }
            };

            // Flag to check if any weak references are dropped in this iteration
            let mut dropped = false;

            mut_table.retain(|_, weak_hc: &mut Weak<Inner<T>>| {
                if weak_hc.strong_count() == 0 {
                    dropped = true; // A weak reference was dropped
                    false // Remove this entry
                } else {
                    true // Keep this entry
                }
            });

            // Break the loop if no weak references were dropped in this iteration
            if !dropped {
                break;
            }
        }
    }

    /*fn cleanup(&self) {
        let mut_table_result = self.table.write();

        let mut mut_table = match mut_table_result {
            Ok(guard) => guard,
            Err(poisoned) => {
                eprintln!("Mutex is poisoned. Continuing with the poisoned lock.");
                poisoned.into_inner() // continues, because we are removing the value
            }
        };

        mut_table.retain(|_, weak_Hc: &mut Weak<Inner<T>>| weak_Hc.strong_count() > 0);
    }*/
}