Skip to main content

FastOutput

Struct FastOutput 

Source
pub struct FastOutput<W>
where W: Write,
{ buf: Box<[u8]>, pos: usize, inner: W, }

Fields§

§buf: Box<[u8]>§pos: usize§inner: W

Implementations§

Source§

impl FastOutput<StdoutLock<'static>>

Source

pub fn stdout() -> Self

Source§

impl<W> FastOutput<W>
where W: Write,

Source

pub fn new(writer: W) -> Self

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 810)
809    pub fn stdout() -> Self {
810        Self::new(stdout().lock())
811    }
Source

pub fn with_capacity(capacity: usize, writer: W) -> Self

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 828)
827    pub fn new(writer: W) -> Self {
828        Self::with_capacity(1 << 18, writer)
829    }
Source

pub fn flush(&mut self)

Source

fn flush_buf(&mut self)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 840)
839    pub fn flush(&mut self) {
840        self.flush_buf();
841        self.inner.flush().unwrap();
842    }
843
844    #[cold]
845    fn flush_buf(&mut self) {
846        if self.pos != 0 {
847            self.inner.write_all(&self.buf[..self.pos]).unwrap();
848            self.pos = 0;
849        }
850    }
851
852    #[inline]
853    fn ensure_capacity(&mut self, capacity: usize) {
854        if self.buf.len() - self.pos < capacity {
855            self.flush_buf();
856        }
857    }
858
859    #[inline]
860    unsafe fn write_byte_unchecked(&mut self, byte: u8) {
861        unsafe {
862            *self.buf.as_mut_ptr().add(self.pos) = byte;
863        }
864        self.pos += 1;
865    }
866
867    #[inline]
868    unsafe fn write_digit4_unchecked(&mut self, x: usize) {
869        debug_assert!(x < 10000);
870        unsafe {
871            ptr::write_unaligned(
872                self.buf.as_mut_ptr().add(self.pos) as *mut u32,
873                ptr::read_unaligned((DIGIT4.as_ptr() as *const u8).add(4 * x) as *const u32),
874            );
875        }
876        self.pos += 4;
877    }
878
879    #[inline]
880    unsafe fn write_digit4_trimmed_unchecked(&mut self, x: usize) {
881        unsafe {
882            let word = *DIGIT4_TRIMMED.get_unchecked(x);
883            ptr::write_unaligned(
884                self.buf.as_mut_ptr().add(self.pos).cast::<u32>(),
885                (word & 0x3fffffff).to_le(),
886            );
887            self.pos += (word >> 30) as usize + 1;
888        }
889    }
890
891    #[inline]
892    unsafe fn write_u8_unchecked(&mut self, x: u8) {
893        let off = (x < 10) as usize + (x < 100) as usize + 1;
894        unsafe {
895            ptr::write_unaligned(
896                self.buf.as_mut_ptr().add(self.pos) as *mut u32,
897                ptr::read_unaligned(
898                    (DIGIT4.as_ptr() as *const u8).add(4 * x as usize + off) as *const u32
899                ),
900            );
901        }
902        self.pos += 4 - off;
903    }
904
905    #[inline]
906    unsafe fn write_u16_unchecked(&mut self, x: u16) {
907        unsafe {
908            if x >= 10000 {
909                self.write_digit4_trimmed_unchecked((x / 10000) as usize);
910                self.write_digit4_unchecked((x % 10000) as usize);
911            } else {
912                self.write_digit4_trimmed_unchecked(x as usize);
913            }
914        }
915    }
916
917    #[inline]
918    unsafe fn write_u32_unchecked(&mut self, x: u32) {
919        unsafe {
920            if x >= 1_0000_0000 {
921                let b = x / 10000;
922                let a = x / 100000000;
923                self.write_u8_unchecked(a as u8);
924                self.write_digit4_unchecked((b - a * 10000) as usize);
925                self.write_digit4_unchecked((x % 10000) as usize);
926            } else if x >= 10000 {
927                self.write_digit4_trimmed_unchecked((x / 10000) as usize);
928                self.write_digit4_unchecked((x % 10000) as usize);
929            } else {
930                self.write_digit4_trimmed_unchecked(x as usize);
931            }
932        }
933    }
934
935    #[inline(always)]
936    unsafe fn write_u64_unchecked(&mut self, x: u64) {
937        unsafe {
938            if x < 10000 {
939                self.write_digit4_trimmed_unchecked(x as usize);
940                return;
941            }
942            if x >= 1_0000_0000_0000_0000 {
943                let d = x / 10000;
944                let c = x / 100000000;
945                let b = x / 1000000000000;
946                let a = x / 10000000000000000;
947                self.write_digit4_trimmed_unchecked(a as usize);
948                self.write_digit4_unchecked((b - a * 10000) as usize);
949                self.write_digit4_unchecked((c - b * 10000) as usize);
950                self.write_digit4_unchecked((d - c * 10000) as usize);
951                self.write_digit4_unchecked((x % 10000) as usize);
952            } else if x >= 1_0000_0000_0000 {
953                let c = x / 10000;
954                let b = x / 100000000;
955                let a = x / 1000000000000;
956                self.write_digit4_trimmed_unchecked(a as usize);
957                self.write_digit4_unchecked((b - a * 10000) as usize);
958                self.write_digit4_unchecked((c - b * 10000) as usize);
959                self.write_digit4_unchecked((x % 10000) as usize);
960            } else if x >= 1_0000_0000 {
961                let b = x / 10000;
962                let a = x / 100000000;
963                self.write_digit4_trimmed_unchecked(a as usize);
964                self.write_digit4_unchecked((b - a * 10000) as usize);
965                self.write_digit4_unchecked((x % 10000) as usize);
966            } else {
967                self.write_digit4_trimmed_unchecked((x / 10000) as usize);
968                self.write_digit4_unchecked((x % 10000) as usize);
969            }
970        }
971    }
972
973    #[inline]
974    pub fn u8(&mut self, x: u8) {
975        self.ensure_capacity(4);
976        unsafe { self.write_u8_unchecked(x) }
977    }
978
979    #[inline]
980    pub fn u16(&mut self, x: u16) {
981        self.ensure_capacity(5);
982        unsafe { self.write_u16_unchecked(x) }
983    }
984
985    #[inline]
986    pub fn u32(&mut self, x: u32) {
987        self.ensure_capacity(10);
988        unsafe { self.write_u32_unchecked(x) }
989    }
990
991    #[inline(always)]
992    pub fn u64(&mut self, x: u64) {
993        self.ensure_capacity(20);
994        unsafe { self.write_u64_unchecked(x) }
995    }
996
997    #[inline]
998    pub fn i8(&mut self, x: i8) {
999        if x < 0 {
1000            self.ensure_capacity(5);
1001            unsafe {
1002                self.write_byte_unchecked(b'-');
1003                self.write_u8_unchecked(x.wrapping_neg() as u8);
1004            }
1005        } else {
1006            self.u8(x as u8);
1007        }
1008    }
1009
1010    #[inline]
1011    pub fn i16(&mut self, x: i16) {
1012        if x < 0 {
1013            self.ensure_capacity(6);
1014            unsafe {
1015                self.write_byte_unchecked(b'-');
1016                self.write_u16_unchecked(x.wrapping_neg() as u16);
1017            }
1018        } else {
1019            self.u16(x as u16);
1020        }
1021    }
1022
1023    #[inline]
1024    pub fn i32(&mut self, x: i32) {
1025        if x < 0 {
1026            self.ensure_capacity(11);
1027            unsafe {
1028                self.write_byte_unchecked(b'-');
1029                self.write_u32_unchecked(x.wrapping_neg() as u32);
1030            }
1031        } else {
1032            self.u32(x as u32);
1033        }
1034    }
1035
1036    #[inline(always)]
1037    pub fn i64(&mut self, x: i64) {
1038        if x < 0 {
1039            self.ensure_capacity(21);
1040            unsafe {
1041                self.write_byte_unchecked(b'-');
1042                self.write_u64_unchecked(x.wrapping_neg() as u64);
1043            }
1044        } else {
1045            self.u64(x as u64);
1046        }
1047    }
1048
1049    #[inline(always)]
1050    pub fn usize(&mut self, x: usize) {
1051        if usize::BITS == 64 {
1052            self.u64(x as u64);
1053        } else {
1054            self.u32(x as u32);
1055        }
1056    }
1057
1058    #[inline(always)]
1059    pub fn isize(&mut self, x: isize) {
1060        if isize::BITS == 64 {
1061            self.i64(x as i64);
1062        } else {
1063            self.i32(x as i32);
1064        }
1065    }
1066
1067    pub fn u128(&mut self, mut x: u128) {
1068        const BASE: u128 = 10_000_000_000_000_000_000;
1069        let mut groups = [0u64; 2];
1070        let mut len = 0;
1071        while x > u64::MAX as u128 {
1072            groups[len] = (x % BASE) as u64;
1073            x /= BASE;
1074            len += 1;
1075        }
1076        self.u64(x as u64);
1077        for &x in groups[..len].iter().rev() {
1078            self.ensure_capacity(19);
1079            unsafe {
1080                ptr::copy_nonoverlapping(
1081                    DIGIT4[(x / 10_000_000_000_000_000) as usize]
1082                        .as_ptr()
1083                        .add(1),
1084                    self.buf.as_mut_ptr().add(self.pos),
1085                    3,
1086                );
1087                self.pos += 3;
1088                self.write_digit4_unchecked((x / 1_000_000_000_000 % 10000) as usize);
1089                self.write_digit4_unchecked((x / 100_000_000 % 10000) as usize);
1090                self.write_digit4_unchecked((x / 10000 % 10000) as usize);
1091                self.write_digit4_unchecked((x % 10000) as usize);
1092            }
1093        }
1094    }
1095
1096    pub fn i128(&mut self, x: i128) {
1097        if x < 0 {
1098            self.byte(b'-');
1099        }
1100        self.u128(x.unsigned_abs());
1101    }
1102
1103    #[inline]
1104    pub fn byte(&mut self, b: u8) {
1105        self.ensure_capacity(1);
1106        unsafe { self.write_byte_unchecked(b) }
1107    }
1108
1109    #[inline(always)]
1110    pub fn bytes(&mut self, s: &[u8]) {
1111        if s.len() > self.buf.len() {
1112            self.flush_buf();
1113            self.inner.write_all(s).unwrap();
1114        } else {
1115            self.ensure_capacity(s.len());
1116            unsafe {
1117                ptr::copy_nonoverlapping(s.as_ptr(), self.buf.as_mut_ptr().add(self.pos), s.len());
1118            }
1119            self.pos += s.len();
1120        }
1121    }
Source

