<commit_before>'use strict';

import {IPv4} from "./IPv4";
import {IPv4Prefix} from "./Prefix";
import * as bigInt from "big-integer/BigInteger";
import {leftPadWithZeroBit} from "./BinaryUtils";
import {binaryToDecimal} from "./BinaryUtils";
import {Validator} from "./Validator";

/**
 * Represents a continuous segment of IPv4 addresses
 */
export class IPv4Range implements IterableIterator<IPv4> {
    private readonly bitValue: number = 32;
    private readonly cidrPrefix: IPv4Prefix;
    private readonly ipv4: IPv4;
    private internalCounterValue: IPv4;

    static of(rangeIncidrNotation:string):IPv4Range {
        let [isValid, message] = Validator.isValidCidrNotation(rangeIncidrNotation);
        if (!isValid) {
            throw new Error(message);
        }
        let cidrComponents: Array<string> = rangeIncidrNotation.split("/");
        let ipString = cidrComponents[0];
        let prefix = parseInt(cidrComponents[1]);
        return new IPv4Range(IPv4.fromDecimalDottedString(ipString), IPv4Prefix.of(prefix));
    };

    constructor(ipv4: IPv4, prefix: IPv4Prefix) {
        this.ipv4 = ipv4;
        this.cidrPrefix = prefix;
        this.internalCounterValue = this.getFirst();
    }

    public getSize(): number {
        /**
         * Using bitwise shit operation this will be
         * 1 << (this.bitValue - this.prefix.getValue())
         * Since left shift a number by x is equivalent to multiplying the number by the power x raised to 2
         * 2 << 4 = 2 * (2 raised to 4)
          */
       return Math.pow(2, (this.bitValue - this.cidrPrefix.getValue()));
    }

    public toCidrString(): string {
        return `${this.ipv4.toString()}/${this.cidrPrefix.toString()}`
    }

    public toRangeString(): string {
        return `${this.getFirst()}-${this.getLast()}`
    }

    public getFirst(): IPv4 {
        return IPv4.fromBigInteger(this.ipv4.getValue().and(this.cidrPrefix.toSubnet().getValue()));
    }

    public getLast(): IPv4 {
        let invertedSubnet = leftPadWithZeroBit(this.cidrPrefix.toSubnet().getValue().xor('4294967295').toString(2), 32);
        return IPv4.fromBigInteger(this.ipv4.getValue().or(binaryToDecimal(invertedSubnet)));
    }

    public isConsecutive(otherRange: IPv4Range): boolean {<commit_msg>Using access modifier on constructor argument to automatically create member variable for IPv4Range<commit_after>'use strict';

import {IPv4} from "./IPv4";
import {IPv4Prefix} from "./Prefix";
import * as bigInt from "big-integer/BigInteger";
import {leftPadWithZeroBit} from "./BinaryUtils";
import {binaryToDecimal} from "./BinaryUtils";
import {Validator} from "./Validator";

/**
 * Represents a continuous segment of IPv4 addresses
 */
export class IPv4Range implements IterableIterator<IPv4> {
    private readonly bitValue: number = 32;
    private internalCounterValue: IPv4;

    static of(rangeIncidrNotation:string):IPv4Range {
        let [isValid, message] = Validator.isValidCidrNotation(rangeIncidrNotation);
        if (!isValid) {
            throw new Error(message);
        }
        let cidrComponents: Array<string> = rangeIncidrNotation.split("/");
        let ipString = cidrComponents[0];
        let prefix = parseInt(cidrComponents[1]);
        return new IPv4Range(IPv4.fromDecimalDottedString(ipString), IPv4Prefix.of(prefix));
    };

    constructor(private readonly ipv4: IPv4, private readonly cidrPrefix: IPv4Prefix) {
        this.internalCounterValue = this.getFirst();
    }

    public getSize(): number {
        /**
         * Using bitwise shit operation this will be
         * 1 << (this.bitValue - this.prefix.getValue())
         * Since left shift a number by x is equivalent to multiplying the number by the power x raised to 2
         * 2 << 4 = 2 * (2 raised to 4)
          */
       return Math.pow(2, (this.bitValue - this.cidrPrefix.getValue()));
    }

    public toCidrString(): string {
        return `${this.ipv4.toString()}/${this.cidrPrefix.toString()}`
    }

    public toRangeString(): string {
        return `${this.getFirst()}-${this.getLast()}`
    }

    public getFirst(): IPv4 {
        return IPv4.fromBigInteger(this.ipv4.getValue().and(this.cidrPrefix.toSubnet().getValue()));
    }

    public getLast(): IPv4 {
        let invertedSubnet = leftPadWithZeroBit(this.cidrPrefix.toSubnet().getValue().xor('4294967295').toString(2), 32);
        return IPv4.fromBigInteger(this.ipv4.getValue().or(binaryToDecimal(invertedSubnet)));
    }

    public isConsecutive(otherRange: IPv4Range): boolean {
{'fraction_non_alphanumeric': 0.12478031634446397, 'fraction_numerical': 0.021968365553602813, 'mean_word_length': 3.2831608654750704, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, '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': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '61826', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1354, 'n_words': 328}