  Constrained codes are used to prevent errors from occurring in various data
storage and data transmission systems. They can help in increasing the storage
density of magnetic storage devices, in managing the lifetime of electronic
storage devices, and in increasing the reliability of data transmission over
wires. We recently introduced families of lexicographically-ordered constrained
(LOCO) codes. These codes achieve capacity with simple encoding and decoding,
and they are easy to reconfigure. In this paper, we generalize our work on LOCO
codes by presenting a systematic method that guides the code designer to build
any constrained code based on lexicographic indexing once the finite set of
data patterns to forbid is known. In particular, we connect the set of
forbidden patterns directly to the cardinality of the code and to the rule that
uncovers the index associated with a codeword. By doing that, we reveal the
secret arithmetic of patterns, and make the code design significantly easier.
We design optimal (rate-wise) constrained codes for the new two-dimensional
magnetic recording (TDMR) technology. We show notable performance gains as a
result of solely applying the new codes. Moreover, we show how near-optimal
constrained codes be designed and used to further reduce complexity.