fn ensure_capacity(&mut self, capacity: usize)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 975)
974    pub fn u8(&mut self, x: u8) {
975        self.ensure_capacity(4);
976        unsafe { self.write_u8_unchecked(x) }
977    }
978
979    #[inline]
980    pub fn u16(&mut self, x: u16) {
981        self.ensure_capacity(5);
982        unsafe { self.write_u16_unchecked(x) }
983    }
984
985    #[inline]
986    pub fn u32(&mut self, x: u32) {
987        self.ensure_capacity(10);
988        unsafe { self.write_u32_unchecked(x) }
989    }
990
991    #[inline(always)]
992    pub fn u64(&mut self, x: u64) {
993        self.ensure_capacity(20);
994        unsafe { self.write_u64_unchecked(x) }
995    }
996
997    #[inline]
998    pub fn i8(&mut self, x: i8) {
999        if x < 0 {
1000            self.ensure_capacity(5);
1001            unsafe {
1002                self.write_byte_unchecked(b'-');
1003                self.write_u8_unchecked(x.wrapping_neg() as u8);
1004            }
1005        } else {
1006            self.u8(x as u8);
1007        }
1008    }
1009
1010    #[inline]
1011    pub fn i16(&mut self, x: i16) {
1012        if x < 0 {
1013            self.ensure_capacity(6);
1014            unsafe {
1015                self.write_byte_unchecked(b'-');
1016                self.write_u16_unchecked(x.wrapping_neg() as u16);
1017            }
1018        } else {
1019            self.u16(x as u16);
1020        }
1021    }
1022
1023    #[inline]
1024    pub fn i32(&mut self, x: i32) {
1025        if x < 0 {
1026            self.ensure_capacity(11);
1027            unsafe {
1028                self.write_byte_unchecked(b'-');
1029                self.write_u32_unchecked(x.wrapping_neg() as u32);
1030            }
1031        } else {
1032            self.u32(x as u32);
1033        }
1034    }
1035
1036    #[inline(always)]
1037    pub fn i64(&mut self, x: i64) {
1038        if x < 0 {
1039            self.ensure_capacity(21);
1040            unsafe {
1041                self.write_byte_unchecked(b'-');
1042                self.write_u64_unchecked(x.wrapping_neg() as u64);
1043            }
1044        } else {
1045            self.u64(x as u64);
1046        }
1047    }
1048
1049    #[inline(always)]
1050    pub fn usize(&mut self, x: usize) {
1051        if usize::BITS == 64 {
1052            self.u64(x as u64);
1053        } else {
1054            self.u32(x as u32);
1055        }
1056    }
1057
1058    #[inline(always)]
1059    pub fn isize(&mut self, x: isize) {
1060        if isize::BITS == 64 {
1061            self.i64(x as i64);
1062        } else {
1063            self.i32(x as i32);
1064        }
1065    }
1066
1067    pub fn u128(&mut self, mut x: u128) {
1068        const BASE: u128 = 10_000_000_000_000_000_000;
1069        let mut groups = [0u64; 2];
1070        let mut len = 0;
1071        while x > u64::MAX as u128 {
1072            groups[len] = (x % BASE) as u64;
1073            x /= BASE;
1074            len += 1;
1075        }
1076        self.u64(x as u64);
1077        for &x in groups[..len].iter().rev() {
1078            self.ensure_capacity(19);
1079            unsafe {
1080                ptr::copy_nonoverlapping(
1081                    DIGIT4[(x / 10_000_000_000_000_000) as usize]
1082                        .as_ptr()
1083                        .add(1),
1084                    self.buf.as_mut_ptr().add(self.pos),
1085                    3,
1086                );
1087                self.pos += 3;
1088                self.write_digit4_unchecked((x / 1_000_000_000_000 % 10000) as usize);
1089                self.write_digit4_unchecked((x / 100_000_000 % 10000) as usize);
1090                self.write_digit4_unchecked((x / 10000 % 10000) as usize);
1091                self.write_digit4_unchecked((x % 10000) as usize);
1092            }
1093        }
1094    }
1095
1096    pub fn i128(&mut self, x: i128) {
1097        if x < 0 {
1098            self.byte(b'-');
1099        }
1100        self.u128(x.unsigned_abs());
1101    }
1102
1103    #[inline]
1104    pub fn byte(&mut self, b: u8) {
1105        self.ensure_capacity(1);
1106        unsafe { self.write_byte_unchecked(b) }
1107    }
1108
1109    #[inline(always)]
1110    pub fn bytes(&mut self, s: &[u8]) {
1111        if s.len() > self.buf.len() {
1112            self.flush_buf();
1113            self.inner.write_all(s).unwrap();
1114        } else {
1115            self.ensure_capacity(s.len());
1116            unsafe {
1117                ptr::copy_nonoverlapping(s.as_ptr(), self.buf.as_mut_ptr().add(self.pos), s.len());
1118            }
1119            self.pos += s.len();
1120        }
1121    }
Source

unsafe fn write_byte_unchecked(&mut self, byte: u8)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 1002)
998    pub fn i8(&mut self, x: i8) {
999        if x < 0 {
1000            self.ensure_capacity(5);
1001            unsafe {
1002                self.write_byte_unchecked(b'-');
1003                self.write_u8_unchecked(x.wrapping_neg() as u8);
1004            }
1005        } else {
1006            self.u8(x as u8);
1007        }
1008    }
1009
1010    #[inline]
1011    pub fn i16(&mut self, x: i16) {
1012        if x < 0 {
1013            self.ensure_capacity(6);
1014            unsafe {
1015                self.write_byte_unchecked(b'-');
1016                self.write_u16_unchecked(x.wrapping_neg() as u16);
1017            }
1018        } else {
1019            self.u16(x as u16);
1020        }
1021    }
1022
1023    #[inline]
1024    pub fn i32(&mut self, x: i32) {
1025        if x < 0 {
1026            self.ensure_capacity(11);
1027            unsafe {
1028                self.write_byte_unchecked(b'-');
1029                self.write_u32_unchecked(x.wrapping_neg() as u32);
1030            }
1031        } else {
1032            self.u32(x as u32);
1033        }
1034    }
1035
1036    #[inline(always)]
1037    pub fn i64(&mut self, x: i64) {
1038        if x < 0 {
1039            self.ensure_capacity(21);
1040            unsafe {
1041                self.write_byte_unchecked(b'-');
1042                self.write_u64_unchecked(x.wrapping_neg() as u64);
1043            }
1044        } else {
1045            self.u64(x as u64);
1046        }
1047    }
1048
1049    #[inline(always)]
1050    pub fn usize(&mut self, x: usize) {
1051        if usize::BITS == 64 {
1052            self.u64(x as u64);
1053        } else {
1054            self.u32(x as u32);
1055        }
1056    }
1057
1058    #[inline(always)]
1059    pub fn isize(&mut self, x: isize) {
1060        if isize::BITS == 64 {
1061            self.i64(x as i64);
1062        } else {
1063            self.i32(x as i32);
1064        }
1065    }
1066
1067    pub fn u128(&mut self, mut x: u128) {
1068        const BASE: u128 = 10_000_000_000_000_000_000;
1069        let mut groups = [0u64; 2];
1070        let mut len = 0;
1071        while x > u64::MAX as u128 {
1072            groups[len] = (x % BASE) as u64;
1073            x /= BASE;
1074            len += 1;
1075        }
1076        self.u64(x as u64);
1077        for &x in groups[..len].iter().rev() {
1078            self.ensure_capacity(19);
1079            unsafe {
1080                ptr::copy_nonoverlapping(
1081                    DIGIT4[(x / 10_000_000_000_000_000) as usize]
1082                        .as_ptr()
1083                        .add(1),
1084                    self.buf.as_mut_ptr().add(self.pos),
1085                    3,
1086                );
1087                self.pos += 3;
1088                self.write_digit4_unchecked((x / 1_000_000_000_000 % 10000) as usize);
1089                self.write_digit4_unchecked((x / 100_000_000 % 10000) as usize);
1090                self.write_digit4_unchecked((x / 10000 % 10000) as usize);
1091                self.write_digit4_unchecked((x % 10000) as usize);
1092            }
1093        }
1094    }
1095
1096    pub fn i128(&mut self, x: i128) {
1097        if x < 0 {
1098            self.byte(b'-');
1099        }
1100        self.u128(x.unsigned_abs());
1101    }
1102
1103    #[inline]
1104    pub fn byte(&mut self, b: u8) {
1105        self.ensure_capacity(1);
1106        unsafe { self.write_byte_unchecked(b) }
1107    }
Source

unsafe fn write_digit4_unchecked(&mut self, x: usize)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 910)
906    unsafe fn write_u16_unchecked(&mut self, x: u16) {
907        unsafe {
908            if x >= 10000 {
909                self.write_digit4_trimmed_unchecked((x / 10000) as usize);
910                self.write_digit4_unchecked((x % 10000) as usize);
911            } else {
912                self.write_digit4_trimmed_unchecked(x as usize);
913            }
914        }
915    }
916
917    #[inline]
918    unsafe fn write_u32_unchecked(&mut self, x: u32) {
919        unsafe {
920            if x >= 1_0000_0000 {
921                let b = x / 10000;
922                let a = x / 100000000;
923                self.write_u8_unchecked(a as u8);
924                self.write_digit4_unchecked((b - a * 10000) as usize);
925                self.write_digit4_unchecked((x % 10000) as usize);
926            } else if x >= 10000 {
927                self.write_digit4_trimmed_unchecked((x / 10000) as usize);
928                self.write_digit4_unchecked((x % 10000) as usize);
929            } else {
930                self.write_digit4_trimmed_unchecked(x as usize);
931            }
932        }
933    }
934
935    #[inline(always)]
936    unsafe fn write_u64_unchecked(&mut self, x: u64) {
937        unsafe {
938            if x < 10000 {
939                self.write_digit4_trimmed_unchecked(x as usize);
940                return;
941            }
942            if x >= 1_0000_0000_0000_0000 {
943                let d = x / 10000;
944                let c = x / 100000000;
945                let b = x / 1000000000000;
946                let a = x / 10000000000000000;
947                self.write_digit4_trimmed_unchecked(a as usize);
948                self.write_digit4_unchecked((b - a * 10000) as usize);
949                self.write_digit4_unchecked((c - b * 10000) as usize);
950                self.write_digit4_unchecked((d - c * 10000) as usize);
951                self.write_digit4_unchecked((x % 10000) as usize);
952            } else if x >= 1_0000_0000_0000 {
953                let c = x / 10000;
954                let b = x / 100000000;
955                let a = x / 1000000000000;
956                self.write_digit4_trimmed_unchecked(a as usize);
957                self.write_digit4_unchecked((b - a * 10000) as usize);
958                self.write_digit4_unchecked((c - b * 10000) as usize);
959                self.write_digit4_unchecked((x % 10000) as usize);
960            } else if x >= 1_0000_0000 {
961                let b = x / 10000;
962                let a = x / 100000000;
963                self.write_digit4_trimmed_unchecked(a as usize);
964                self.write_digit4_unchecked((b - a * 10000) as usize);
965                self.write_digit4_unchecked((x % 10000) as usize);
966            } else {
967                self.write_digit4_trimmed_unchecked((x / 10000) as usize);
968                self.write_digit4_unchecked((x % 10000) as usize);
969            }
970        }
971    }
972
973    #[inline]
974    pub fn u8(&mut self, x: u8) {
975        self.ensure_capacity(4);
976        unsafe { self.write_u8_unchecked(x) }
977    }
978
979    #[inline]
980    pub fn u16(&mut self, x: u16) {
981        self.ensure_capacity(5);
982        unsafe { self.write_u16_unchecked(x) }
983    }
984
985    #[inline]
986    pub fn u32(&mut self, x: u32) {
987        self.ensure_capacity(10);
988        unsafe { self.write_u32_unchecked(x) }
989    }
990
991    #[inline(always)]
992    pub fn u64(&mut self, x: u64) {
993        self.ensure_capacity(20);
994        unsafe { self.write_u64_unchecked(x) }
995    }
996
997    #[inline]
998    pub fn i8(&mut self, x: i8) {
999        if x < 0 {
1000            self.ensure_capacity(5);
1001            unsafe {
1002                self.write_byte_unchecked(b'-');
1003                self.write_u8_unchecked(x.wrapping_neg() as u8);
1004            }
1005        } else {
1006            self.u8(x as u8);
1007        }
1008    }
1009
1010    #[inline]
1011    pub fn i16(&mut self, x: i16) {
1012        if x < 0 {
1013            self.ensure_capacity(6);
1014            unsafe {
1015                self.write_byte_unchecked(b'-');
1016                self.write_u16_unchecked(x.wrapping_neg() as u16);
1017            }
1018        } else {
1019            self.u16(x as u16);
1020        }
1021    }
1022
1023    #[inline]
1024    pub fn i32(&mut self, x: i32) {
1025        if x < 0 {
1026            self.ensure_capacity(11);
1027            unsafe {
1028                self.write_byte_unchecked(b'-');
1029                self.write_u32_unchecked(x.wrapping_neg() as u32);
1030            }
1031        } else {
1032            self.u32(x as u32);
1033        }
1034    }
1035
1036    #[inline(always)]
1037    pub fn i64(&mut self, x: i64) {
1038        if x < 0 {
1039            self.ensure_capacity(21);
1040            unsafe {
1041                self.write_byte_unchecked(b'-');
1042                self.write_u64_unchecked(x.wrapping_neg() as u64);
1043            }
1044        } else {
1045            self.u64(x as u64);
1046        }
1047    }
1048
1049    #[inline(always)]
1050    pub fn usize(&mut self, x: usize) {
1051        if usize::BITS == 64 {
1052            self.u64(x as u64);
1053        } else {
1054            self.u32(x as u32);
1055        }
1056    }
1057
1058    #[inline(always)]
1059    pub fn isize(&mut self, x: isize) {
1060        if isize::BITS == 64 {
1061            self.i64(x as i64);
1062        } else {
1063            self.i32(x as i32);
1064        }
1065    }
1066
1067    pub fn u128(&mut self, mut x: u128) {
1068        const BASE: u128 = 10_000_000_000_000_000_000;
1069        let mut groups = [0u64; 2];
1070        let mut len = 0;
1071        while x > u64::MAX as u128 {
1072            groups[len] = (x % BASE) as u64;
1073            x /= BASE;
1074            len += 1;
1075        }
1076        self.u64(x as u64);
1077        for &x in groups[..len].iter().rev() {
1078            self.ensure_capacity(19);
1079            unsafe {
1080                ptr::copy_nonoverlapping(
1081                    DIGIT4[(x / 10_000_000_000_000_000) as usize]
1082                        .as_ptr()
1083                        .add(1),
1084                    self.buf.as_mut_ptr().add(self.pos),
1085                    3,
1086                );
1087                self.pos += 3;
1088                self.write_digit4_unchecked((x / 1_000_000_000_000 % 10000) as usize);
1089                self.write_digit4_unchecked((x / 100_000_000 % 10000) as usize);
1090                self.write_digit4_unchecked((x / 10000 % 10000) as usize);
1091                self.write_digit4_unchecked((x % 10000) as usize);
1092            }
1093        }
1094    }
Source

unsafe fn write_digit4_trimmed_unchecked(&mut self, x: usize)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 909)
906    unsafe fn write_u16_unchecked(&mut self, x: u16) {
907        unsafe {
908            if x >= 10000 {
909                self.write_digit4_trimmed_unchecked((x / 10000) as usize);
910                self.write_digit4_unchecked((x % 10000) as usize);
911            } else {
912                self.write_digit4_trimmed_unchecked(x as usize);
913            }
914        }
915    }
916
917    #[inline]
918    unsafe fn write_u32_unchecked(&mut self, x: u32) {
919        unsafe {
920            if x >= 1_0000_0000 {
921                let b = x / 10000;
922                let a = x / 100000000;
923                self.write_u8_unchecked(a as u8);
924                self.write_digit4_unchecked((b - a * 10000) as usize);
925                self.write_digit4_unchecked((x % 10000) as usize);
926            } else if x >= 10000 {
927                self.write_digit4_trimmed_unchecked((x / 10000) as usize);
928                self.write_digit4_unchecked((x % 10000) as usize);
929            } else {
930                self.write_digit4_trimmed_unchecked(x as usize);
931            }
932        }
933    }
934
935    #[inline(always)]
936    unsafe fn write_u64_unchecked(&mut self, x: u64) {
937        unsafe {
938            if x < 10000 {
939                self.write_digit4_trimmed_unchecked(x as usize);
940                return;
941            }
942            if x >= 1_0000_0000_0000_0000 {
943                let d = x / 10000;
944                let c = x / 100000000;
945                let b = x / 1000000000000;
946                let a = x / 10000000000000000;
947                self.write_digit4_trimmed_unchecked(a as usize);
948                self.write_digit4_unchecked((b - a * 10000) as usize);
949                self.write_digit4_unchecked((c - b * 10000) as usize);
950                self.write_digit4_unchecked((d - c * 10000) as usize);
951                self.write_digit4_unchecked((x % 10000) as usize);
952            } else if x >= 1_0000_0000_0000 {
953                let c = x / 10000;
954                let b = x / 100000000;
955                let a = x / 1000000000000;
956                self.write_digit4_trimmed_unchecked(a as usize);
957                self.write_digit4_unchecked((b - a * 10000) as usize);
958                self.write_digit4_unchecked((c - b * 10000) as usize);
959                self.write_digit4_unchecked((x % 10000) as usize);
960            } else if x >= 1_0000_0000 {
961                let b = x / 10000;
962                let a = x / 100000000;
963                self.write_digit4_trimmed_unchecked(a as usize);
964                self.write_digit4_unchecked((b - a * 10000) as usize);
965                self.write_digit4_unchecked((x % 10000) as usize);
966            } else {
967                self.write_digit4_trimmed_unchecked((x / 10000) as usize);
968                self.write_digit4_unchecked((x % 10000) as usize);
969            }
970        }
971    }
Source

