@@ -1172,6 +1172,69 @@ parse_provenance_table_dict(
11721172 return ret ;
11731173}
11741174
1175+ static int
1176+ parse_index_dict (tsk_table_collection_t * tables , PyObject * dict )
1177+ {
1178+ int err ;
1179+ int ret = -1 ;
1180+ size_t insertion_length , removal_length ;
1181+ PyObject * insertion_input = NULL ;
1182+ PyArrayObject * insertion_array = NULL ;
1183+ PyObject * removal_input = NULL ;
1184+ PyArrayObject * removal_array = NULL ;
1185+
1186+ /* Get the inputs */
1187+ insertion_input = get_table_dict_value (dict , "edge_insertion_order" , false);
1188+ if (insertion_input == NULL ) {
1189+ goto out ;
1190+ }
1191+ removal_input = get_table_dict_value (dict , "edge_removal_order" , false);
1192+ if (removal_input == NULL ) {
1193+ goto out ;
1194+ }
1195+
1196+ if ((insertion_input == Py_None ) != (removal_input == Py_None )) {
1197+ PyErr_SetString (PyExc_TypeError ,
1198+ "edge_insertion_order and edge_removal_order must be specified together" );
1199+ goto out ;
1200+ }
1201+
1202+ if (insertion_input != Py_None ) {
1203+ insertion_array = table_read_column_array (
1204+ insertion_input , NPY_INT32 , & insertion_length , false);
1205+ if (insertion_array == NULL ) {
1206+ goto out ;
1207+ }
1208+ removal_array
1209+ = table_read_column_array (removal_input , NPY_INT32 , & removal_length , false);
1210+ if (removal_array == NULL ) {
1211+ goto out ;
1212+ }
1213+ if (insertion_length != removal_length ) {
1214+ PyErr_SetString (PyExc_ValueError ,
1215+ "edge_insertion_order and edge_removal_order must be the same length" );
1216+ goto out ;
1217+ }
1218+ if (insertion_length != tables -> edges .num_rows ) {
1219+ PyErr_SetString (PyExc_ValueError ,
1220+ "edge_insertion_order and edge_removal_order must be "
1221+ "the same length as the number of edges" );
1222+ goto out ;
1223+ }
1224+ err = tsk_table_collection_set_index (
1225+ tables , PyArray_DATA (insertion_array ), PyArray_DATA (removal_array ));
1226+ if (err != 0 ) {
1227+ handle_tskit_error (err );
1228+ goto out ;
1229+ }
1230+ }
1231+ ret = 0 ;
1232+ out :
1233+ Py_XDECREF (insertion_array );
1234+ Py_XDECREF (removal_array );
1235+ return ret ;
1236+ }
1237+
11751238static int
11761239parse_table_collection_dict (tsk_table_collection_t * tables , PyObject * tables_dict )
11771240{
@@ -1339,6 +1402,21 @@ parse_table_collection_dict(tsk_table_collection_t *tables, PyObject *tables_dic
13391402 goto out ;
13401403 }
13411404
1405+ /* index */
1406+ value = get_table_dict_value (tables_dict , "index" , false);
1407+ if (value == NULL ) {
1408+ goto out ;
1409+ }
1410+ if (value != Py_None ) {
1411+ if (!PyDict_Check (value )) {
1412+ PyErr_SetString (PyExc_TypeError , "not a dictionary" );
1413+ goto out ;
1414+ }
1415+ if (parse_index_dict (tables , value ) != 0 ) {
1416+ goto out ;
1417+ }
1418+ }
1419+
13421420 ret = 0 ;
13431421out :
13441422 return ret ;
@@ -1480,6 +1558,14 @@ write_table_arrays(tsk_table_collection_t *tables, PyObject *dict)
14801558 { NULL },
14811559 };
14821560
1561+ struct table_col index_cols [] = {
1562+ { "edge_insertion_order" , (void * ) tables -> indexes .edge_insertion_order ,
1563+ tables -> indexes .num_edges , NPY_INT32 },
1564+ { "edge_removal_order" , (void * ) tables -> indexes .edge_removal_order ,
1565+ tables -> indexes .num_edges , NPY_INT32 },
1566+ { NULL },
1567+ };
1568+
14831569 struct table_desc table_descs [] = {
14841570 { "individuals" , individual_cols , tables -> individuals .metadata_schema ,
14851571 tables -> individuals .metadata_schema_length },
@@ -1496,6 +1582,7 @@ write_table_arrays(tsk_table_collection_t *tables, PyObject *dict)
14961582 { "populations" , population_cols , tables -> populations .metadata_schema ,
14971583 tables -> populations .metadata_schema_length },
14981584 { "provenances" , provenance_cols , NULL , 0 },
1585+ { "index" , index_cols , NULL , 0 },
14991586 };
15001587
15011588 for (j = 0 ; j < sizeof (table_descs ) / sizeof (* table_descs ); j ++ ) {
@@ -1557,7 +1644,7 @@ dump_tables_dict(tsk_table_collection_t *tables)
15571644 }
15581645
15591646 /* Dict representation version */
1560- val = Py_BuildValue ("ll" , 1 , 1 );
1647+ val = Py_BuildValue ("ll" , 1 , 2 );
15611648 if (val == NULL ) {
15621649 goto out ;
15631650 }
0 commit comments