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 long
static final CVMBigInteger
static final BigInteger
static final CVMBigInteger
static final BigInteger
Fields inherited from class convex.core.data.ACell
cachedRef, EMPTY_ARRAY
-
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 ABlob
blob()
protected BigInteger
protected ABlob
long
Number of bytes in minimal representation of this Integer.int
static CVMBigInteger
Create a big integer from a valid blob representation.dec()
Decrements this IntegerDivides this integer with a given denominator.double
int
encode
(byte[] bs, int pos) Writes this Cell's encoding to a byte array, including a tag byte which will be written first.protected int
encodeRaw
(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 Longint
Estimate the encoded data size for this Cell.Gets the Java BigInteger representing this numberbyte
getTag()
Gets the tag byte for this cell.inc()
Increments this Integerboolean
Returns true if this Cell is in a canonical representation for message writing.boolean
isZero()
Check if this numeric value is equal to zerolong
Returns 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 CVMBigInteger
Parses a string as a CVMBigInteger.boolean
print
(BlobBuilder sb, long limit) Prints this Object to a readable String Representation.Divides this integer with a given denominator.static CVMBigInteger
static CVMBigInteger
read
(ByteBuffer bb) Returns the remainder of dividing this integer with a given divisorsignum()
Gets the signum of this numerical value.Subtracts another integer from this integerConverts this Cell to its 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.void
Validates the local structure and invariants of this cell.static CVMBigInteger
wrap
(byte[] bs) Creates a CVMBigInteger WARNING: might not be canonicalstatic CVMBigInteger
wrap
(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
Methods inherited from class convex.core.data.prim.APrimitive
calcMemorySize, getRefCount, isCVMValue, isEmbedded
Methods inherited from class convex.core.data.ACell
announce, announce, attachMemorySize, attachRef, cachedEncoding, cachedHash, createAnnounced, createEncoding, createPersisted, createPersisted, createRef, equals, equals, getCanonical, getChildRefs, getEncoding, getEncodingLength, getHash, getMemorySize, getRef, getRef, hashCode, isCompletelyEncoded, mark, mark, toCVMString, updateRefs, validate, write
Methods 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:
-
-
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
-
wrap
Creates a CVMBigInteger WARNING: might not be canonical- Parameters:
value
- Java BigInteger- Returns:
- CVMBigInteger instance
-
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
Description copied from class:ANumeric
Casts to a CVM Long value. Takes last 64 bits. -
longValue
public long longValue()- Specified by:
longValue
in classAPrimitive
- Returns:
- Java long value representing this primitive CVM value
-
big
Description copied from class:AInteger
Converts this integer to a Java BigInteger. WARNING: might be O(n) -
blob
-
buildBlob
-
buildBigInteger
-
toDouble
Description copied from class:ANumeric
Casts to a CVM Double value. -
numericType
Description copied from class:ANumeric
Gets the numeric type that should be used as for calculations- Specified by:
numericType
in classANumeric
- Returns:
- Double.class or Long.class, or null if not a numeric type
-
signum
Description copied from class:ANumeric
Gets the signum of this numerical value. Will be -1, 0 or 1 for Longs, -1.0, 0.0 , 1.0 or ##NaN for Doubles. -
estimatedEncodingSize
public int estimatedEncodingSize()Description copied from interface:IWriteable
Estimate 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:
doubleValue
in classANumeric
- Returns:
- Java double value representing this primitive CVM value
-
validateCell
Description copied from class:ACell
Validates 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:
validateCell
in classACell
- Throws:
InvalidDataException
- If the Cell is invalid
-
getTag
public byte getTag()Description copied from class:ACell
Gets 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:ACell
Writes 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:
encode
in interfaceIWriteable
- Specified by:
encode
in 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:ACell
Writes this Cell's encoding to a byte array, excluding the tag byte. -
print
Description copied from class:AObject
Prints 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:AInteger
Number of bytes in minimal representation of this Integer. Returns 0 if and only if the integer is zero.- Specified by:
byteLength
in classAInteger
- Returns:
- Number of bytes
-
toString
Description copied from class:ACell
Returns the Java String representation of this Cell. The String representation is intended to be a easy-to-read textual representation of the Cell's data content. -
getBigInteger
Gets the Java BigInteger representing this number- Returns:
- Java BigInteger
-
isCanonical
public boolean isCanonical()Description copied from class:ACell
Returns true if this Cell is in a canonical representation for message writing. Non-canonical objects may be used on a temporary internal basis, they must always be converted to canonical representations for external use (e.g. Encoding).- Specified by:
isCanonical
in classAInteger
- Returns:
- true if the object is in canonical format, false otherwise
-
toCanonical
Description copied from class:ACell
Converts this Cell to its canonical version. Must return this Cell if already canonical, may be O(n) in size of value otherwise.- Specified by:
toCanonical
in classAInteger
- Returns:
- Canonical version of Cell
-
read
- Throws:
BadFormatException
-
read
- Throws:
BadFormatException
-
abs
Description copied from class:ANumeric
Gets the absolute value of this number. May be ##NaN for Doubles. -
inc
Description copied from class:AInteger
Increments this Integer -
dec
Description copied from class:AInteger
Decrements this Integer -
compareTo
-
add
Description copied from class:AInteger
Adds another integer to this integer -
sub
Description copied from class:AInteger
Subtracts another integer from this integer -
negate
Description copied from class:ANumeric
Negates this numeric value -
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:ANumeric
Gets the numeric value as a Long integer, or null if not a valid Long- Specified by:
ensureLong
in classANumeric
- Returns:
- CVMLong value, or null
-
multiply
Description copied from class:ANumeric
Multiplies a second numeric value with this value -
isZero
public boolean isZero()Description copied from class:ANumeric
Check if this numeric value is equal to zero -
mod
Description copied from class:AInteger
Returns the modulus of this integer with a given integer base -
rem
Description copied from class:AInteger
Returns the remainder of dividing this integer with a given divisor -
div
Description copied from class:AInteger
Divides this integer with a given denominator. Performs Euclidian division consistent with mod -
quot
Description copied from class:AInteger
Divides this integer with a given denominator. Performs division consistent with rem
-