unsafe fn write_u8_unchecked(&mut self, x: u8)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 923)
918    unsafe fn write_u32_unchecked(&mut self, x: u32) {
919        unsafe {
920            if x >= 1_0000_0000 {
921                let b = x / 10000;
922                let a = x / 100000000;
923                self.write_u8_unchecked(a as u8);
924                self.write_digit4_unchecked((b - a * 10000) as usize);
925                self.write_digit4_unchecked((x % 10000) as usize);
926            } else if x >= 10000 {
927                self.write_digit4_trimmed_unchecked((x / 10000) as usize);
928                self.write_digit4_unchecked((x % 10000) as usize);
929            } else {
930                self.write_digit4_trimmed_unchecked(x as usize);
931            }
932        }
933    }
934
935    #[inline(always)]
936    unsafe fn write_u64_unchecked(&mut self, x: u64) {
937        unsafe {
938            if x < 10000 {
939                self.write_digit4_trimmed_unchecked(x as usize);
940                return;
941            }
942            if x >= 1_0000_0000_0000_0000 {
943                let d = x / 10000;
944                let c = x / 100000000;
945                let b = x / 1000000000000;
946                let a = x / 10000000000000000;
947                self.write_digit4_trimmed_unchecked(a as usize);
948                self.write_digit4_unchecked((b - a * 10000) as usize);
949                self.write_digit4_unchecked((c - b * 10000) as usize);
950                self.write_digit4_unchecked((d - c * 10000) as usize);
951                self.write_digit4_unchecked((x % 10000) as usize);
952            } else if x >= 1_0000_0000_0000 {
953                let c = x / 10000;
954                let b = x / 100000000;
955                let a = x / 1000000000000;
956                self.write_digit4_trimmed_unchecked(a as usize);
957                self.write_digit4_unchecked((b - a * 10000) as usize);
958                self.write_digit4_unchecked((c - b * 10000) as usize);
959                self.write_digit4_unchecked((x % 10000) as usize);
960            } else if x >= 1_0000_0000 {
961                let b = x / 10000;
962                let a = x / 100000000;
963                self.write_digit4_trimmed_unchecked(a as usize);
964                self.write_digit4_unchecked((b - a * 10000) as usize);
965                self.write_digit4_unchecked((x % 10000) as usize);
966            } else {
967                self.write_digit4_trimmed_unchecked((x / 10000) as usize);
968                self.write_digit4_unchecked((x % 10000) as usize);
969            }
970        }
971    }
972
973    #[inline]
974    pub fn u8(&mut self, x: u8) {
975        self.ensure_capacity(4);
976        unsafe { self.write_u8_unchecked(x) }
977    }
978
979    #[inline]
980    pub fn u16(&mut self, x: u16) {
981        self.ensure_capacity(5);
982        unsafe { self.write_u16_unchecked(x) }
983    }
984
985    #[inline]
986    pub fn u32(&mut self, x: u32) {
987        self.ensure_capacity(10);
988        unsafe { self.write_u32_unchecked(x) }
989    }
990
991    #[inline(always)]
992    pub fn u64(&mut self, x: u64) {
993        self.ensure_capacity(20);
994        unsafe { self.write_u64_unchecked(x) }
995    }
996
997    #[inline]
998    pub fn i8(&mut self, x: i8) {
999        if x < 0 {
1000            self.ensure_capacity(5);
1001            unsafe {
1002                self.write_byte_unchecked(b'-');
1003                self.write_u8_unchecked(x.wrapping_neg() as u8);
1004            }
1005        } else {
1006            self.u8(x as u8);
1007        }
1008    }
Source

unsafe fn write_u16_unchecked(&mut self, x: u16)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 982)
980    pub fn u16(&mut self, x: u16) {
981        self.ensure_capacity(5);
982        unsafe { self.write_u16_unchecked(x) }
983    }
984
985    #[inline]
986    pub fn u32(&mut self, x: u32) {
987        self.ensure_capacity(10);
988        unsafe { self.write_u32_unchecked(x) }
989    }
990
991    #[inline(always)]
992    pub fn u64(&mut self, x: u64) {
993        self.ensure_capacity(20);
994        unsafe { self.write_u64_unchecked(x) }
995    }
996
997    #[inline]
998    pub fn i8(&mut self, x: i8) {
999        if x < 0 {
1000            self.ensure_capacity(5);
1001            unsafe {
1002                self.write_byte_unchecked(b'-');
1003                self.write_u8_unchecked(x.wrapping_neg() as u8);
1004            }
1005        } else {
1006            self.u8(x as u8);
1007        }
1008    }
1009
1010    #[inline]
1011    pub fn i16(&mut self, x: i16) {
1012        if x < 0 {
1013            self.ensure_capacity(6);
1014            unsafe {
1015                self.write_byte_unchecked(b'-');
1016                self.write_u16_unchecked(x.wrapping_neg() as u16);
1017            }
1018        } else {
1019            self.u16(x as u16);
1020        }
1021    }
Source

