Electronic and structural properties of superconducting MgB$_ 2$, CaSi$_ 2$, and related compounds