All Downloads are FREE. Search and download functionalities are using the official Maven repository.

convex.core.data.Sets Maven / Gradle / Ivy

The newest version!
package convex.core.data;

import java.util.ArrayList;
import java.util.Collection;

import convex.core.exceptions.BadFormatException;
import convex.core.lang.RT;
import convex.core.util.Utils;

public class Sets {
	
	static final Ref[] EMPTY_ENTRIES = new Ref[0];

	@SuppressWarnings({ "rawtypes", "unchecked" })
	static final SetLeaf EMPTY = Cells.intern(new SetLeaf(EMPTY_ENTRIES));
	
	@SuppressWarnings("rawtypes")
	public static final Ref EMPTY_REF = EMPTY.getRef();

	@SuppressWarnings("unchecked")
	public static  SetLeaf empty() {
		return (SetLeaf) EMPTY;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static  Ref> emptyRef() {
		return (Ref)EMPTY_REF;
	}

	@SafeVarargs
	public static  ASet of(Object... elements) {
		int n=elements.length;
		ASet result=empty();
		for (int i=0; i) result.conj(RT.cvm(elements[i]));
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@SafeVarargs
	public static  ASet of(ACell... elements) {
		int n=elements.length;
		ASet result=empty();
		for (int i=0; i) result.conj((T) elements[i]);
		}
		return result;
 	}

	/**
	 * Creates a set of all the elements in the given data structure
	 * 
	 * @param  Type of elements
	 * @param source Source for elements
	 * @return A Set
	 */
	@SuppressWarnings("unchecked")
	public static  ASet create(ACountable source) {
		if (source==null) return EMPTY;
		if (source instanceof ASet) return (ASet) source;

		if (source instanceof AMap) {
			ASequence seq = RT.sequence(source); // should always be non-null
			return Sets.create(seq);
		}
		if (source instanceof ACountable) return Sets.fromCollection(source);
		throw new IllegalArgumentException("Unexpected type!" + Utils.getClass(source));
	}
	
	/**
	 * Creates a set of all the elements in the given data structure
	 * 
	 * @param  Type of elements
	 * @param source Source for elements
	 * @return A Set
	 */
	public static  ASet fromCollection(Collection source) {
		return Sets.of(source.toArray());
	}
	
	/**
	 * Creates a set of all the elements in the given data structure
	 * 
	 * @param  Type of elements
	 * @param source Source for elements
	 * @return A Set
	 */
	@SuppressWarnings("unchecked")
	private static  ASet fromCollection(ACountable source) {
		long n=source.count();
		ASet set=EMPTY;
		for (long i=0; i) set;
	}

	public static  ASet read(Blob b, int pos) throws BadFormatException {
		long count = Format.readVLCLong(b,pos+1);
		if (count <= SetLeaf.MAX_ELEMENTS) {
			return SetLeaf.read(b, pos, count);
		} else {
			return SetTree.read(b, pos, count);
		}
	}

	public static  AHashSet createWithShift(int shift, ArrayList> values) {
		AHashSet result=Sets.empty();
		for (Ref v: values) {
			result=result.includeRef(v, shift);
		}
		return result;
	}



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy