Created
January 2, 2014 20:19
-
-
Save Varriount/8226008 to your computer and use it in GitHub Desktop.
RemoveDir Implementations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Original Implementation | |
proc removeDir*(dir: string) = | |
for kind, path in walkDir(dir): | |
case kind | |
of pcFile, pcLinkToFile, pcLinkToDir: removeFile(path) | |
of pcDir: removeDir(path) | |
rawRemoveDir(dir) | |
## New Implementation | |
proc removeDirTwo*(dir: string) = | |
## Method Two | |
var stack = @[(pcDir, dir)] | |
while stack.len > 0: | |
var empty = true | |
var (currentKind, currentDir) = stack[stack.len-1] | |
for childKind, childPath in walkDir(currentDir): | |
if childKind in {pcFile, pcLinkToFile}: | |
removeFile(childPath) | |
if childKind in {pcDir, pcLinkToDir}: | |
stack.add((childKind, childPath)) | |
empty = false | |
if empty: | |
rawRemoveDir(stack.pop()[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment