Skip to content

Instantly share code, notes, and snippets.

@edefazio
Last active October 16, 2015 23:05
Show Gist options
  • Save edefazio/494b92ceae3dad0e7435 to your computer and use it in GitHub Desktop.
Save edefazio/494b92ceae3dad0e7435 to your computer and use it in GitHub Desktop.
TypeFrame data vectorization using Types, Fields, and Frames
import typeframe.api.Fields;
import typeframe.frame.bitwise.BitFrame;
import typeframe.type.ByteType;
public class Types_Fields_Frames
{
public static ByteType COMPONENT =
ByteType.INSTANCE;
public static final BitFrame RGBColorFrame =
BitFrame.of( "RGB (Red Green Blue) Color Frame",
Fields.of( "Red", COMPONENT ),
Fields.of( "Green", COMPONENT ),
Fields.of( "Blue", COMPONENT )
);
public static void main (String[] args)
{
//this will print out the structure of the Frame
//System.out.println ( RGBColorFrame );
//create a vectorized "compound" of data
long red = RGBColorFrame.pack( 255, 0, 0 );
System.out.println ( RGBColorFrame.describe( red ) );
/*
----------------------------------------111111110000000000000000 [16711680] (24-bits)
11111111 Red[255]->255 (8-bits)
00000000 Green[0]->0 (8-bits)
00000000 Blue[0]->0 (8-bits)
*/
//read a single field within the compound
byte blueComponent = (Byte)RGBColorFrame.read( "Blue", red );
// update a single field within the compound
long redGreen = RGBColorFrame.update( "Green", 255, red );
//unpack the entire compound to an array
Byte[] components = new Byte[3];
RGBColorFrame.getUnpacker().asFormsTo( red, components, 0 );
}
}
@edefazio
Copy link
Author

TypeFrame defines {@code Type}s, {@code Field}s and {@code Frame}s for "vectorizing" data.
Data that is vectorized within a word is called a "compound".

  • each {@code Type} defines the finite domain of states/forms for data
    (i.e. a {@code BoolType} has (2) states {false, true} represented in (1) bit as either a [0] or [1])
  • a {@code Field} is a named variable declaration of a specific {@code Type}
  • a {@code Frame} composes multiple {@code Field}s in a single word.

...To use TypeFrame:

  1. define a {@code Frame} containing {@code Field}s for how the data is structured
  2. "compounds" (of "vectorized" data) can be created by the {@code frame.pack(...)}
  3. you may describe the contents of a "compound" using {@code frame.describe(...)}
  4. you can read an individual field in the compound by {@code frame.read(...)}
  5. you can update a field within a "compound" using {@code frame.update(...)}
  6. you can "unpack" all of the field values from within the compound using {@code frame.unpack(...)}

TypeFrame precisely defines HOW data is assigned to bits within a word, and
saves developers from writing tedious manual bit-shifting and masking operations.
(which can be error-prone and "ugly").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment