/** * Construct the array with the given length and expected density * @param length the length * @param density the density */ public SparseHashedShortArray(int length, float density) { if (length < 0) throw new IllegalArgumentException("length must be >= 0"); if (density <= 0 || density > 1) throw new IllegalArgumentException("density must be > 0 and < 1"); this.length = length; int capacity = (int) (density * length); this.data = new TIntShortHashMap(capacity); }
/** * Construct the array with the given length and capacity for non-zero elements * @param length the length * @param capacity the capacity */ public SparseHashedShortArray(int length, int capacity) { if (length < 0) throw new IllegalArgumentException("length must be >= 0"); if (capacity <= 0) throw new IllegalArgumentException("capacity must be > 0"); this.length = length; this.data = new TIntShortHashMap(capacity); }
@Override public SparseShortArray copy() { SparseHashedShortArray copy = new SparseHashedShortArray(length); copy.data = new TIntShortHashMap(data); return copy; }
/** * Constructor * @param length the length for this array */ SparseArrayOfZonedDateTimes(int length, ZonedDateTime defaultValue) { super(ZonedDateTime.class, ArrayStyle.SPARSE, false); this.length = length; this.defaultValue = defaultValue; this.defaultValueAsLong = defaultValue != null ? defaultValue.toInstant().toEpochMilli() : nullValue; this.defaultZoneId = defaultValue != null ? zoneIdMap1.get(defaultValue.getZone()) : NULL_ZONE; this.values = new TIntLongHashMap((int)Math.max(length * 0.5, 10d), 0.8f, -1, defaultValueAsLong); this.zoneIds = new TIntShortHashMap((int)Math.max(length * 0.5, 10d), 0.8f, -1, defaultZoneId); }
@Override() public final Array<ZonedDateTime> copy() { try { final SparseArrayOfZonedDateTimes copy = (SparseArrayOfZonedDateTimes)super.clone(); copy.values = new TIntLongHashMap(values); copy.zoneIds = new TIntShortHashMap(zoneIds); copy.defaultValue = this.defaultValue; copy.defaultValueAsLong = this.defaultValueAsLong; return copy; } catch (Exception ex) { throw new ArrayException("Failed to copy Array: " + this, ex); } }
@Override public SparseShortArray reverse() { //TODO: this could be more efficient and avoid the copy TIntShortHashMap tmp = new TIntShortHashMap(data.size()); for (Entry e : entries()) tmp.put(length - e.index, e.value); this.data = tmp; return this; } }