Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Booleans #8

Merged
merged 15 commits into from
Feb 2, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*-
* #%L
* ImgLib2 data structures using Unsafe.
* %%
* Copyright (C) 2017 - 2021 Howard Hughes Medical Institute.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package net.imglib2.img.basictypelongaccess.unsafe;

import net.imglib2.img.basictypeaccess.volatiles.VolatileBooleanAccess;
import net.imglib2.img.basictypelongaccess.BooleanLongAccess;

public class NativeBooleanUnsafe
gselzer marked this conversation as resolved.
Show resolved Hide resolved
implements BooleanLongAccess, VolatileBooleanAccess
{
private static final boolean DEFAULT_IS_VALID = true;
private static final byte ZERO = (byte) 0;
private static final byte ONE = (byte) 1;

private final ByteUnsafe wrapped;

public NativeBooleanUnsafe( final long address )
{
this( address, null );
}

public NativeBooleanUnsafe( final long address, final boolean isValid )
{
this( address, null, isValid );
}

public NativeBooleanUnsafe( final long address, final Object ownerReference )
{
this( address, ownerReference, DEFAULT_IS_VALID );
}

public NativeBooleanUnsafe( final long address, final Object ownerReference, final boolean isValid )
{
super();
this.wrapped = new ByteUnsafe(address, ownerReference, isValid);
}

@Override
public boolean getValue( final int index )
{
return getValue( ( long ) index );
}

@Override
public void setValue( final int index, final boolean value )
{
setValue( ( long ) index, value );
}

@Override
public boolean getValue( final long index )
{
return wrapped.getValue(index) > 0;
}

@Override
public void setValue( final long index, final boolean value )
{
wrapped.setValue(index, value ? ONE : ZERO);
}

public long getAddres()
{
return wrapped.getAddres();
}
Comment on lines +122 to +125
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo, might need to be fixed in wrapped as well. Is this a typo that already exists throughout imglib2-unsafe?

Suggested change
public long getAddres()
{
return wrapped.getAddres();
}
public long getAddress()
{
return wrapped.getAddress();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I saw that too @hanslovsky, but it's all over the place. Here's another example.

I figured this would be a breaking change to address, so I didn't fix it.

I suppose we could write getAddress and deprecate getAddres 😅


@Override
public boolean isValid()
{
return wrapped.isValid();
}

@Override
public void finalize() throws Throwable
{
wrapped.finalize();
}

}