stackoverflowerror when processing depth-first-iteration via scala - java

I intend to recursively iterate all grids within a circle zone, the code below will perform depth-first-search. But after 204 stacks, java.lang.StackOverflowError will be thrown.
def geohash_circle_around_point(lat: Double, lon: Double, radius: Double) = {
def expand_neighbors_impl(ghCenter: GeoHash, ghCur: GeoHash, buffer: collection.mutable.Set[GeoHash]): Unit = {
// MARK: DP: check whether it's iterated already or not
if(buffer contains ghCur) {
return
}
buffer += ghCur
for(ghAround <- get4GeoHashAround(ghCur)) {
if(distanceBetweenGeohash(ghCenter, ghAround) <= radius) {
expand_neighbors_impl(ghCenter, ghAround, buffer)
}
}
}
def get4GeoHashAround(gh: GeoHash): Array[GeoHash] = {
Array(gh.getNorthernNeighbour, gh.getSouthernNeighbour, gh.getWesternNeighbour, gh.getEasternNeighbour)
}
def distanceBetweenGeohash(gh1: GeoHash, gh2: GeoHash) = {
haversine(gh1.getBoundingBoxCenterPoint.getLatitude, gh1.getBoundingBoxCenterPoint.getLongitude, gh2.getBoundingBoxCenterPoint.getLatitude, gh2.getBoundingBoxCenterPoint.getLongitude)
}
val ghCenter = GeoHash.withBitPrecision(lat, lon, 40)
val s = collection.mutable.Set[GeoHash]()
expand_neighbors_impl(ghCenter, ghCenter, s)
s.map(_.getBoundingBox)
}
The stacktrace is as follows:
Exception in thread "main" java.lang.StackOverflowError
at scala.collection.mutable.HashSet.index(HashSet.scala:40)
at scala.collection.mutable.FlatHashTable$class.findElemImpl(FlatHashTable.scala:126)
at scala.collection.mutable.FlatHashTable$class.containsElem(FlatHashTable.scala:121)
at scala.collection.mutable.HashSet.containsElem(HashSet.scala:40)
at scala.collection.mutable.HashSet.contains(HashSet.scala:57)
at Test$.Test$$expand_neighbors_impl$1(Test.scala:32)
at Test$$anonfun$Test$$expand_neighbors_impl$1$1.apply(Test.scala:39)
at Test$$anonfun$Test$$expand_neighbors_impl$1$1.apply(Test.scala:37)
at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186)
at Test$.Test$$expand_neighbors_impl$1(Test.scala:37)
at Test$$anonfun$Test$$expand_neighbors_impl$1$1.apply(Test.scala:39)
at Test$$anonfun$Test$$expand_neighbors_impl$1$1.apply(Test.scala:37)
at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33)
at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:186)
at Test$.Test$$expand_neighbors_impl$1(Test.scala:37)
....
Could anyone give some suggestions? Thanks!
P.S.
Implementation for equals and hashCode for GeoHash:
public boolean equals(Object obj) {
if(obj == this) {
return true;
} else {
if(obj instanceof GeoHash) {
GeoHash other = (GeoHash)obj;
if(other.significantBits == this.significantBits && other.bits == this.bits) {
return true;
}
}
return false;
}
}
public int hashCode() {
byte f = 17;
int f1 = 31 * f + (int)(this.bits ^ this.bits >>> 32);
f1 = 31 * f1 + this.significantBits;
return f1;
}

Seems like you really need more than 200 calls at 40 precision...
You might want to consider rewriting your recursion to be tail-recursive, in order to be optimized by the compiler. Here's a way to do this:
#tailrec
def expand_neighbors_impl(ghCenter: GeoHash, toGoThrough: List[GeoHash], buffer: Set[GeoHash] = Set()): Set[GeoHash] = {
toGoThrough.headOption match {
case None => buffer
case Some(ghCur) =>
if (buffer contains ghCur) {
expand_neighbors_impl(ghCenter, toGoThrough.tail, buffer)
}
else {
val neighbors = get4GeoHashAround(ghCur).filter(distanceBetweenGeohash(ghCenter, _) <= radius)
expand_neighbors_impl(ghCenter, neighbors ++: toGoThrough, buffer + ghCur)
}
}
}
def expand_neighbors_impl(ghCenter: GeoHash, ghCur: GeoHash): Set[GeoHash] =
expand_neighbors_impl(ghCenter, List(ghCur))
Besides using tail-recursion, it avoids using a mutable Set, which might give some unexpected complication.

Related

How do I declare the identifier in this smart contract in remix?

Need to identify totalSupply.
If you find any potential errors or something that can cause an issue, I would highly appreciate you for helping me, preventing any errors in the future.
To avoid adding parameters, because I don't know how to put them into the contract when verification happens
How can I add in the name, symbol in the contract I believe those will be strings and the maxNftSupply as 10000 and the saleStart time those would be uints.
If I can have a verbal conversation with someone that can help, I would love that opportunity.
contract TestMB is ERC721, Ownable {
using SafeMath for uint256;
string public TEST_PROVENANCE = "";
uint256 public startingIndexBlock;
uint256 public startingIndex;
uint256 public constant testPrice = 80000000000000000; //0.08 ETH
uint public constant maxTestPurchase = 20;
uint256 public MAX_TEST;
bool public saleIsActive = false;
uint256 public REVEAL_TIMESTAMP;
constructor(string memory name, string memory symbol, uint256 maxNftSupply, uint256 saleStart) ERC721(name, symbol) {
MAX_TEST = maxNftSupply;
REVEAL_TIMESTAMP = saleStart + (86400 * 9);
}
function withdraw() public onlyOwner {
(bool os, ) = payable(owner()).call{value: address(this).balance}('');
require(os);
}
function reserveTest() public onlyOwner {
uint supply = totalSupply();
uint i;
for (i = 0; i < 30; i++) {
_safeMint(msg.sender, supply + i);
}
}
function setRevealTimestamp(uint256 revealTimeStamp) public onlyOwner {
REVEAL_TIMESTAMP = revealTimeStamp;
}
/*
* Set provenance once it's calculated
*/
function setProvenanceHash(string memory provenanceHash) public onlyOwner {
TEST_PROVENANCE = provenanceHash;
}
function setBaseURI(string memory _setBaseURI) public onlyOwner {
setBaseURI = _setBaseURI;
}
function flipSaleState() public onlyOwner {
saleIsActive = !saleIsActive;
}
function mintTest(uint numberOfTokens) public payable {
require(saleIsActive, "Sale must be active to mint Test");
require(numberOfTokens <= maxTestPurchase, "Can only mint 20 tokens at a time");
require(totalSupply().add(numberOfTokens) <= MAX_TEST, "Purchase would exceed max supply of Test");
require(testPrice.mul(numberOfTokens) <= msg.value, "Ether value sent is not correct");
for(uint i = 0; i < numberOfTokens; i++) {
uint mintIndex = totalSupply();
if (totalSupply() < MAX_TEST) {
_safeMint(msg.sender, mintIndex);
}
}
if (startingIndexBlock == 0 && (totalSupply() == MAX_TEST || block.timestamp >= REVEAL_TIMESTAMP)) {
startingIndexBlock = block.number;
}
}
function setStartingIndex() public {
require(startingIndex == 0, "Starting index is already set");
require(startingIndexBlock != 0, "Starting index block must be set");
startingIndex = uint(blockhash(startingIndexBlock)) % MAX_TEST;
if (block.number.sub(startingIndexBlock) > 255) {
startingIndex = uint(blockhash(block.number - 1)) % MAX_TEST;
}
if (startingIndex == 0) {
startingIndex = startingIndex.add(1);
}
}
function emergencySetStartingIndexBlock() public onlyOwner {
require(startingIndex == 0, "Starting index is already set");
startingIndexBlock = block.number;
}
}
Your contract inherits from ERC721 instead of ERC721Enumerable, which contains the totalSupply() function, while ERC721 does not. What you can do is import ERC721Enumerable into the contract and have it inherit like so:
contract TestMB is ERC721, ERC721Enumerable, Ownable {}

assertEquals(tree.contains(obj),hash.contains(obj)) returns false after overriding hashCode()

One of my labs for school has me overriding both the equals and the hashCode methods. I have been able to do the equals method just fine but I'm running into some issues with the hashCode method.
My equals code looks like this, I think I did it correctly but I'm not 100% sure
#Override public boolean equals(Object other){
if(this == other){
return true;
}
if(other == null){
return false;
}
if(!(other instanceof Polynomial)){
return false;
}
Polynomial other2 = (Polynomial) other;
if(this.getDegree() != other2.getDegree()){
return false;
}
for(int i=0;i<this.getDegree();i++){
if(this.getCoefficient(i)!=other2.getCoefficient(i)){
return false;
}
}
return true;
}
The following is my hashCode method, I got to here by using the auto generation that eclipse offers for hashCode
#Override public int hashCode() {
final int p = 31; //prime
int res = 1;
res = p * res + ((coefficients == null) ? 0 : coefficients.hashCode());
res = p * res + this.getDegree();
return res;
}
I think this should be fine however I am running into an issue with the auto tester provided to us to check our labs. The issue arises with the following piece of code. BlueJ flags the assertEquals to be wrong, stating that true is expected but result was false. to be specific it's assertEquals(tree.contains(p1), hash.contains(p1)) that is being caught by BlueJ.
#Test public void massTest() {
Random rng = new Random(SEED);
TreeSet<Polynomial> tree = new TreeSet<Polynomial>();
HashSet<Polynomial> hash = new HashSet<Polynomial>();
CRC32 check = new CRC32();
for(int i = 0; i < TRIALS; i++) {
Polynomial p1 = createRandom(rng.nextInt(10), rng);
Polynomial p2 = createRandom(rng.nextInt(10), rng);
assertEquals(tree.contains(p1), hash.contains(p1));
tree.add(p1);
hash.add(p1);
assertEquals(0, p1.compareTo(p1));
assertEquals(0, p2.compareTo(p2));
assertEquals(p1.compareTo(p2), -p2.compareTo(p1));
check.update(p1.compareTo(p2));
}
assertEquals(tree.size(), hash.size());
for(Polynomial p: tree) {
assertTrue(hash.contains(p));
}
for(Polynomial p: hash) {
assertTrue(tree.contains(p));
}
assertEquals(28339163L, check.getValue());
}
}
My issues right now is that I can't figure out what it is exactly I'm doing wrong and to be honest I'm also not sure what it is that the assertEquals is even checking for. If someone could point me in the right direction I would greatly appreciate it.
The exact error given to me is
java.lang.AssertionError: expected: true but was:false
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
at PolynomialTestThree.massTest(PolynomialTestThree.java:59)

How to parse string into BigDecimal uniformly?

I am gettings strings as the input for a program, but those could be represented in various formats. E.g.:
8900
8.9E+3
89E+2
8900.000
All those numbers are equal mathematically and the following program also tells a match:
public class BigDecimalMain {
public static void main(String... args) {
BigDecimal a = new BigDecimal("8900");
BigDecimal b = new BigDecimal("8.9E+3");
BigDecimal c = new BigDecimal("89E+2");
BigDecimal d = new BigDecimal("8900.000");
System.out.println(a.compareTo(b));
System.out.println(a.compareTo(c));
System.out.println(a.compareTo(d));
}
}
Output:
0
0
0
My program uses .equals to match objects and it fails to give the right answer in the case above.
Question: How to parse string into BigDecimal uniformly? I mean the following:
find a function PARSE, that
for any STR1 && STR2
PARSE(STR1).compareTo(PARSE(STR2)) == 0 <=> PARSE(STR1).equals(PARSE(STR2))
With a common scale you could use setScale(xxx):
{
BigDecimal a = new BigDecimal("8900");
BigDecimal b = new BigDecimal("8.9E+3");
BigDecimal c = new BigDecimal("89E+2");
BigDecimal d = new BigDecimal("8900.000");
System.out.println(a.compareTo(b));
System.out.println(a.compareTo(c));
System.out.println(a.compareTo(d));
System.out.println(a.equals(b));
System.out.println(a.equals(c));
System.out.println(a.equals(d));
}
{
BigDecimal a = new BigDecimal("8900").setScale(5);
BigDecimal b = new BigDecimal("8.9E+3").setScale(5);
BigDecimal c = new BigDecimal("89E+2").setScale(5);
BigDecimal d = new BigDecimal("8900.000").setScale(5);
System.out.println(a.compareTo(b));
System.out.println(a.compareTo(c));
System.out.println(a.compareTo(d));
System.out.println(a.equals(b));
System.out.println(a.equals(c));
System.out.println(a.equals(d));
}
shows
0
0
0
false
false
false
0
0
0
true
true
true
Consider the following approach:
class UniformDecimal {
private final BigDecimal decimal;
public UniformDecimal(BigDecimal decimal) {
this.decimal = decimal;
}
#Override
public int hashCode() {
return decimal.toBigInteger().mod(BigInteger.valueOf((1<<31)-1)).intValue();
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UniformDecimal that = (UniformDecimal) o;
return that.decimal.compareTo(this.decimal)==0;
}
}
Though, I'm not sure about hashCode implementation
UPDATE. It seems that stripTrailingZeros() does the thing!
Found something useful at here: HiveDecimal
private static BigDecimal normalize(BigDecimal bd, boolean allowRounding) {
if (bd == null) {
return null;
}
bd = trim(bd);
int intDigits = bd.precision() - bd.scale();
if (intDigits > MAX_PRECISION) {
return null;
}
int maxScale = Math.min(MAX_SCALE, Math.min(MAX_PRECISION - intDigits, bd.scale()));
if (bd.scale() > maxScale) {
if (allowRounding) {
bd = bd.setScale(maxScale, RoundingMode.HALF_UP);
// Trimming is again necessary, because rounding may introduce new trailing 0's.
bd = trim(bd);
} else {
bd = null;
}
}
return bd;
}
private static BigDecimal trim(BigDecimal d) {
if (d.compareTo(BigDecimal.ZERO) == 0) {
// Special case for 0, because java doesn't strip zeros correctly on that number.
d = BigDecimal.ZERO;
} else {
d = d.stripTrailingZeros();
if (d.scale() < 0) {
// no negative scale decimals
d = d.setScale(0);
}
}
return d;
}

Copy constructor help, trying to copy a boolean array. Java

I have looked through as many previous questions as possible but never saw a question that had a boolean array as a variable.
Here is my class:
public class Register {
private boolean[] register;
private int length;
//Normal constructor
public Register(int n) {
if (n == 8 || n == 16 || n == 32 || n == 64) {
length = n;
register = new boolean[length];
for (int i = 0; i < length; i++) {
register[i] = false;
}
} else {
throw new RegisterException(
"A register can only contain 8, 16, 32, or 64 bits");
}
}
// Creates a copy of reg (an existing Register)
public Register(Register reg) {
length = reg.length;
register = new boolean[reg.register.length];
System.arraycopy(reg.register, 0, this.register, 0, reg.register.length);
}
In my driver program i am loading "1101101" into register1, but when i do:
Register register2 = new Register(register1);
and print out both results i get:
0000000001101101
0000000000010110
Not really sure what is going on O.o any help would be appreciated, thanks!
This is my load method. i held off on putting it in here because it might be hard to read:
public void load(String binaryRep) {
String allTheBits = binaryRep;
int charPosition = 0;
int loadLength;
int binaryNum = 0;
String index = "";
String trimmedIndex = "";
if (allTheBits.length() > 0 && allTheBits.length() <= length) {
loadLength = allTheBits.length();
for (int i = length - (loadLength); i < length; i++) {
index = allTheBits.charAt(charPosition) + "";
trimmedIndex = index.trim();
binaryNum = Integer.parseInt(trimmedIndex);
if (binaryNum == 1) {
register[i] = true;
} else if (binaryNum == 0) {
register[i] = false;
}
charPosition++;
}
} else {
throw new RegisterException("You can only load 0 - " + length
+ "bits.");
}
}
Here's a more idiomatic way of doing it (using the Cloneable interface):
public class Register implements Cloneable {
private boolean[] register;
public Register(boolean[] register) {
int n = register.length;
if (n == 8 || n == 16 || n == 32 || n == 64) {
this.register = register;
} else {
throw new IllegalArgumentException(
"A register can only contain 8, 16, 32, or 64 bits");
}
}
#Override
public String toString() {
StringBuilder builder = new StringBuilder();
for ( boolean b : this.register ) {
builder.append( b ? "1" : "0" );
}
return builder.toString();
}
public Register( int n ) {
this( new boolean[n] );
}
public int getLength() {
return this.register.length;
}
#Override
public Register clone() {
boolean[] clonedRegister = new boolean[this.register.length];
System.arraycopy(this.register, 0, clonedRegister,0, this.register.length);
return new Register( clonedRegister );
}
}
And a JUnit test showing it in action:
import org.junit.Assert;
import org.junit.Test;
public class RegisterTest {
#Test
public void testRegisterToString() {
Register source = new Register( new boolean[] {true, true, false, false, true, false, true, false } );
String result = "11001010";
Assert.assertEquals( result, source.toString() );
}
#Test
public void testRegisterCloning() {
Register source = new Register( new boolean[] {true, true, false, false, true, false, false, false } );
Register clone = source.clone();
Assert.assertEquals( source.toString(), clone.toString() );
}
}
A couple of remarks so that you learn some basic things.
As #Ted said, no need to keep the length field as register. length will give you the same
Local variables are not initialized with default values, but arrays are, as they are stored in the heap. So there's no need to iterate over the "register" array to set all of its positions to false
Using an array of booleans to do this may have felt easy but its extremely inefficient memory wise, as each boolean takes at least 32 bits in the heap. Therefore, to represent a 64 bit register you are using at least 32*64+32=2080 bits... using a byte array and bitwise logic will be a bit harder but hey, it's a small challenge :)
Anyway, your code looks fine (BTW, use Arrays.copyOf method as it's more readable), so the error should be coming from another side.
I just verified your load method with the following:
public static void main(String [] args)
{
Register r1 = new Register(8);
r1.load("1101101");
Register r2 = new Register(r1);
for (int i=0; i<8; i++) System.out.println(r2.register[i]);
}
Output:
> run Register
false
true
true
false
true
true
false
true
>
It looks right to me as far as the contents of the Register objects are concerned, so the problem probably is with the access.

Wrapping BSD select() with JNA

I need to wrap a BSD-like C socket API to Java with JNA. It has basically the same functions as standard BSD socket API.
Wrapping select() is problematic because of the fd_set-structure required in its arguments and the FD_* masking functions (macros) that are needed to handle fd_sets. I tried to crawl through the header files (e.g. sys/select.h in Ubuntu 8.04) but the definitions are not so straightforward. Especially I found it difficult to find the implementation of FD_*-macros, which is needed when wrapping them with JNA's InvocationMapper.
Note: I'm not trying to wrap the standard TCP or unix-socket API, but a custom one. Thus built-in sockets in Java do not fit the bill.
Especially I found it difficult to find the implementation of FD_*-macros, which is needed when wrapping them with JNA's InvocationMapper.
The C pre-processor cpp is useful to find out how macros are expanded. Write a dummy program that uses the relevant macros (it should be lexically correct, but needn't compile), run it through cpp and watch what happens.
I use a byte array for the fd_set structure and some arithmetic to find the right byte position within the array:
private static final int FD_SETSIZE = 1024;
private static final boolean isBigEndian = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
private static interface Libc extends Library {
int select (int nfds, byte[] readfds, byte[] writefds, byte[] errfds, TimeVal timeout);
//...
}
private static class FdSet {
byte[] a;
FdSet() {
a = new byte[FD_SETSIZE / 8]; }
void set (int fd) {
a[getBytePos(fd)] |= getBitMask(fd); }
boolean isSet (int fd) {
return (a[getBytePos(fd)] & getBitMask(fd)) != 0; }
private static int getBytePos (int fd) {
if (fd < 0 || fd >= LibcDefs.FD_SETSIZE) {
throw new RuntimeException("File handle out of range for fd_set."); }
if (isBigEndian) {
return (fd / 8 / Native.LONG_SIZE + 1) * Native.LONG_SIZE - 1 -
fd / 8 % Native.LONG_SIZE; }
else {
return fd / 8; }}
private static int getBitMask (int fd) {
return 1 << (fd % 8); }}
private static class TimeVal extends Structure {
public NativeLong tv_sec;
public NativeLong tv_usec;
TimeVal (int ms) {
set(ms); }
void set (int ms) {
tv_sec.setValue(ms / 1000);
tv_usec.setValue(ms % 1000 * 1000); }
#Override protected List<?> getFieldOrder() {
return Arrays.asList("tv_sec", "tv_usec"); }}
public boolean waitInputReady (int timeoutMs) throws IOException {
TimeVal timeVal = (timeoutMs < 0) ? null : new TimeVal(timeoutMs);
FdSet rxSet = new FdSet();
FdSet errorSet = new FdSet();
rxSet.set(fileHandle);
errorSet.set(fileHandle);
int rc = libc.select(fileHandle + 1, rxSet.a, null, errorSet.a, timeVal);
checkSelectErrors(rc, errorSet);
if (rc == 0) {
return false; }
if (!rxSet.isSet(fileHandle)) {
throw new RuntimeException("rxSet bit is not set after select()."); }
return true; }
public boolean waitOutputReady (int timeoutMs) throws IOException {
TimeVal timeVal = (timeoutMs < 0) ? null : new TimeVal(timeoutMs);
FdSet txSet = new FdSet();
FdSet errorSet = new FdSet();
txSet.set(fileHandle);
errorSet.set(fileHandle);
int rc = libc.select(fileHandle + 1, null, txSet.a, errorSet.a, timeVal);
checkSelectErrors(rc, errorSet);
if (rc == 0) {
return false; }
if (!txSet.isSet(fileHandle)) {
throw new RuntimeException("txSet bit is not set after select()."); }
return true; }
private void checkSelectErrors (int rc, FdSet errorSet) throws IOException {
if (rc == -1) {
throw new IOException("Error in select(), errno=" + Native.getLastError() + "."); }
boolean error = errorSet.isSet(fileHandle);
if (!(rc == 0 && !error || rc == 1 || rc == 2 && error)) {
throw new RuntimeException("Invalid return code received from select(), rc=" + rc + ", error=" + error + "."); }
if (error) {
throw new IOException("Channel error state detected"); }}

Categories