<commit_before>// Copyright (c) 2016, Open DICOMweb Project. All rights reserved.
// Use of this source code is governed by the open source license
// that can be found in the LICENSE file.
// Author: <NAME> <<EMAIL>> -
// See the AUTHORS file for other contributors.

import 'dart:typed_data';

import 'package:dictionary/src/dicom/constants.dart';

/// Floating Point Data Types

//TODO: need work.

/// The [Type] of Range checkers.
typedef bool _InRange(double val);

double _floatError(String type, double min, double val, double max) {
  throw new RangeError('$type: out of range (min <= val <= max');
}

class Float {
  static String get type => "Float";

  static bool inRange(double min, double val, double max) => (min <= val && val <= max);

  static double guard(double min, double val, double max) =>
      inRange(min, val, max) ? val : _floatError(type, min, val, max);


  static bool check(double n, List<String> issues, double min, double max) {
    if (n < min) {
      issues.add('Value $n is less than the minimum($min) allowed value.');
      return false;
    }
    if (n > max) {
      issues.add('Value $n is greater than the maximum($min) allowed value.');
      return false;
    }
    return true;
  }

  /// Returns a [List<int>] if all values are [int], otherwise null.
  @deprecated
  static List<double> validate(List<double> values, _InRange inRange) =>
      listGuard(values, inRange);

  /// Returns a [List<int>] if all values are [int], otherwise null.
  static List<double> listGuard(List<double> values, _InRange inRange) {
    print('values: $values');
    for (int i = 0; i < values.length; i++)
      if ((values[i] is double) && inRange(values[i])) {
        print('values: $values');
        return values;
      }
    return null;
  }

  /// Returns a [List<int>] if all values are [int], otherwise [null].
  static bool isValidList(List<double> vList, _InRange inRange) {
    if (listGuard(vList, inRange) == null) return false;
    return true;
  }

  static bool isNotValidList(List<double> vList, _InRange inRange) =>
      !isValidList(vList, inRange);
}

class Float32 extends Float {
  static const name = "Float32";
  static const int sizeInBits = 32;
  static const int sizeInBytes = 4;

  //TODO: test
  static const min = 0x80800000;
  //TODO: test
  static const max = 0x7F7FFFFF;
  //TODO: test
  static const pZero = 0x00000000;
  //TODO: test
  static const mZero = 0x80000000;
  //TODO
  static const pInfinity = 0x7F800000;
  //TODO
  static const nInfinity = 0xFF800000;
  //TODO
  static const qNaN = 0x7FC00000;
  //TODO
  static const sNaN = 0x7FC00000;
  static const int maxShortLength = kMaxShortLengthInBytes ~/ sizeInBytes;
  static const int maxLongLength = kMaxLongLengthInBytes ~/ sizeInBytes;
  static final Float32List emptyList = new Float32List(0);

  static bool inRange(double val) => (val >= min) && (val <= max);

  static double guard(double min, double val, double max) =>
      inRange(val) ? val : _floatError("Float32", min, val, max);

  /// Returns a [values] if all values are valid Uint32, otherwise null.
  @deprecated
  static Float32List validate(Float32List values) => listGuard(values);

  /// Returns it argument [vList] if valid; otherwise, returns [null].
  static List<double> listGuard(List<double> vList) {
    if (vList == null || vList.length == 0) throw "invalid Float32: $vList";
    //return Float.listGuard(vList, inRange);
    return vList;
  }

  /// Returns a [true] if all values are valid Uint32, otherwise [false].
  static bool isValidList(List<double> vList) =>
      (listGuard(vList) == null) ? false : true;

  static bool isNotValidList(List<double> vList) =>
      !isValidList(vList);

  static bool isAligned(int offsetInBytes) => offsetInBytes % sizeInBytes == 0;

  static const shiftValue = 2;

  static int toLength(int lengthInBytes) => lengthInBytes >> shiftValue;

  static int toLengthInBytes(int length) => length << shiftValue;

  /// Returns a [Float32List] created from [bytes]. if  [offsetInBytes] is aligned
  /// on an 8-byte boundary, then a [Float32List.view] is returned; otherwise,
  /// the [bytes] are copied to a [new] [Float64List].
  static Float32List viewOfBytes(Uint8List bytes, [int offsetInBytes = 0, int length]) {
    if (offsetInBytes < 0) throw new ArgumentError('Invalid offsetInBytes($offsetInBytes)');
    if (length == null) length = bytes.lengthInBytes >> shiftValue;
    if (length < 0) throw new ArgumentError('Invalid length($length)');
    if (isAligned(offsetInBytes)) {
      return bytes.buffer.asFloat32List(offsetInBytes, length);
    } else {
      Float32List vList = new Float32List(length);
      ByteData bd = bytes.buffer.asByteData(offsetInBytes, length >> shiftValue);
      for (int i = 0; i < length; i++, offsetInBytes += sizeInBytes)
        vList[i] = bd.getFloat32(offsetInBytes);
      return vList;
    }
  }

