Skip to content

Instantly share code, notes, and snippets.

@fredludlow
Created August 2, 2016 15:12
Show Gist options
  • Save fredludlow/eae62f1bc9a66e35e6b158010db26d7b to your computer and use it in GitHub Desktop.
Save fredludlow/eae62f1bc9a66e35e6b158010db26d7b to your computer and use it in GitHub Desktop.
Sketch of atomIndex in bond-store
/**
* @file Bond Store
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @private
*/
import Store from "./store.js";
import BondProxy from "../proxy/bond-proxy.js";
function BondStore( sizeOrObject ){
Store.call( this, sizeOrObject );
this.atomAtomIndex = {};
}
BondStore.prototype = Object.assign( Object.create(
Store.prototype ), {
constructor: BondStore,
type: "BondStore",
__fields: [
[ "atomIndex1", 1, "int32" ],
[ "atomIndex2", 1, "int32" ],
[ "bondOrder", 1, "int8" ],
],
addBond: function( atom1, atom2, bondOrder ){
this.growIfFull();
var i = this.count;
var aai = this.atomAtomIndex;
var ai1 = atom1.index, ai2 = atom2.index;
if( atom1.index < atom2.index ){
this.atomIndex1[ i ] = atom1.index;
this.atomIndex2[ i ] = atom2.index;
}else{
this.atomIndex2[ i ] = atom1.index;
this.atomIndex1[ i ] = atom2.index;
}
if( bondOrder ) this.bondOrder[ i ] = bondOrder;
aai[ ai1 ] = aai[ ai1 ] || {};
aai[ ai1 ][ ai2 ] = i;
aai[ ai2 ] = aai[ ai2 ] || {};
aai[ ai2 ][ ai1 ] = i;
this.count += 1;
},
addBondIfConnected: function( atom1, atom2, bondOrder ){
if( atom1.connectedTo( atom2 ) ){
this.addBond( atom1, atom2, bondOrder );
return true;
}
return false;
},
getBondIndicesForAtom: function( atom1 ) {
var bonds = [];
var o = this.atomAtomIndex[atom1.index];
if( o !== undefined ){
for( var ai2 in o ){
if( o.hasOwnProperty( ai2 ) ) {
bonds.push( o[ ai2 ] );
}
}
}
return bonds;
},
/** Invoke callback( bp ) for each bond for this atom */
eachBondForAtom: function( atom1, callback ) {
var bonds = this.getBondIndicesForAtom( atom1 );
var bp = new BondProxy( atom1.structure, 0 );
for( var i = 0; i < bonds.length; ++i ){
bp.index = bonds[i];
callback( bp );
}
},
deduplicate: function() {
// New arrays, or in place?
}
} );
export default BondStore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment