# Path: sdopt/util/file_reader.py
# @contextmanager
# def lines_of(filename):
#     print('Trying to read file \"%s\"' % filename)
#     try:
#         f = fileinput.input(filename, mode='r')
#         yield _rstripped_lines(f)
#     finally:
#         print('Read ',f.lineno(),' lines from file \"',filename,'\"', sep='')
#         f.close()
# 
# Path: sdopt/util/misc.py
# def advance(itr, n):
#     return islice(itr, n, None)
# 
# def nth(iterable, n, default=None):
#     'Returns the nth item or a default value.'
#     return next(islice(iterable, n, None), default)
# 
# def skip_until(pred, itr):
#     return dropwhile(lambda s: not pred(s), itr)


from os.path import dirname, join
from ..util.file_reader import lines_of
from ..util.misc import advance, nth, skip_until
import numpy as np
import scipy.sparse as sp
    return s[beg:end]

def read_gjh(filename, nonzeros):
    with lines_of(filename) as lines:
        return parse(lines, nonzeros)

def parse(lines, nonzeros):
    # Construct the Jacobian in coordinate format
    ai, aj = np.zeros(nonzeros,np.int32), np.zeros(nonzeros,np.int32)
    ra, k  = np.zeros(nonzeros, np.float64), 0
    nrows, ncols = get_J_shape(lines)
    lines_from_first_row = skip_until(lambda s: s.startswith('['), lines) 
    for line in lines_from_first_row:
        if line[0]=='[':
            # has hit a new row: [12,*]
            comma = line.find(',')
            row = int(line[1:comma])-1
        elif line[0] == '\t':
            # data in row: <tab> col index <tab> entry
            col_idx, data = line.split()
            col, data = int(col_idx)-1, float(data)
            ai[k], aj[k], ra[k] = row, col, data
            k += 1
        else:
            break
    return sp.coo_matrix((ra, (ai,aj)), shape=(nrows,ncols))

def get_J_shape(lines):
    # It's on the 3rd line, something like: 
    # param J{1..16, 1..16} default 0;

# Path: sdopt/util/file_reader.py
# @contextmanager
# def lines_of(filename):
#     print('Trying to read file \"%s\"' % filename)
#     try:
#         f = fileinput.input(filename, mode='r')
#         yield _rstripped_lines(f)
#     finally:
#         print('Read ',f.lineno(),' lines from file \"',filename,'\"', sep='')
#         f.close()
# 
# Path: sdopt/util/misc.py
# def advance(itr, n):
#     return islice(itr, n, None)
# 
# def nth(iterable, n, default=None):
#     'Returns the nth item or a default value.'
#     return next(islice(iterable, n, None), default)
# 
# def skip_until(pred, itr):
#     return dropwhile(lambda s: not pred(s), itr)


# Path: sdopt/parsers/gjh_parser.py
from os.path import dirname, join
from ..util.file_reader import lines_of
from ..util.misc import advance, nth, skip_until
import numpy as np
import scipy.sparse as sp

    return s[beg:end]

def read_gjh(filename, nonzeros):
    with lines_of(filename) as lines:
        return parse(lines, nonzeros)

def parse(lines, nonzeros):
    # Construct the Jacobian in coordinate format
    ai, aj = np.zeros(nonzeros,np.int32), np.zeros(nonzeros,np.int32)
    ra, k  = np.zeros(nonzeros, np.float64), 0
    nrows, ncols = get_J_shape(lines)
    lines_from_first_row = skip_until(lambda s: s.startswith('['), lines) 
    for line in lines_from_first_row:
        if line[0]=='[':
            # has hit a new row: [12,*]
            comma = line.find(',')
            row = int(line[1:comma])-1
        elif line[0] == '\t':
            # data in row: <tab> col index <tab> entry
            col_idx, data = line.split()
            col, data = int(col_idx)-1, float(data)
            ai[k], aj[k], ra[k] = row, col, data
            k += 1
        else:
            break
    return sp.coo_matrix((ra, (ai,aj)), shape=(nrows,ncols))

def get_J_shape(lines):
    # It's on the 3rd line, something like: 
    # param J{1..16, 1..16} default 0;

    line = nth(lines, 3)