simple-sds-sbwt 0.3.3

A fork of simple-sds used in the sbwt crate.
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//! A bit-packed integer vector storing fixed-width integers.

use crate::ops::{Vector, Resize, Pack, Access, AccessIter, Push, Pop};
use crate::raw_vector::{RawVector, RawVectorWriter, AccessRaw, PushRaw, PopRaw};
#[cfg(not(target_family = "wasm"))]
use crate::raw_vector::RawVectorMapper;
use crate::serialize::Serialize;
#[cfg(not(target_family = "wasm"))]
use crate::serialize::{MemoryMap, MemoryMapped};
use crate::bits;

use std::io::{Error, ErrorKind};
use std::iter::{FusedIterator, FromIterator};
use std::path::Path;
use std::io;

#[cfg(test)]
mod tests;

//-----------------------------------------------------------------------------

/// A contiguous growable and mutable bit-packed array of fixed-width integers.
///
/// This structure contains [`RawVector`], which is in turn contains [`Vec`].
/// Each item consists of the lowest 1 to 64 bits of a [`u64`] value, as specified by parameter `width`.
/// The maximum length of the vector is `usize::MAX / width` items.
///
/// A default constructed `IntVector` has `width == 64`.
/// `IntVector` can be built from an iterator over [`u8`], [`u16`], [`u32`], [`u64`], or [`usize`] values.
///
/// `IntVector` implements the following `simple_sds` traits:
/// * Basic functionality: [`Vector`], [`Resize`], [`Pack`]
/// * Queries and operations: [`Access`], [`Push`], [`Pop`]
/// * Serialization: [`Serialize`]
///
/// # Notes
///
/// * `IntVector` never panics from I/O errors.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IntVector {
    len: usize,
    width: usize,
    data: RawVector,
}

impl IntVector {
    /// Creates an empty vector with specified width.
    /// 
    /// Returns [`Err`] if the width is invalid.
    ///
    /// # Examples
    ///
    /// ```
    /// use simple_sds_sbwt::int_vector::IntVector;
    /// use simple_sds_sbwt::ops::Vector;
    ///
    /// let v = IntVector::new(13).unwrap();
    /// assert!(v.is_empty());
    /// assert_eq!(v.width(), 13);
    /// ```
    pub fn new(width: usize) -> Result<IntVector, &'static str> {
        if width == 0 || width > bits::WORD_BITS {
            Err("Integer width must be 1 to 64 bits")
        }
        else {
            Ok(IntVector {
                len: 0,
                width,
                data: RawVector::new(),
            })
        }
    }

    /// Creates an initialized vector of specified length and width.
    /// 
    /// Returns [`Err`] if the width is invalid.
    ///
    /// # Arguments
    ///
    /// * `len`: Number of items in the vector.
    /// * `width`: Width of each item in bits.
    /// * `value`: Initialization value.
    ///
    /// # Examples
    ///
    /// ```
    /// use simple_sds_sbwt::int_vector::IntVector;
    /// use simple_sds_sbwt::ops::{Vector, Access};
    ///
    /// let v = IntVector::with_len(4, 13, 1234).unwrap();
    /// assert_eq!(v.len(), 4);
    /// assert_eq!(v.width(), 13);
    /// for i in 0..v.len() {
    ///     assert_eq!(v.get(i), 1234);
    /// }
    /// ```
    ///
    /// # Panics
    ///
    /// May panic if the vector would exceed the maximum length.
    pub fn with_len(len: usize, width: usize, value: u64) -> Result<IntVector, &'static str> {
        if width == 0 || width > bits::WORD_BITS {
            return Err("Integer width must be 1 to 64 bits");
        }
        let mut data = RawVector::with_capacity(len * width);
        for _ in 0..len {
            unsafe { data.push_int(value, width); }
        }
        Ok(IntVector {
            len, width, data,
        })
    }

    /// Creates an empty vector with enough capacity for at least the specified number of items of specified width.
    ///
    /// Returns [`Err`] if the width is invalid.
    ///
    /// # Arguments
    ///
    /// * `capacity`: Minimum capacity of the vector in items.
    /// * `width`: Width of each item in bits.
    ///
    /// # Examples
    ///
    /// ```
    /// use simple_sds_sbwt::int_vector::IntVector;
    /// use simple_sds_sbwt::ops::{Vector, Resize};
    ///
    /// let v = IntVector::with_capacity(4, 13).unwrap();
    /// assert!(v.is_empty());
    /// assert_eq!(v.width(), 13);
    /// assert!(v.capacity() >= 4);
    /// ```
    ///
    /// # Panics
    ///
    /// May panic if the capacity would exceed the maximum length.
    pub fn with_capacity(capacity: usize, width: usize) -> Result<IntVector, &'static str> {
        if width == 0 || width > bits::WORD_BITS {
            Err("Integer width must be 1 to 64 bits")
        } else {
            Ok(IntVector {
                len: 0,
                width,
                data: RawVector::with_capacity(capacity * width),
            })
        }
    }

    /// Returns the size of a serialized vector with the given parameters in [`u64`] elements.
    ///
    /// # Arguments
    ///
    /// * `capacity`: Minimum capacity of the vector in items.
    /// * `width`: Width of each item in bits.
    ///
    /// # Examples
    ///
    /// ```
    /// use simple_sds_sbwt::int_vector::IntVector;
    ///
    /// assert_eq!(IntVector::size_by_params(12, 31), 10);
    /// ```
    ///
    /// # Panics
    ///
    /// May panic if the vector would exceed the maximum length.
    pub fn size_by_params(capacity: usize, width: usize) -> usize {
        2 + RawVector::size_by_params(capacity * width)
    }
}

//-----------------------------------------------------------------------------

impl Vector for IntVector {
    type Item = u64;

    #[inline]
    fn len(&self) -> usize {
        self.len
    }

    #[inline]
    fn width(&self) -> usize {
        self.width
    }

    #[inline]
    fn max_len(&self) -> usize {
        usize::MAX / self.width()
    }
}

impl Resize for IntVector {
    fn resize(&mut self, new_len: usize, value: <Self as Vector>::Item) {
        match new_len {
            new_len if new_len > self.len() => {
                self.reserve(new_len - self.len());
                while self.len() < new_len {
                    self.push(value);
                }
            },
            new_len if new_len < self.len() => {
                self.data.resize(new_len * self.width(), false);
                self.len = new_len;
            },
            _ => (),
        }
    }

    fn clear(&mut self) {
        self.data.clear();
        self.len = 0;
    }

    #[inline]
    fn capacity(&self) -> usize {
        self.data.capacity() / self.width()
    }

    fn reserve(&mut self, additional: usize) {
        self.data.reserve(additional * self.width());
    }
}

impl Pack for IntVector {
    fn pack(&mut self) {
        if self.is_empty() {
            return;
        }
        let new_width = bits::bit_len(self.iter().max().unwrap());
        if new_width == self.width() {
            return;
        }
        let mut new_data = RawVector::with_capacity(self.len() * new_width);
        for value in self.iter() {
            unsafe { new_data.push_int(value, new_width); }
        }
        self.width = new_width;
        self.data = new_data;
    }
}

impl<'a> Access<'a> for IntVector {
    type Iter = AccessIter<'a, Self>;

    #[inline]
    fn get(&self, index: usize) -> <Self as Vector>::Item {
        assert!(index < self.len(), "Index is out of bounds");
        unsafe { self.data.int(index * self.width(), self.width()) }
    }

    fn iter(&'a self) -> Self::Iter {
        Self::Iter::new(self)
    }

    #[inline]
    fn is_mutable(&self) -> bool {
        true
    }

    #[inline]
    fn set(&mut self, index: usize, value: <Self as Vector>::Item) {
        assert!(index < self.len(), "Index is out of bounds");
        unsafe { self.data.set_int(index * self.width(), value, self.width()); }
    }
}

impl Push for IntVector {
    #[inline]
    fn push(&mut self, value: <Self as Vector>::Item) {
        unsafe { self.data.push_int(value, self.width()); }
        self.len += 1;
    }    
}

impl Pop for IntVector {
    #[inline]
    fn pop(&mut self) -> Option<<Self as Vector>::Item> {
        if self.len() > 0 {
            self.len -= 1;
        }
        unsafe { self.data.pop_int(self.width()) }
    }    
}

