Skip to content

Commit

Permalink
ObjectInt(Ordered)Map...
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyettinger committed May 15, 2024
1 parent 519f8d1 commit c9fc014
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 34 deletions.
172 changes: 155 additions & 17 deletions src/main/java/com/github/tommyettinger/ds/ObjectIntMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.github.tommyettinger.ds;

import com.github.tommyettinger.digital.Base;
import com.github.tommyettinger.digital.BitConversion;
import com.github.tommyettinger.ds.support.util.IntIterator;
import com.github.tommyettinger.function.IntIntToIntBiFunction;
Expand Down Expand Up @@ -681,33 +682,170 @@ public String toString () {
return toString(", ", true);
}

protected String toString (String separator, boolean braces) {
if (size == 0) {return braces ? "{}" : "";}
StringBuilder buffer = new StringBuilder(32);
if (braces) {buffer.append('{');}
K[] keyTable = this.keyTable;
public String toString (String separator, boolean braces) {
return appendAsString(new StringBuilder(32), separator, braces).toString();
}

public StringBuilder appendAsString (StringBuilder sb, String separator, boolean braces) {
if (size == 0) {return braces ? sb.append("{}") : sb;}
if (braces) {sb.append('{');}
Object[] keyTable = this.keyTable;
int[] valueTable = this.valueTable;
int i = keyTable.length;
while (i-- > 0) {
K key = keyTable[i];
Object key = keyTable[i];
if (key == null) {continue;}
buffer.append(key == this ? "(this)" : key);
buffer.append('=');
int value = valueTable[i];
buffer.append(value);
sb.append(key == this ? "(this)" : key);
sb.append('=');
sb.append(valueTable[i]);
break;
}
while (i-- > 0) {
K key = keyTable[i];
Object key = keyTable[i];
if (key == null) {continue;}
buffer.append(separator);
buffer.append(key == this ? "(this)" : key);
buffer.append('=');
sb.append(separator);
sb.append(key == this ? "(this)" : key);
sb.append('=');
int value = valueTable[i];
buffer.append(value);
sb.append(value);
}
if (braces) {sb.append('}');}
return sb;
}

/**
* Creates a String from the contents of this ObjectIntMap, but uses {@link Base#BASE10} to convert each
* value to their unsigned String representations in base-10. For example, values will look like
* {@code 12345123451234512345} . This will not apply any prefixes or suffixes around keys or values.
*
* @return the String representation of the unsigned keys and unsigned values of this map
*/
public String toStringUnsigned () {
return toStringUnsigned(", ", "=", true, Base.BASE10, "", "", "", "");
}

/**
* Creates a String from the contents of this ObjectIntMap, but uses the given {@link Base} to convert each
* value to their unsigned String representations in that base. For example, values will look like
* {@code 12345123451234512345} . This will not apply any prefixes or suffixes around keys or values.
*
* @param separator how to separate entries, such as {@code ", "}
* @param braces true to wrap the output in curly braces, or false to omit them
* @param base a {@link Base} from digital to use for both keys and values ({@link Base#BASE16} is suggested)
* @return the String representation of the unsigned keys and unsigned values of this map
*/
public String toStringUnsigned (String separator, boolean braces, Base base) {
return toStringUnsigned(separator, "=", braces, base, "", "", "", "");
}

/**
* Creates a String from the contents of this ObjectIntMap, but uses the given {@link Base} to convert each
* value to their unsigned String representations in that base. For example, values will look like
* {@code 12345123451234512345} . This will apply the given prefixes or suffixes around keys or values; if a
* prefix/suffix is {@code null}, then this will insert the literal text {@code "null"}.
*
* @param entrySeparator how to separate entries, such as {@code ", "}
* @param keyValueSeparator how to separate each key from its value, such as {@code "="} or {@code ":"}
* @param braces true to wrap the output in curly braces, or false to omit them
* @param base a {@link Base} from digital to use for both keys and values ({@link Base#BASE16} is suggested)
* @param keyPrefix a String that will be at the start of each key ({@code "0x"} is suggested)
* @param keySuffix a String that will be at the end of each key ({@code ""} is suggested)
* @param valuePrefix a String that will be at the start of each value ({@code ""} is suggested)
* @param valueSuffix a String that will be at the end of each value ({@code "L"} is suggested)
* @return the String representation of the unsigned keys and unsigned values of this map
*/
public String toStringUnsigned (String entrySeparator, String keyValueSeparator, boolean braces, Base base,
String keyPrefix, String keySuffix, String valuePrefix, String valueSuffix) {
return appendUnsigned(new StringBuilder(32), entrySeparator, keyValueSeparator, braces, base, keyPrefix, keySuffix, valuePrefix, valueSuffix).toString();
}

/**
* Appends to a StringBuilder from the contents of this ObjectIntMap, but uses the given {@link Base} to convert each
* value to their unsigned String representations in that base. For example, values will look like
* {@code 12345123451234512345} . This will apply the given prefixes or suffixes around keys or values; if a
* prefix/suffix is {@code null}, then this will insert the literal text {@code "null"}.
*
* @param sb a StringBuilder that this can append to
* @param entrySeparator how to separate entries, such as {@code ", "}
* @param keyValueSeparator how to separate each key from its value, such as {@code "="} or {@code ":"}
* @param braces true to wrap the output in curly braces, or false to omit them
* @param base a {@link Base} from digital to use for both keys and values ({@link Base#BASE16} is suggested)
* @param keyPrefix a String that will be at the start of each key
* @param keySuffix a String that will be at the end of each key
* @param valuePrefix a String that will be at the start of each value
* @param valueSuffix a String that will be at the end of each value
* @return {@code sb}, with the unsigned keys and unsigned values of this map
*/
public StringBuilder appendUnsigned (StringBuilder sb, String entrySeparator, String keyValueSeparator, boolean braces, Base base,
String keyPrefix, String keySuffix, String valuePrefix, String valueSuffix) {
if (size == 0) {return braces ? sb.append("{}") : sb;}
if (braces) {sb.append('{');}
Object[] keyTable = this.keyTable;
int[] valueTable = this.valueTable;
int i = keyTable.length;
while (i-- > 0) {
Object key = keyTable[i];
if (key == null) {continue;}
sb.append(keyPrefix).append(key == this ? "(this)" : key).append(keySuffix).append(keyValueSeparator);
base.appendUnsigned(sb.append(valuePrefix), valueTable[i]).append(valueSuffix);
break;
}
while (i-- > 0) {
Object key = keyTable[i];
if (key == null) {continue;}
sb.append(entrySeparator);
sb.append(keyPrefix).append(key == this ? "(this)" : key).append(keySuffix).append(keyValueSeparator);
base.appendUnsigned(sb.append(valuePrefix), valueTable[i]).append(valueSuffix);
}
if (braces) {sb.append('}');}
return sb;
}

/**
* Creates a String from the contents of this ObjectIntMap, but uses {@link Base#appendReadable(StringBuilder, int)}
* to convert each value to Java-readable String representations in base-10. This will separate keys
* from values with {@code ", "}, and will also separate entries with {@code ", "}. This does not wrap the output in
* any braces, and allocates a new StringBuilder on each call.
*
* @return a new StringBuilder with any keys and values separated by {@code ", "}, written so Java can read them
*/
public StringBuilder appendReadable () {
return appendReadable(new StringBuilder(32), false);
}

/**
* Creates a String from the contents of this ObjectIntMap, but uses {@link Base#appendReadable(StringBuilder, int)}
* to convert each value to Java-readable String representations in base-10. This will separate keys
* from values with {@code ", "}, and will also separate entries with {@code ", "}. If {@code braces} is true, then
* the output will be surrounded by curly braces.
*
* @param sb a StringBuilder that this can append to
* @param braces true to wrap the output in curly braces, or false to omit them
* @return {@code sb}, with any keys and values separated by {@code ", "}, written so Java can read them
*/
public StringBuilder appendReadable (final StringBuilder sb, boolean braces) {
if (size == 0) {return braces ? sb.append("{}") : sb;}
final String separator = ", ";
if (braces) {sb.append('{');}
Object[] keyTable = this.keyTable;
int[] valueTable = this.valueTable;
int i = keyTable.length;
while (i-- > 0) {
Object key = keyTable[i];
if (key == null) {continue;}
sb.append(key).append(", ");
Base.appendReadable(sb, valueTable[i]);
break;
}
while (i-- > 0) {
Object key = keyTable[i];
if (key == null) {continue;}
sb.append(separator);
sb.append(key).append(", ");
Base.appendReadable(sb, valueTable[i]);
}
if (braces) {buffer.append('}');}
return buffer.toString();
if (braces) {sb.append('}');}
return sb;
}

/**
Expand Down
57 changes: 46 additions & 11 deletions src/main/java/com/github/tommyettinger/ds/ObjectIntOrderedMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.github.tommyettinger.ds;

import com.github.tommyettinger.digital.Base;
import com.github.tommyettinger.ds.support.sort.IntComparator;
import com.github.tommyettinger.ds.support.sort.IntComparators;

Expand Down Expand Up @@ -558,24 +559,58 @@ public Entries<K> entrySet () {
return entrySet().iterator();
}

public StringBuilder appendAsString (StringBuilder sb, String separator, boolean braces) {
if (size == 0) {return braces ? sb.append("{}") : sb;}
if (braces) {sb.append('{');}
ObjectList<K> keys = this.keys;
for (int i = 0, n = keys.size(); i < n; i++) {
K key = keys.get(i);
if (i > 0) {sb.append(separator);}
sb.append(key);
sb.append('=');
sb.append(get(key));
}
if (braces) {sb.append('}');}
return sb;
}

@Override
public StringBuilder appendUnsigned (StringBuilder sb, String entrySeparator, String keyValueSeparator, boolean braces, Base base,
String keyPrefix, String keySuffix, String valuePrefix, String valueSuffix) {
if (size == 0) {return braces ? sb.append("{}") : sb;}
if (braces) {sb.append('{');}
ObjectList<K> keys = this.keys;
for (int i = 0, n = keys.size(); i < n; i++) {
K key = keys.get(i);
if (i > 0)
sb.append(entrySeparator);
sb.append(keyPrefix).append(key).append(keySuffix).append(keyValueSeparator);
base.appendUnsigned(sb.append(valuePrefix), get(key)).append(valueSuffix);
}
if (braces) {sb.append('}');}
return sb;
}

@Override
protected String toString (String separator, boolean braces) {
if (size == 0) {return braces ? "{}" : "";}
StringBuilder buffer = new StringBuilder(32);
if (braces) {buffer.append('{');}
public StringBuilder appendReadable (StringBuilder sb, boolean braces) {
if (size == 0)
return braces ? sb.append("{}") : sb;
if (braces)
sb.append('{');
ObjectList<K> keys = this.keys;
for (int i = 0, n = keys.size(); i < n; i++) {
K key = keys.get(i);
if (i > 0) {buffer.append(separator);}
buffer.append(key == this ? "(this)" : key);
buffer.append('=');
int value = get(key);
buffer.append(value);
if (i > 0)
sb.append(", ");
sb.append(key).append(", ");
Base.appendReadable(sb, get(key));
}
if (braces) {buffer.append('}');}
return buffer.toString();
if (braces)
sb.append('}');
return sb;
}


public static class OrderedMapEntries<K> extends Entries<K> {
protected ObjectList<K> keys;

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/github/tommyettinger/ds/ObjectLongMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ public StringBuilder appendAsString (StringBuilder sb, String separator, boolean
}

/**
* Creates a String from the contents of this ObjectFloatMap, but uses {@link Base#BASE10} to convert each
* Creates a String from the contents of this ObjectLongMap, but uses {@link Base#BASE10} to convert each
* value to their unsigned String representations in base-10. For example, values will look like
* {@code 12345123451234512345} . This will not apply any prefixes or suffixes around keys or values.
*
Expand All @@ -727,7 +727,7 @@ public String toStringUnsigned () {
}

/**
* Creates a String from the contents of this ObjectFloatMap, but uses the given {@link Base} to convert each
* Creates a String from the contents of this ObjectLongMap, but uses the given {@link Base} to convert each
* value to their unsigned String representations in that base. For example, values will look like
* {@code 12345123451234512345} . This will not apply any prefixes or suffixes around keys or values.
*
Expand All @@ -741,7 +741,7 @@ public String toStringUnsigned (String separator, boolean braces, Base base) {
}

/**
* Creates a String from the contents of this ObjectFloatMap, but uses the given {@link Base} to convert each
* Creates a String from the contents of this ObjectLongMap, but uses the given {@link Base} to convert each
* value to their unsigned String representations in that base. For example, values will look like
* {@code 12345123451234512345} . This will apply the given prefixes or suffixes around keys or values; if a
* prefix/suffix is {@code null}, then this will insert the literal text {@code "null"}.
Expand All @@ -762,7 +762,7 @@ public String toStringUnsigned (String entrySeparator, String keyValueSeparator,
}

/**
* Appends to a StringBuilder from the contents of this ObjectFloatMap, but uses the given {@link Base} to convert each
* Appends to a StringBuilder from the contents of this ObjectLongMap, but uses the given {@link Base} to convert each
* value to their unsigned String representations in that base. For example, values will look like
* {@code 12345123451234512345} . This will apply the given prefixes or suffixes around keys or values; if a
* prefix/suffix is {@code null}, then this will insert the literal text {@code "null"}.
Expand Down Expand Up @@ -804,7 +804,7 @@ public StringBuilder appendUnsigned (StringBuilder sb, String entrySeparator, St
}

/**
* Creates a String from the contents of this ObjectFloatMap, but uses {@link Base#appendReadable(StringBuilder, int)}
* Creates a String from the contents of this ObjectLongMap, but uses {@link Base#appendReadable(StringBuilder, int)}
* to convert each value to Java-readable String representations in base-10. This will separate keys
* from values with {@code ", "}, and will also separate entries with {@code ", "}. This does not wrap the output in
* any braces, and allocates a new StringBuilder on each call.
Expand All @@ -816,7 +816,7 @@ public StringBuilder appendReadable () {
}

/**
* Creates a String from the contents of this ObjectFloatMap, but uses {@link Base#appendReadable(StringBuilder, int)}
* Creates a String from the contents of this ObjectLongMap, but uses {@link Base#appendReadable(StringBuilder, int)}
* to convert each value to Java-readable String representations in base-10. This will separate keys
* from values with {@code ", "}, and will also separate entries with {@code ", "}. If {@code braces} is true, then
* the output will be surrounded by curly braces.
Expand Down

0 comments on commit c9fc014

Please sign in to comment.