<commit_before>package som.vmobjects;

import java.util.Arrays;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.profiles.ValueProfile;

import som.vm.NotYetImplementedException;
import som.vm.constants.Nil;

/**
 * SArrays are implemented using a Strategy-like approach.
 * The SArray objects are 'tagged' with a type, and the strategy behavior
 * is implemented directly in the AST nodes.
 */
public abstract class SArray extends SAbstractObject {
  public static final int FIRST_IDX = 0;

  protected Object storage;
  protected final SClass clazz;

  public SArray(final long length, final SClass clazz) {
    storage = (int) length;
    this.clazz = clazz;
  }

  public SArray(final Object storage, final SClass clazz) {
    assert !(storage instanceof Long);
    assert storage != null;
    this.storage = storage;
    this.clazz   = clazz;
  }

  @Override
  public final SClass getSOMClass() {
    return clazz;
  }

  public Object getStoragePlain() {
    CompilerAsserts.neverPartOfCompilation();
    return storage;
  }

  public int getEmptyStorage(final ValueProfile storageType) {
    assert isEmptyType();
    return (int) storageType.profile(storage);
  }

  public PartiallyEmptyArray getPartiallyEmptyStorage(final ValueProfile storageType) {
    assert isPartiallyEmptyType();
    return (PartiallyEmptyArray) storageType.profile(storage);
  }

  public Object[] getObjectStorage(final ValueProfile storageType) {
    assert isObjectType();
    return (Object[]) storageType.profile(storage);
  }

  public long[] getLongStorage(final ValueProfile storageType) {
    assert isLongType();
    return (long[]) storageType.profile(storage);
  }

  public double[] getDoubleStorage(final ValueProfile storageType) {
    assert isDoubleType();
    return (double[]) storageType.profile(storage);
  }

  public boolean[] getBooleanStorage(final ValueProfile storageType) {
    assert isBooleanType();
    return (boolean[]) storageType.profile(storage);
  }

  public boolean isEmptyType() {
    return storage instanceof Integer;
  }

  public boolean isPartiallyEmptyType() {
    return storage instanceof PartiallyEmptyArray;
  }

  public boolean isObjectType()  { return storage instanceof Object[]; }
  public boolean isLongType()    { return storage instanceof long[];   }
  public boolean isDoubleType()  { return storage instanceof double[]; }
  public boolean isBooleanType() { return storage instanceof boolean[]; }

  public boolean isSomePrimitiveType() {
    return isLongType() || isDoubleType() || isBooleanType();
  }


  private static long[] createLong(final Object[] arr) {
    long[] storage = new long[arr.length];
    for (int i = 0; i < arr.length; i++) {
      storage[i] = (long) arr[i];
    }
    return storage;
  }

  private static double[] createDouble(final Object[] arr) {
    double[] storage = new double[arr.length];
    for (int i = 0; i < arr.length; i++) {
      storage[i] = (double) arr[i];
    }
    return storage;
  }

  private static boolean[] createBoolean(final Object[] arr) {
    boolean[] storage = new boolean[arr.length];
    for (int i = 0; i < arr.length; i++) {
      storage[i] = (boolean) arr[i];
    }
    return storage;<commit_msg>Use more precise checks for array guards<commit_after>package som.vmobjects;

import java.util.Arrays;

import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.profiles.ValueProfile;

import som.vm.NotYetImplementedException;
import som.vm.constants.Nil;

/**
 * SArrays are implemented using a Strategy-like approach.
 * The SArray objects are 'tagged' with a type, and the strategy behavior
 * is implemented directly in the AST nodes.
 */
public abstract class SArray extends SAbstractObject {
  public static final int FIRST_IDX = 0;

  protected Object storage;
  protected final SClass clazz;

  public SArray(final long length, final SClass clazz) {
    storage = (int) length;
    this.clazz = clazz;
  }

  public SArray(final Object storage, final SClass clazz) {
    assert !(storage instanceof Long);
    assert storage != null;
    this.storage = storage;
    this.clazz   = clazz;
  }

  @Override
  public final SClass getSOMClass() {
    return clazz;
  }

  public Object getStoragePlain() {
    CompilerAsserts.neverPartOfCompilation();
    return storage;
  }

  public int getEmptyStorage(final ValueProfile storageType) {
    assert isEmptyType();
    return (int) storageType.profile(storage);
  }

  public PartiallyEmptyArray getPartiallyEmptyStorage(final ValueProfile storageType) {
    assert isPartiallyEmptyType();
    return (PartiallyEmptyArray) storageType.profile(storage);
  }

  public Object[] getObjectStorage(final ValueProfile storageType) {
    assert isObjectType();
    return (Object[]) storageType.profile(storage);
  }

  public long[] getLongStorage(final ValueProfile storageType) {
    assert isLongType();
    return (long[]) storageType.profile(storage);
  }

  public double[] getDoubleStorage(final ValueProfile storageType) {
    assert isDoubleType();
    return (double[]) storageType.profile(storage);
  }

  public boolean[] getBooleanStorage(final ValueProfile storageType) {
    assert isBooleanType();
    return (boolean[]) storageType.profile(storage);
  }

  public boolean isEmptyType() {
    return storage instanceof Integer;
  }

  public boolean isPartiallyEmptyType() {
    return storage instanceof PartiallyEmptyArray;
  }

  public boolean isObjectType()  { return storage.getClass() == Object[].class; }
  public boolean isLongType()    { return storage.getClass() == long[].class;   }
  public boolean isDoubleType()  { return storage.getClass() == double[].class; }
  public boolean isBooleanType() { return storage.getClass() == boolean[].class; }

  public boolean isSomePrimitiveType() {
    return isLongType() || isDoubleType() || isBooleanType();
  }


  private static long[] createLong(final Object[] arr) {
    long[] storage = new long[arr.length];
    for (int i = 0; i < arr.length; i++) {
      storage[i] = (long) arr[i];
    }
    return storage;
  }

  private static double[] createDouble(final Object[] arr) {
    double[] storage = new double[arr.length];
    for (int i = 0; i < arr.length; i++) {
      storage[i] = (double) arr[i];
    }
    return storage;
  }

  private static boolean[] createBoolean(final Object[] arr) {
    boolean[] storage = new boolean[arr.length];
    for (int i = 0; i < arr.length; i++) {
      storage[i] = (boolean) arr[i];
    }
    return storage;
{'fraction_non_alphanumeric': 0.107945122870496, 'fraction_numerical': 0.0012060907583295642, 'mean_word_length': 3.9323420074349444, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '2949595', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1885, 'n_words': 555}