unsafe fn write_u32_unchecked(&mut self, x: u32)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 988)
986    pub fn u32(&mut self, x: u32) {
987        self.ensure_capacity(10);
988        unsafe { self.write_u32_unchecked(x) }
989    }
990
991    #[inline(always)]
992    pub fn u64(&mut self, x: u64) {
993        self.ensure_capacity(20);
994        unsafe { self.write_u64_unchecked(x) }
995    }
996
997    #[inline]
998    pub fn i8(&mut self, x: i8) {
999        if x < 0 {
1000            self.ensure_capacity(5);
1001            unsafe {
1002                self.write_byte_unchecked(b'-');
1003                self.write_u8_unchecked(x.wrapping_neg() as u8);
1004            }
1005        } else {
1006            self.u8(x as u8);
1007        }
1008    }
1009
1010    #[inline]
1011    pub fn i16(&mut self, x: i16) {
1012        if x < 0 {
1013            self.ensure_capacity(6);
1014            unsafe {
1015                self.write_byte_unchecked(b'-');
1016                self.write_u16_unchecked(x.wrapping_neg() as u16);
1017            }
1018        } else {
1019            self.u16(x as u16);
1020        }
1021    }
1022
1023    #[inline]
1024    pub fn i32(&mut self, x: i32) {
1025        if x < 0 {
1026            self.ensure_capacity(11);
1027            unsafe {
1028                self.write_byte_unchecked(b'-');
1029                self.write_u32_unchecked(x.wrapping_neg() as u32);
1030            }
1031        } else {
1032            self.u32(x as u32);
1033        }
1034    }
Source

unsafe fn write_u64_unchecked(&mut self, x: u64)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 994)
992    pub fn u64(&mut self, x: u64) {
993        self.ensure_capacity(20);
994        unsafe { self.write_u64_unchecked(x) }
995    }
996
997    #[inline]
998    pub fn i8(&mut self, x: i8) {
999        if x < 0 {
1000            self.ensure_capacity(5);
1001            unsafe {
1002                self.write_byte_unchecked(b'-');
1003                self.write_u8_unchecked(x.wrapping_neg() as u8);
1004            }
1005        } else {
1006            self.u8(x as u8);
1007        }
1008    }
1009
1010    #[inline]
1011    pub fn i16(&mut self, x: i16) {
1012        if x < 0 {
1013            self.ensure_capacity(6);
1014            unsafe {
1015                self.write_byte_unchecked(b'-');
1016                self.write_u16_unchecked(x.wrapping_neg() as u16);
1017            }
1018        } else {
1019            self.u16(x as u16);
1020        }
1021    }
1022
1023    #[inline]
1024    pub fn i32(&mut self, x: i32) {
1025        if x < 0 {
1026            self.ensure_capacity(11);
1027            unsafe {
1028                self.write_byte_unchecked(b'-');
1029                self.write_u32_unchecked(x.wrapping_neg() as u32);
1030            }
1031        } else {
1032            self.u32(x as u32);
1033        }
1034    }
1035
1036    #[inline(always)]
1037    pub fn i64(&mut self, x: i64) {
1038        if x < 0 {
1039            self.ensure_capacity(21);
1040            unsafe {
1041                self.write_byte_unchecked(b'-');
1042                self.write_u64_unchecked(x.wrapping_neg() as u64);
1043            }
1044        } else {
1045            self.u64(x as u64);
1046        }
1047    }
Source

pub fn u8(&mut self, x: u8)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 1006)
998    pub fn i8(&mut self, x: i8) {
999        if x < 0 {
1000            self.ensure_capacity(5);
1001            unsafe {
1002                self.write_byte_unchecked(b'-');
1003                self.write_u8_unchecked(x.wrapping_neg() as u8);
1004            }
1005        } else {
1006            self.u8(x as u8);
1007        }
1008    }
Source

pub fn u16(&mut self, x: u16)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 1019)
1011    pub fn i16(&mut self, x: i16) {
1012        if x < 0 {
1013            self.ensure_capacity(6);
1014            unsafe {
1015                self.write_byte_unchecked(b'-');
1016                self.write_u16_unchecked(x.wrapping_neg() as u16);
1017            }
1018        } else {
1019            self.u16(x as u16);
1020        }
1021    }
Source

pub fn u32(&mut self, x: u32)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 1032)
1024    pub fn i32(&mut self, x: i32) {
1025        if x < 0 {
1026            self.ensure_capacity(11);
1027            unsafe {
1028                self.write_byte_unchecked(b'-');
1029                self.write_u32_unchecked(x.wrapping_neg() as u32);
1030            }
1031        } else {
1032            self.u32(x as u32);
1033        }
1034    }
1035
1036    #[inline(always)]
1037    pub fn i64(&mut self, x: i64) {
1038        if x < 0 {
1039            self.ensure_capacity(21);
1040            unsafe {
1041                self.write_byte_unchecked(b'-');
1042                self.write_u64_unchecked(x.wrapping_neg() as u64);
1043            }
1044        } else {
1045            self.u64(x as u64);
1046        }
1047    }
1048
1049    #[inline(always)]
1050    pub fn usize(&mut self, x: usize) {
1051        if usize::BITS == 64 {
1052            self.u64(x as u64);
1053        } else {
1054            self.u32(x as u32);
1055        }
1056    }
Source