  static toFloat32List(List<double> list) =><commit_msg>interim - has to be implemented if multiple values separated by '/'<commit_after>// Copyright (c) 2016, Open DICOMweb Project. All rights reserved.
// Use of this source code is governed by the open source license
// that can be found in the LICENSE file.
// Author: <NAME> <<EMAIL>.edu> -
// See the AUTHORS file for other contributors.

import 'dart:typed_data';

import 'package:dictionary/src/dicom/constants.dart';

/// Floating Point Data Types

//TODO: need work.

/// The [Type] of Range checkers.
typedef bool _InRange(double val);

double _floatError(String type, double min, double val, double max) {
  throw new RangeError('$type: out of range (min <= val <= max');
}

class Float {
  static String get type => "Float";

  static bool inRange(double min, double val, double max) => (min <= val && val <= max);

  static double guard(double min, double val, double max) =>
      inRange(min, val, max) ? val : _floatError(type, min, val, max);


  static bool check(double n, List<String> issues, double min, double max) {
    if (n < min) {
      issues.add('Value $n is less than the minimum($min) allowed value.');
      return false;
    }
    if (n > max) {
      issues.add('Value $n is greater than the maximum($min) allowed value.');
      return false;
    }
    return true;
  }

  /// Returns a [List<int>] if all values are [int], otherwise null.
  @deprecated
  static List<double> validate(List<double> values, _InRange inRange) =>
      listGuard(values, inRange);

  /// Returns a [List<int>] if all values are [int], otherwise null.
  static List<double> listGuard(List<double> values, _InRange inRange) {
    print('values: $values');
    for (int i = 0; i < values.length; i++)
      if ((values[i] is double) && inRange(values[i])) {
        print('values: $values');
        return values;
      }
    return null;
  }

  /// Returns a [List<int>] if all values are [int], otherwise [null].
  static bool isValidList(List<double> vList, _InRange inRange) {
    if (listGuard(vList, inRange) == null) return false;
    return true;
  }

  static bool isNotValidList(List<double> vList, _InRange inRange) =>
      !isValidList(vList, inRange);
}

class Float32 extends Float {
  static const name = "Float32";
  static const int sizeInBits = 32;
  static const int sizeInBytes = 4;

  //TODO: test
  static const min = 0x80800000;
  //TODO: test
  static const max = 0x7F7FFFFF;
  //TODO: test
  static const pZero = 0x00000000;
  //TODO: test
  static const mZero = 0x80000000;
  //TODO
  static const pInfinity = 0x7F800000;
  //TODO
  static const nInfinity = 0xFF800000;
  //TODO
  static const qNaN = 0x7FC00000;
  //TODO
  static const sNaN = 0x7FC00000;
  static const int maxShortLength = kMaxShortLengthInBytes ~/ sizeInBytes;
  static const int maxLongLength = kMaxLongLengthInBytes ~/ sizeInBytes;
  static final Float32List emptyList = new Float32List(0);

  static bool inRange(double val) => (val >= min) && (val <= max);

  static double guard(double min, double val, double max) =>
      inRange(val) ? val : _floatError("Float32", min, val, max);

  /// Returns a [values] if all values are valid Uint32, otherwise null.
  @deprecated
  static Float32List validate(Float32List values) => listGuard(values);

  /// Returns it argument [vList] if valid; otherwise, returns [null].
  static List<double> listGuard(List<double> vList) {
    if (vList == null || vList.length == 0) throw "invalid Float32: $vList";
    //return Float.listGuard(vList, inRange);
    return vList;
  }

  /// Returns a [true] if all values are valid Uint32, otherwise [false].
  static bool isValidList(List<double> vList) =>
      (listGuard(vList) == null) ? false : true;

  static bool isNotValidList(List<double> vList) =>
      !isValidList(vList);

  static bool isAligned(int offsetInBytes) => offsetInBytes % sizeInBytes == 0;

  static const shiftValue = 2;

  static int toLength(int lengthInBytes) => lengthInBytes >> shiftValue;

  static int toLengthInBytes(int length) => length << shiftValue;

  /// Returns a [Float32List] created from [bytes]. if  [offsetInBytes] is aligned
  /// on an 8-byte boundary, then a [Float32List.view] is returned; otherwise,
  /// the [bytes] are copied to a [new] [Float64List].
  static Float32List viewOfBytes(Uint8List bytes, [int offsetInBytes = 0, int length]) {
    if (offsetInBytes < 0) throw new ArgumentError('Invalid offsetInBytes($offsetInBytes)');
    if (length == null) length = bytes.lengthInBytes >> shiftValue;
    if (length < 0) throw new ArgumentError('Invalid length($length)');
    if (isAligned(offsetInBytes)) {
      var f32List = [];
      f32List.add(double.parse(new String.fromCharCodes(bytes)));
      return new Float32List.fromList(f32List);
      //return bytes.buffer.asFloat32List(offsetInBytes, length); unexpected result
    } else {
      Float32List vList = new Float32List(length);
      ByteData bd = bytes.buffer.asByteData(offsetInBytes, length >> shiftValue);
      for (int i = 0; i < length; i++, offsetInBytes += sizeInBytes)
        vList[i] = bd.getFloat32(offsetInBytes);
      return vList;
    }
  }

  static toFloat32List(List<double> list) =>
{'fraction_non_alphanumeric': 0.1228018178225647, 'fraction_numerical': 0.02351313969571231, 'mean_word_length': 3.57226738934056, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '4006666', 'n_tokens_mistral': 3363, 'n_tokens_neox': 3161, 'n_words': 1061}