I have problem with listing numbering from [this](https://tex.stackexchange.com/questions/134418/numbering-of-listings) topic. Numbering isn't restarted, it just continues.

```
\AtBeginDocument{%
  \renewcommand{\thelstlisting}{%
    \ifnum\value{subsection}=0
      \thesection.\arabic{lstlisting}%
    \else
      \ifnum\value{subsubsection}=0
        \thesubsection.\arabic{lstlisting}%
      \else
        \thesubsubsection.\arabic{lstlisting}%
      \fi
    \fi
  }
}

```

For subsubsection 4.2.1 it should start from the beginning but it continues so I'm getting 4.2.1.2 (because there is one previous listing).

![enter image description here](https://i.stack.imgur.com/uqT2o.png)
![enter image description here](https://i.stack.imgur.com/HVHFs.png)