<commit_before>﻿// Copyright (c) 2013 <NAME>
// See the file COPYING for license details.

using System;
using System.Collections.Generic;
using System.Text;

namespace x2
{
    /// <summary>
    /// A variable-length byte buffer class that provides methods to read/write 
    /// primitive data types as byte sequences in a platform-independent way.
    /// </summary>
    /// <remarks>
    /// All the members of this class are not thread-safe.
    /// </remarks>
    public class Buffer
    {
        private readonly List<byte[]> blocks;
        private readonly int blockSizeExponent;
        private readonly int remainderMask;

        private byte[] currentBlock;
        private int currentBlockIndex;
        private int position;
        private int back;
        private int front;

        private int marker;

        public int Length
        {
            get { return (back - front); }
        }

        public int Position
        {
            get { return position; }
            set
            {
                if (value < front || back < value)
                {
                    throw new IndexOutOfRangeException();
                }
                position = value;
                int blockIndex = position >> blockSizeExponent;
                if (blockIndex >= blocks.Count)
                {
                    blockIndex = blocks.Count - 1;
                }
                if (blockIndex != currentBlockIndex)
                {
                    currentBlockIndex = blockIndex;
                    currentBlock = blocks[currentBlockIndex];
                }
            }
        }

        private int BlockSize
        {
            get { return (1 << blockSizeExponent); }
        }

        private int Capacity
        {
            get { return (BlockSize * blocks.Count); }
        }

        public Buffer(int blockSizeExponent)
        {
            if (blockSizeExponent < 0 || 31 < blockSizeExponent)
            {
                throw new ArgumentOutOfRangeException();
            }
            blocks = new List<byte[]>();
            this.blockSizeExponent = blockSizeExponent;
            remainderMask = ~(~0 << blockSizeExponent);

            blocks.Add(AcquireBlock(BlockSize));

            currentBlockIndex = 0;
            currentBlock = blocks[currentBlockIndex];
            position = 0;
            back = 0;
            front = 0;

            marker = -1;
        }

        ~Buffer() { }

        public static int Write(byte[] buffer, int value)
        {
            if (buffer.Length < 4)
            {
                throw new ArgumentException();
            }
            buffer[0] = (byte)(value >> 24);
            buffer[1] = (byte)(value >> 16);
            buffer[2] = (byte)(value >> 8);<commit_msg>touched x2.Buffer<commit_after>﻿// Copyright (c) 2013 <NAME>
// See the file COPYING for license details.

using System;
using System.Collections.Generic;
using System.Text;

namespace x2
{
    /// <summary>
    /// A variable-length (though it's limited to a multiple of a power of 2)
    /// byte buffer class that provides methods to read/write primitive data
    /// types as byte sequences in a platform-independent way.
    /// </summary>
    public class Buffer
    {
        private readonly List<byte[]> blocks;
        private readonly int blockSizeExponent;
        private readonly int remainderMask;

        private byte[] currentBlock;
        private int currentBlockIndex;
        private int position;
        private int back;
        private int front;

        private int marker;

        private int BlockSize
        {
            get { return (1 << blockSizeExponent); }
        }

        private int Capacity
        {
            get { return (BlockSize * blocks.Count); }
        }

        public int Length
        {
            get { return (back - front); }
        }

        public int Position
        {
            get { return position; }
            set
            {
                if (value < front || back < value)
                {
                    throw new IndexOutOfRangeException();
                }
                position = value;
                int blockIndex = position >> blockSizeExponent;
                if (blockIndex >= blocks.Count)
                {
                    blockIndex = blocks.Count - 1;
                }
                if (blockIndex != currentBlockIndex)
                {
                    currentBlockIndex = blockIndex;
                    currentBlock = blocks[currentBlockIndex];
                }
            }
        }

        public Buffer(int blockSizeExponent)
        {
            if (blockSizeExponent < 0 || 31 < blockSizeExponent)
            {
                throw new ArgumentOutOfRangeException();
            }
            blocks = new List<byte[]>();
            this.blockSizeExponent = blockSizeExponent;
            remainderMask = ~(~0 << blockSizeExponent);

            blocks.Add(AcquireBlock(BlockSize));

            currentBlockIndex = 0;
            currentBlock = blocks[currentBlockIndex];
            position = 0;
            back = 0;
            front = 0;

            marker = -1;
        }

        public static int Write(byte[] buffer, int value)
        {
            if (buffer.Length < 4)
            {
                throw new ArgumentException();
            }
            buffer[0] = (byte)(value >> 24);
            buffer[1] = (byte)(value >> 16);
            buffer[2] = (byte)(value >> 8);
{'fraction_non_alphanumeric': 0.08846990572878898, 'fraction_numerical': 0.009427121102248005, 'mean_word_length': 1.354673495518566, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3654491', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1302, 'n_words': 391}