Package convex.core.data.prim
Class CVMBigInteger
java.lang.Object
convex.core.data.AObject
convex.core.data.ACell
convex.core.data.prim.APrimitive
convex.core.data.prim.ANumeric
convex.core.data.prim.AInteger
convex.core.data.prim.CVMBigInteger
- All Implemented Interfaces:
IValidated,IWriteable,Comparable<ANumeric>
Arbitrary precision Integer implementation for the CVM.
A CVMBigInteger is a canonical CVM value if and only if it represents a number that cannot be a CVMLong
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final longprotected static final longstatic final CVMBigIntegerstatic final BigIntegerstatic final CVMBigIntegerstatic final BigIntegerFields inherited from class convex.core.data.ACell
cachedRef, memorySize -
Method Summary
Modifier and TypeMethodDescriptionabs()Gets the absolute value of this number.Adds another integer to this integerbig()Converts this integer to a Java BigInteger.protected ABlobblob()protected BigIntegerprotected ABloblongNumber of bytes in minimal representation of this Integer.protected longCalculates the Memory Size for this Cell.intstatic CVMBigIntegerCreate a big integer from a valid blob representation.dec()Decrements this IntegerDivides this integer with a given denominator.doubleintencode(byte[] bs, int pos) Writes this Cell's encoding to a byte array, including a tag byte which will be written first.protected intencodeRaw(byte[] bs, int pos) Writes this Cell's encoding to a byte array, excluding the tag byte.Gets the numeric value as a Long integer, or null if not a valid LongbooleanChecks for equality with another Cell.booleanintEstimate the encoded data size for this Cell.Gets the Java BigInteger representing this numbergetRef(int i) Gets a numbered child Ref from within this Cell.intGets the number of Refs contained within this Cell.bytegetTag()Gets the tag byte for this cell.inc()Increments this IntegerbooleanReturns true if this Cell is in a canonical representation.booleanDetermines if this Cell Represents an embedded object.booleanisLong()Return true if this value is a valid 64-bit long integerbooleanisZero()Check if this numeric value is equal to zerolongReturns the modulus of this integer with a given integer baseMultiplies a second numeric value with this valuenegate()Negates this numeric valueClass<?> Gets the numeric type that should be used as for calculationsstatic CVMBigIntegerParses a string as a CVMBigInteger.booleanprint(BlobBuilder sb, long limit) Prints this Object to a readable String Representation.Divides this integer with a given denominator.static CVMBigIntegerReturns the remainder of dividing this integer with a given divisorsignum()Gets the signum of this numerical value.Subtracts another integer from this integertoBlob()Converts this Integer value to Blob formConverts this Cell to a canonical version.toDouble()Casts to a CVM Double value.toLong()Casts to a CVM Long value.toString()Returns the Java String representation of this Cell.voidValidates the local structure and invariants of this cell.static CVMBigIntegerwrap(byte[] bs) Creates a CVMBigInteger WARNING: might not be canonicalstatic CVMBigIntegerwrap(BigInteger value) Creates a CVMBigInteger WARNING: might not be canonicalMethods inherited from class convex.core.data.prim.AInteger
add, create, create, create, getType, parse, sub, toInteger, toPowerMethods inherited from class convex.core.data.prim.APrimitive
isCVMValue, isDataValue, updateRefsMethods inherited from class convex.core.data.ACell
attachMemorySize, attachRef, cachedEncoding, cachedHash, createEncoding, createRef, equals, genericEquals, getCanonical, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getMemorySize, getRef, hashCode, toCVMString, validateMethods inherited from class convex.core.data.AObject
attachEncoding, print, print
-
Field Details
-
MIN_POSITIVE
-
MIN_NEGATIVE
-
MIN_POSITIVE_BIG
-
MIN_NEGATIVE_BIG
-
LONG_BYTELENGTH
protected static final long LONG_BYTELENGTH- See Also:
-
MAX_BYTELENGTH
protected static final long MAX_BYTELENGTH- See Also:
-
-
Method Details
-
wrap
Creates a CVMBigInteger WARNING: might not be canonical- Parameters:
bs- Bytes representing BigInteger value. Highest bit assumed to be sign.- Returns:
- CVMBigInteger instance or null if not valid
-
wrap
Creates a CVMBigInteger WARNING: might not be canonical- Parameters:
value- Java BigInteger- Returns:
- CVMBigInteger instance or null if not valid
-
create
Create a big integer from a valid blob representation. Blob can be non-canonical.- Parameters:
data- Blob data containing minimal BigInteger twos complement representation- Returns:
- Big Integer value or null if not valid.
-
toLong
-
longValue
public long longValue()- Specified by:
longValuein classAPrimitive- Returns:
- Java long value representing this primitive CVM value
-
big
Description copied from class:AIntegerConverts this integer to a Java BigInteger. WARNING: might be O(n) -
blob
-
buildBlob
-
buildBigInteger
-
toDouble
-
numericType
Description copied from class:ANumericGets the numeric type that should be used as for calculations- Specified by:
numericTypein classANumeric- Returns:
- Double.class or Long.class, or null if not a numeric type
-
signum
-
estimatedEncodingSize
public int estimatedEncodingSize()Description copied from interface:IWriteableEstimate the encoded data size for this Cell. Used for quickly sizing buffers. Implementations should try to return a size that is highly likely to contain the entire object when encoded, including the tag byte. Should not traverse soft Refs, i.e. must be usable on arbitrary partial data structures- Returns:
- The estimated size for the binary representation of this object.
-
doubleValue
public double doubleValue()- Specified by:
doubleValuein classANumeric- Returns:
- Java double value representing this primitive CVM value
-
validateCell
Description copied from class:ACellValidates the local structure and invariants of this cell. Called by validate() super implementation. Should validate directly contained data, but should not validate all other structure of this cell. In particular, should not traverse potentially missing child Refs.- Specified by:
validateCellin classACell- Throws:
InvalidDataException- If the Cell is invalid
-
getTag
public byte getTag()Description copied from class:ACellGets the tag byte for this cell. The tag byte is always equal to the first byte of the Cell's canonical Encoding, and is sufficient to distinguish how to read the rest of the encoding. -
encode
public int encode(byte[] bs, int pos) Description copied from class:ACellWrites this Cell's encoding to a byte array, including a tag byte which will be written first. Cell must be canonical, or else an error may occur.- Specified by:
encodein interfaceIWriteable- Specified by:
encodein classACell- Parameters:
bs- A byte array to which to write the encodingpos- The offset into the byte array- Returns:
- New position after writing
-
encodeRaw
protected int encodeRaw(byte[] bs, int pos) Description copied from class:ACellWrites this Cell's encoding to a byte array, excluding the tag byte. -
print
Description copied from class:AObjectPrints this Object to a readable String Representation. SECURITY: Must halt and return false in O(1) time when limit of printing is exceeded otherwise DoS attacks may be possible. -
byteLength
public long byteLength()Description copied from class:AIntegerNumber of bytes in minimal representation of this Integer. Returns 0 if and only if the integer is zero.- Specified by:
byteLengthin classAInteger- Returns:
- Number of bytes
-
toString
-
equals
Description copied from class:ACellChecks for equality with another Cell. In general, Cells are considered equal if they have the same canonical representation, i.e. an identical encoding with the same hash value. Subclasses SHOULD override this if they have a more efficient equals implementation. MUST NOT require reads from Store. -
equals
-
getBigInteger
Gets the Java BigInteger representing this number- Returns:
- Java BigInteger
-
isCanonical
public boolean isCanonical()Description copied from class:ACellReturns true if this Cell is in a canonical representation. Non-canonical objects may be used on a temporary internal basis, they should be converted to canonical representations for general purpose use.- Specified by:
isCanonicalin classAInteger- Returns:
- true if the object is in canonical format, false otherwise
-
calcMemorySize
protected long calcMemorySize()Description copied from class:ACellCalculates the Memory Size for this Cell. Assumes not already calculated Requires any child Refs to be either direct or of persisted in store at minimum, or you might get a MissingDataException- Overrides:
calcMemorySizein classAPrimitive- Returns:
- Memory Size of this Cell
-
isEmbedded
public boolean isEmbedded()Description copied from class:ACellDetermines if this Cell Represents an embedded object. Embedded objects are encoded directly into the encoding of the containing Cell (avoiding the need for a hashed reference). Subclasses should override this if they have a cheap O(1) way to determine if they are embedded or otherwise.- Overrides:
isEmbeddedin classAPrimitive- Returns:
- true if Cell is embedded, false otherwise
-
getRefCount
public int getRefCount()Description copied from class:ACellGets the number of Refs contained within this Cell. This number is final / immutable for any given instance and is defined by the Cell encoding rules. Contained Refs may be either external or embedded.- Overrides:
getRefCountin classAPrimitive- Returns:
- The number of Refs in this Cell
-
getRef
Description copied from class:ACellGets a numbered child Ref from within this Cell. WARNING: May need to convert to a canonical instance- Overrides:
getRefin classAPrimitive- Type Parameters:
R- Type of referenced Cell- Parameters:
i- Index of ref to get- Returns:
- The Ref at the specified index
-
toCanonical
Description copied from class:ACellConverts this Cell to a canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise. Callers should usually use getCanonical(), which caches canonical instances once created- Specified by:
toCanonicalin classAInteger- Returns:
- Canonical version of Cell
-
read
- Throws:
BadFormatException
-
abs
-
inc
-
dec
-
compareTo
-
add
-
sub
-
negate
-
parse
Parses a string as a CVMBigInteger. Might not be canonical.- Parameters:
s- String containing at least one numeric digit. May have an optional sign.- Returns:
- Integer result
-
ensureLong
Description copied from class:ANumericGets the numeric value as a Long integer, or null if not a valid Long- Specified by:
ensureLongin classANumeric- Returns:
- CVMLong value, or null
-
multiply
-
isZero
-
mod
-
rem
-
div
-
quot
-
toBlob
-
isLong
-