pub fn u64(&mut self, x: u64)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 1045)
1037    pub fn i64(&mut self, x: i64) {
1038        if x < 0 {
1039            self.ensure_capacity(21);
1040            unsafe {
1041                self.write_byte_unchecked(b'-');
1042                self.write_u64_unchecked(x.wrapping_neg() as u64);
1043            }
1044        } else {
1045            self.u64(x as u64);
1046        }
1047    }
1048
1049    #[inline(always)]
1050    pub fn usize(&mut self, x: usize) {
1051        if usize::BITS == 64 {
1052            self.u64(x as u64);
1053        } else {
1054            self.u32(x as u32);
1055        }
1056    }
1057
1058    #[inline(always)]
1059    pub fn isize(&mut self, x: isize) {
1060        if isize::BITS == 64 {
1061            self.i64(x as i64);
1062        } else {
1063            self.i32(x as i32);
1064        }
1065    }
1066
1067    pub fn u128(&mut self, mut x: u128) {
1068        const BASE: u128 = 10_000_000_000_000_000_000;
1069        let mut groups = [0u64; 2];
1070        let mut len = 0;
1071        while x > u64::MAX as u128 {
1072            groups[len] = (x % BASE) as u64;
1073            x /= BASE;
1074            len += 1;
1075        }
1076        self.u64(x as u64);
1077        for &x in groups[..len].iter().rev() {
1078            self.ensure_capacity(19);
1079            unsafe {
1080                ptr::copy_nonoverlapping(
1081                    DIGIT4[(x / 10_000_000_000_000_000) as usize]
1082                        .as_ptr()
1083                        .add(1),
1084                    self.buf.as_mut_ptr().add(self.pos),
1085                    3,
1086                );
1087                self.pos += 3;
1088                self.write_digit4_unchecked((x / 1_000_000_000_000 % 10000) as usize);
1089                self.write_digit4_unchecked((x / 100_000_000 % 10000) as usize);
1090                self.write_digit4_unchecked((x / 10000 % 10000) as usize);
1091                self.write_digit4_unchecked((x % 10000) as usize);
1092            }
1093        }
1094    }
Source

pub fn i8(&mut self, x: i8)

Source

pub fn i16(&mut self, x: i16)

Source

pub fn i32(&mut self, x: i32)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 1063)
1059    pub fn isize(&mut self, x: isize) {
1060        if isize::BITS == 64 {
1061            self.i64(x as i64);
1062        } else {
1063            self.i32(x as i32);
1064        }
1065    }
Source

pub fn i64(&mut self, x: i64)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 1061)
1059    pub fn isize(&mut self, x: isize) {
1060        if isize::BITS == 64 {
1061            self.i64(x as i64);
1062        } else {
1063            self.i32(x as i32);
1064        }
1065    }
Source

pub fn usize(&mut self, x: usize)

Source

pub fn isize(&mut self, x: isize)

Source

pub fn u128(&mut self, x: u128)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 1100)
1096    pub fn i128(&mut self, x: i128) {
1097        if x < 0 {
1098            self.byte(b'-');
1099        }
1100        self.u128(x.unsigned_abs());
1101    }
Source

pub fn i128(&mut self, x: i128)

Source

pub fn byte(&mut self, b: u8)

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 1098)
1096    pub fn i128(&mut self, x: i128) {
1097        if x < 0 {
1098            self.byte(b'-');
1099        }
1100        self.u128(x.unsigned_abs());
1101    }
More examples
Hide additional examples
crates/competitive/src/tools/fast_print.rs (line 41)
39    fn fast_print<W: Write>(&self, writer: &mut FastOutput<W>) {
40        if self.is_ascii() {
41            writer.byte(*self as u8);
42        } else {
43            writer.bytes(self.encode_utf8(&mut [0; 4]).as_bytes());
44        }
45    }
Source

pub fn bytes(&mut self, s: &[u8])

Examples found in repository?
crates/competitive/src/tools/fastio.rs (line 1126)
1125    fn write_str(&mut self, s: &str) -> fmt::Result {
1126        self.bytes(s.as_bytes());
1127        Ok(())
1128    }
More examples
Hide additional examples
crates/competitive/src/tools/fast_print.rs (line 28)
27    fn fast_print<W: Write>(&self, writer: &mut FastOutput<W>) {
28        writer.bytes(self.as_bytes());
29    }
30}
31impl FastPrint for String {
32    #[inline]
33    fn fast_print<W: Write>(&self, writer: &mut FastOutput<W>) {
34        self.as_str().fast_print(writer);
35    }
36}
37impl FastPrint for char {
38    #[inline(always)]
39    fn fast_print<W: Write>(&self, writer: &mut FastOutput<W>) {
40        if self.is_ascii() {
41            writer.byte(*self as u8);
42        } else {
43            writer.bytes(self.encode_utf8(&mut [0; 4]).as_bytes());
44        }
45    }

Trait Implementations§

Source§

impl<W> Drop for FastOutput<W>
where W: Write,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<W: Write> Write for FastOutput<W>

Source§

fn write_str(&mut self, s: &str) -> Result

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · Source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more

Auto Trait Implementations§

§

impl<W> Freeze for FastOutput<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for FastOutput<W>
where W: RefUnwindSafe,

§

impl<W> Send for FastOutput<W>
where W: Send,

§

impl<W> Sync for FastOutput<W>
where W: Sync,

§

impl<W> Unpin for FastOutput<W>
where W: Unpin,

§

impl<W> UnsafeUnpin for FastOutput<W>
where W: UnsafeUnpin,

§

impl<W> UnwindSafe for FastOutput<W>
where W: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToArrayVecScalar for T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.