impl Serialize for IntVector {
    fn serialize_header<T: io::Write>(&self, writer: &mut T) -> io::Result<()> {
        self.len.serialize(writer)?;
        self.width.serialize(writer)?;
        self.data.serialize_header(writer)?;
        Ok(())
    }

    fn serialize_body<T: io::Write>(&self, writer: &mut T) -> io::Result<()> {
        self.data.serialize_body(writer)?;
        Ok(())
    }

    fn load<T: io::Read>(reader: &mut T) -> io::Result<Self> {
        let len = usize::load(reader)?;
        let width = usize::load(reader)?;
        let data = RawVector::load(reader)?;
        if len * width != data.len() {
            Err(Error::new(ErrorKind::InvalidData, "Data length does not match len * width"))
        }
        else {
            Ok(IntVector {
                len, width, data,
            })
        }
    }

    fn size_in_elements(&self) -> usize {
        self.len.size_in_elements() + self.width.size_in_elements() + self.data.size_in_elements()
    }
}

//-----------------------------------------------------------------------------

impl Default for IntVector {
    fn default() -> Self {
        IntVector {
            len: 0,
            width: bits::WORD_BITS,
            data: RawVector::new(),
        }
    }
}

impl AsRef<RawVector> for IntVector {
    #[inline]
    fn as_ref(&self) -> &RawVector {
        &(self.data)
    }
}

impl From<IntVector> for RawVector {
    fn from(source: IntVector) -> Self {
        source.data
    }
}

//-----------------------------------------------------------------------------

/// [`IntVector`] transformed into an iterator.
///
/// The type of `Item` is [`u64`].
///
/// # Examples
///
/// ```
/// use simple_sds_sbwt::int_vector::IntVector;
///
/// let source: Vec<u64> = vec![1, 3, 15, 255, 65535];
/// let v: IntVector = source.iter().cloned().collect();
/// let target: Vec<u64> = v.into_iter().collect();
/// assert_eq!(target, source);
/// ```
#[derive(Clone, Debug)]
pub struct IntoIter {
    parent: IntVector,
    index: usize,
}

impl Iterator for IntoIter {
    type Item = <IntVector as Vector>::Item;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index >= self.parent.len() {
            None
        } else {
            let result = Some(self.parent.get(self.index));
            self.index += 1;
            result
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.parent.len() - self.index;
        (remaining, Some(remaining))
    }
}

impl ExactSizeIterator for IntoIter {}

impl FusedIterator for IntoIter {}

impl IntoIterator for IntVector {
    type Item = <Self as Vector>::Item;
    type IntoIter = IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter {
            parent: self,
            index: 0,
        }
    }
}

//-----------------------------------------------------------------------------

/// A buffered file writer compatible with the serialization format of [`IntVector`].
///
/// When the writer goes out of scope, the internal buffer is flushed, the file is closed, and all errors are ignored.
/// Call [`IntVectorWriter::close`] explicitly to handle the errors.
///
/// # Examples
///
/// ```
/// use simple_sds_sbwt::int_vector::{IntVector, IntVectorWriter};
/// use simple_sds_sbwt::ops::{Vector, Access, Push};
/// use simple_sds_sbwt::serialize;
/// use std::fs;
///
/// let filename = serialize::temp_file_name("int-vector-writer");
/// let mut writer = IntVectorWriter::new(&filename, 13).unwrap();
/// assert!(writer.is_empty());
/// writer.push(123); writer.push(456); writer.push(789);
/// assert_eq!(writer.len(), 3);
/// writer.close().unwrap();
///
/// let v: IntVector = serialize::load_from(&filename).unwrap();
/// assert_eq!(v.len(), 3);
/// assert_eq!(v.get(0), 123); assert_eq!(v.get(1), 456); assert_eq!(v.get(2), 789);
/// fs::remove_file(&filename).unwrap();
/// ```
#[derive(Debug)]
pub struct IntVectorWriter {
    len: usize,
    width: usize,
    writer: RawVectorWriter,
}

impl IntVectorWriter {
    /// Creates an empty vector stored in the specified file with the default buffer size.
    ///
    /// If the file already exists, it will be overwritten.
    ///
    /// # Arguments
    ///
    /// * `filename`: Name of the file.
    /// * `width`: Width of each item in bits.
    ///
    /// # Errors
    ///
    /// Returns an error of the kind [`ErrorKind::Other`] if the width is invalid.
    /// Any I/O errors will be passed through.
    pub fn new<P: AsRef<Path>>(filename: P, width: usize) -> io::Result<IntVectorWriter> {
        if width == 0 || width > bits::WORD_BITS {
            return Err(Error::new(ErrorKind::Other, "Integer width must be 1 to 64 bits"));
        }
        // The header will contain `len` and `width`.
        let mut header: Vec<u64> = vec![0, 0];
        let writer = RawVectorWriter::new(filename, &mut header)?;
        let result = IntVectorWriter {
            len: 0,
            width,
            writer,
        };
        Ok(result)
    }

    /// Creates an empty vector stored in the specified file with user-defined buffer size.
    ///
    /// If the file already exists, it will be overwritten.
    /// Actual buffer size may be slightly higher than requested.
    ///
    /// # Arguments
    ///
    /// * `filename`: Name of the file.
    /// * `width`: Width of each item in bits.
    /// * `buf_len`: Buffer size in items.
    ///
    /// # Errors
    ///
    /// Returns an error of the kind [`ErrorKind::Other`] if the width is invalid.
    /// Any I/O errors will be passed through.
    ///
    /// # Panics
    ///
    /// May panic if buffer length would exceed the maximum length.
    pub fn with_buf_len<P: AsRef<Path>>(filename: P, width: usize, buf_len: usize) -> io::Result<IntVectorWriter> {
        if width == 0 || width > bits::WORD_BITS {
            return Err(Error::new(ErrorKind::Other, "Integer width must be 1 to 64 bits"));
        }
        // The header will contain `len` and `width`.
        let mut header: Vec<u64> = vec![0, 0];
        let writer = RawVectorWriter::with_buf_len(filename, &mut header, buf_len * width)?;
        let result = IntVectorWriter {
            len: 0,
            width,
            writer,
        };
        Ok(result)
    }

    /// Returns the name of the file.
    pub fn filename(&self) -> &Path {
        self.writer.filename()
    }

    /// Returns `true` if the file is open for writing.
    pub fn is_open(&self) -> bool {
        self.writer.is_open()
    }

    /// Flushes the buffer, writes the header, and closes the file.
    ///
    /// No effect if the file is closed.
    ///
    /// # Errors
    ///
    /// Any I/O errors will be passed through.
    pub fn close(&mut self) -> io::Result<()> {
        let mut header: Vec<u64> = vec![self.len as u64, self.width as u64];
        self.writer.close_with_header(&mut header)
    }
}

//-----------------------------------------------------------------------------

impl Vector for IntVectorWriter {
    type Item = u64;

    #[inline]
    fn len(&self) -> usize {
        self.len
    }

    #[inline]
    fn width(&self) -> usize {
        self.width
    }

    #[inline]
    fn max_len(&self) -> usize {
        usize::MAX / self.width()
    }
}

impl Push for IntVectorWriter {
    #[inline]
    fn push(&mut self, value: <Self as Vector>::Item) {
        unsafe { self.writer.push_int(value, self.width()); }
        self.len += 1;
    }
}

impl Drop for IntVectorWriter {
    fn drop(&mut self) {
        let _ = self.close();
    }
}

//-----------------------------------------------------------------------------

/// An immutable memory-mapped [`IntVector`].
///
/// This is compatible with the serialization format of [`IntVector`].
///
/// # Examples
///
/// ```
/// use simple_sds_sbwt::int_vector::{IntVector, IntVectorMapper};
/// use simple_sds_sbwt::ops::{Vector, Access};
/// use simple_sds_sbwt::serialize::{MemoryMap, MemoryMapped, MappingMode};
/// use simple_sds_sbwt::serialize;
/// use std::fs;
///
/// let filename = serialize::temp_file_name("int-vector-mapper");
/// let mut v = IntVector::with_len(3, 13, 0).unwrap();
/// v.set(0, 123); v.set(1, 456); v.set(2, 789);
/// serialize::serialize_to(&v, &filename);
///
/// let map = MemoryMap::new(&filename, MappingMode::ReadOnly).unwrap();
/// let mapper = IntVectorMapper::new(&map, 0).unwrap();
/// assert_eq!(mapper.len(), v.len());
/// for i in 0..mapper.len() {
///     assert_eq!(mapper.get(i), v.get(i));
/// }
///
/// drop(mapper); drop(map);
/// fs::remove_file(&filename).unwrap();
/// ```
#[cfg(not(target_family = "wasm"))]
#[derive(PartialEq, Eq, Debug)]
pub struct IntVectorMapper<'a> {
    len: usize,
    width: usize,
    data: RawVectorMapper<'a>,
}

#[cfg(not(target_family = "wasm"))]
impl<'a> Vector for IntVectorMapper<'a> {
    type Item = u64;

    #[inline]
    fn len(&self) -> usize {
        self.len
    }

    #[inline]
    fn width(&self) -> usize {
        self.width
    }

    #[inline]
    fn max_len(&self) -> usize {
        usize::MAX / self.width()
    }
}

#[cfg(not(target_family = "wasm"))]
impl<'a> Access<'a> for IntVectorMapper<'a> {
    type Iter = AccessIter<'a, Self>;

    #[inline]
    fn get(&self, index: usize) -> <Self as Vector>::Item {
        assert!(index < self.len(), "Index is out of bounds");
        unsafe { self.data.int(index * self.width(), self.width()) }
    }

    fn iter(&'a self) -> Self::Iter {
        Self::Iter::new(self)
    }
}

#[cfg(not(target_family = "wasm"))]
impl<'a> MemoryMapped<'a> for IntVectorMapper<'a> {
    fn new(map: &'a MemoryMap, offset: usize) -> io::Result<Self> {
        if offset + 1 >= map.len() {
            return Err(Error::new(ErrorKind::UnexpectedEof, "The starting offset is out of range"));
        }
        let slice: &[u64] = map.as_ref();
        let len = slice[offset] as usize;
        let width = slice[offset + 1] as usize;
        let data = RawVectorMapper::new(map, offset + 2)?;
        Ok(IntVectorMapper {
            len, width, data,
        })
    }

    fn map_offset(&self) -> usize {
        self.data.map_offset() - 2
    }

    fn map_len(&self) -> usize {
        self.data.map_len() + 2
    }
}

#[cfg(not(target_family = "wasm"))]
impl<'a> AsRef<RawVectorMapper<'a>> for IntVectorMapper<'a> {
    #[inline]
    fn as_ref(&self) -> &RawVectorMapper<'a> {
        &(self.data)
    }
}

//-----------------------------------------------------------------------------

macro_rules! from_extend_int_vector {
    ($t:ident, $w:expr) => {
        impl From<Vec<$t>> for IntVector {
            fn from(v: Vec<$t>) -> Self {
                let mut result = IntVector::with_capacity(v.len(), $w).unwrap();
                result.extend(v);
                result
            }
        }

        impl FromIterator<$t> for IntVector {
            fn from_iter<I: IntoIterator<Item = $t>>(iter: I) -> Self {
                let mut result = IntVector::new($w).unwrap();
                result.extend(iter);
                result
            }
        }

        impl Extend<$t> for IntVector {
            fn extend<I: IntoIterator<Item = $t>>(&mut self, iter: I) {
                let mut iter = iter.into_iter();
                let (lower_bound, _) = iter.size_hint();
                self.reserve(lower_bound);
                while let Some(value) = iter.next() {
                    self.push(value as <Self as Vector>::Item);
                }
            }
        }

        impl Extend<$t> for IntVectorWriter {
            fn extend<I: IntoIterator<Item = $t>>(&mut self, iter: I) {
                for value in iter {
                    self.push(value as <Self as Vector>::Item);
                }
            }
        }
    }
}

from_extend_int_vector!(u8, 8);
from_extend_int_vector!(u16, 16);
from_extend_int_vector!(u32, 32);
from_extend_int_vector!(u64, 64);
from_extend_int_vector!(usize, 64);

//-----------------------------------------------------------------------------