home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / A_fast_met2161158282009.psc / RemDupes / frmMain.frm next >
Text File  |  2009-08-28  |  12KB  |  393 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmMain 
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "RemDupes"
  6.    ClientHeight    =   2670
  7.    ClientLeft      =   105
  8.    ClientTop       =   390
  9.    ClientWidth     =   3870
  10.    BeginProperty Font 
  11.       Name            =   "Tahoma"
  12.       Size            =   8.25
  13.       Charset         =   0
  14.       Weight          =   400
  15.       Underline       =   0   'False
  16.       Italic          =   0   'False
  17.       Strikethrough   =   0   'False
  18.    EndProperty
  19.    LinkTopic       =   "Form1"
  20.    ScaleHeight     =   2670
  21.    ScaleWidth      =   3870
  22.    StartUpPosition =   2  'CenterScreen
  23.    Begin MSComDlg.CommonDialog cDialog 
  24.       Left            =   3240
  25.       Top             =   1200
  26.       _ExtentX        =   847
  27.       _ExtentY        =   847
  28.       _Version        =   393216
  29.    End
  30.    Begin VB.TextBox txtOutput 
  31.       Appearance      =   0  'Flat
  32.       BackColor       =   &H00E0E0E0&
  33.       BeginProperty Font 
  34.          Name            =   "Tahoma"
  35.          Size            =   6.75
  36.          Charset         =   0
  37.          Weight          =   400
  38.          Underline       =   0   'False
  39.          Italic          =   0   'False
  40.          Strikethrough   =   0   'False
  41.       EndProperty
  42.       Height          =   260
  43.       Left            =   117
  44.       Locked          =   -1  'True
  45.       TabIndex        =   9
  46.       Text            =   " Output File"
  47.       ToolTipText     =   "Output Location"
  48.       Top             =   465
  49.       Width           =   2938
  50.    End
  51.    Begin VB.CommandButton cmdRemDupes 
  52.       Caption         =   "Remove Duplicates!"
  53.       Height          =   364
  54.       Left            =   117
  55.       TabIndex        =   4
  56.       ToolTipText     =   "Remove Duplicates"
  57.       Top             =   2220
  58.       Width           =   3640
  59.    End
  60.    Begin VB.CommandButton cmdSave 
  61.       Caption         =   "Save"
  62.       Height          =   285
  63.       Left            =   3159
  64.       TabIndex        =   3
  65.       ToolTipText     =   "Select Output File"
  66.       Top             =   435
  67.       Width           =   598
  68.    End
  69.    Begin VB.CheckBox chkAlpha 
  70.       Appearance      =   0  'Flat
  71.       BackColor       =   &H00E0E0E0&
  72.       Caption         =   "Alphabetize"
  73.       ForeColor       =   &H80000008&
  74.       Height          =   247
  75.       Left            =   2457
  76.       TabIndex        =   2
  77.       ToolTipText     =   "Alphabetize Word List"
  78.       Top             =   930
  79.       Value           =   1  'Checked
  80.       Width           =   1183
  81.    End
  82.    Begin VB.CommandButton cmdOpen 
  83.       Caption         =   "Open"
  84.       Height          =   285
  85.       Left            =   3159
  86.       TabIndex        =   1
  87.       ToolTipText     =   "Select Input File"
  88.       Top             =   75
  89.       Width           =   598
  90.    End
  91.    Begin VB.TextBox txtInput 
  92.       Appearance      =   0  'Flat
  93.       BackColor       =   &H00E0E0E0&
  94.       BeginProperty Font 
  95.          Name            =   "Tahoma"
  96.          Size            =   6.75
  97.          Charset         =   0
  98.          Weight          =   400
  99.          Underline       =   0   'False
  100.          Italic          =   0   'False
  101.          Strikethrough   =   0   'False
  102.       EndProperty
  103.       Height          =   260
  104.       Left            =   117
  105.       Locked          =   -1  'True
  106.       TabIndex        =   0
  107.       Text            =   " Input File"
  108.       ToolTipText     =   "Input Location"
  109.       Top             =   105
  110.       Width           =   2938
  111.    End
  112.    Begin VB.Label lblStatus 
  113.       Alignment       =   2  'Center
  114.       Appearance      =   0  'Flat
  115.       BackColor       =   &H00E0E0E0&
  116.       BorderStyle     =   1  'Fixed Single
  117.       Caption         =   " Status"
  118.       ForeColor       =   &H80000008&
  119.       Height          =   240
  120.       Left            =   120
  121.       TabIndex        =   8
  122.       ToolTipText     =   "Status"
  123.       Top             =   1860
  124.       Width           =   3645
  125.    End
  126.    Begin VB.Label lblUnique 
  127.       BackStyle       =   0  'Transparent
  128.       Caption         =   "Unique Items:"
  129.       Height          =   240
  130.       Left            =   240
  131.       TabIndex        =   7
  132.       ToolTipText     =   "Unique"
  133.       Top             =   1395
  134.       Width           =   2115
  135.    End
  136.    Begin VB.Label lblDuplicates 
  137.       BackStyle       =   0  'Transparent
  138.       Caption         =   "Duplicates:"
  139.       Height          =   240
  140.       Left            =   240
  141.       TabIndex        =   6
  142.       ToolTipText     =   "Number of Duplicates"
  143.       Top             =   1170
  144.       Width           =   2115
  145.    End
  146.    Begin VB.Label lblLines 
  147.       BackStyle       =   0  'Transparent
  148.       Caption         =   "Lines:"
  149.       Height          =   240
  150.       Left            =   240
  151.       TabIndex        =   5
  152.       ToolTipText     =   "Line Count"
  153.       Top             =   930
  154.       Width           =   2115
  155.    End
  156.    Begin VB.Shape Shape1 
  157.       BackColor       =   &H00E0E0E0&
  158.       BackStyle       =   1  'Opaque
  159.       Height          =   945
  160.       Left            =   120
  161.       Top             =   810
  162.       Width           =   3645
  163.    End
  164. End
  165. Attribute VB_Name = "frmMain"
  166. Attribute VB_GlobalNameSpace = False
  167. Attribute VB_Creatable = False
  168. Attribute VB_PredeclaredId = True
  169. Attribute VB_Exposed = False
  170. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  171. 'Removes duplicates from large (milllions) wordlists quickly
  172. '
  173. 'Sorts wordlists using the quicksort algorithm, then
  174. 'removes duplicates in a single pass, O(n), by comparing
  175. 'each item against the preceding item.
  176. '
  177. 'Items can be "unsorted" back to the order of the original
  178. 'wordlist with the use of an array of indexes, a new
  179. 'array containing the original position of each item.
  180. '
  181. 'Stable Quicksort algorithms by Rde.
  182. 'http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=63941&lngWId=1
  183. '
  184. 'Much(!) faster Split replacement, "Quick Split," by Merri @ VBForums.com.
  185. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  186. 'Note: memory usage issues
  187.  
  188. Option Explicit
  189.            
  190. Dim InputOK As Boolean, OutputOK As Boolean
  191.  
  192. Dim ArrBuffer() As String, ArrIndexes() As Long
  193.  
  194. Dim LineCount As Long, Unique As Long
  195. Dim Min As Long, Max As Long
  196.     
  197. Private Sub cmdOpen_Click()
  198. On Error GoTo Cancel
  199.     With cDialog
  200.         .CancelError = True
  201.         .FileName = vbNullString
  202.         .Filter = "Text Document (*.txt)|*.txt"
  203.         .ShowOpen
  204.         
  205.         If FileExists(.FileName) Then
  206.             txtInput = .FileName
  207.             txtInput.Tag = .FileTitle
  208.             Call LoadWordList
  209.         Else
  210.             InputOK = False
  211.             lblStatus = "Invalid input file"
  212.             MsgBox "Input file does not exist.", vbInformation, "Error"
  213.         End If
  214.     End With
  215. Cancel:
  216. End Sub
  217.  
  218. Private Sub cmdSave_Click()
  219. On Error GoTo Cancel
  220.     With cDialog
  221.         .CancelError = True
  222.         .FileName = vbNullString
  223.         .Filter = "Text Document (*.txt)|*.txt"
  224.         .ShowSave
  225.  
  226.         If LenB(.FileName) Then
  227.             OutputOK = True
  228.             txtOutput = .FileName
  229.             lblStatus = "Output file selected!"
  230.         Else
  231.             OutputOK = False
  232.         End If
  233.     End With
  234. Cancel:
  235. End Sub
  236.  
  237. Private Sub LoadWordList()
  238.     cmdRemDupes.Enabled = False
  239.     lblStatus = "Loading word list...": DoEvents
  240.     
  241.     Dim strBuffer As String
  242.     Call BinOpen(txtInput, strBuffer)   'File Contents -> strBuffer
  243.     Call QuickSplit(strBuffer, ArrBuffer(), vbNewLine) 'strBuffer -> String Array ArrBuffer()
  244.     
  245.     InputOK = True
  246.     
  247.     Min = LBound(ArrBuffer)
  248.     Max = UBound(ArrBuffer)
  249.     LineCount = (Max + 1) - Min
  250.     
  251.     lblLines = "Lines: " & LineCount
  252.     lblDuplicates = "Duplicates:"
  253.     lblUnique = "Unique Items:"
  254.     
  255.     lblStatus = txtInput.Tag & " loaded!"
  256.     cmdRemDupes.Enabled = True
  257. End Sub
  258.  
  259. Private Sub Reset()
  260.     Erase ArrBuffer()   'Clear Memory
  261.     Erase ArrIndexes()
  262.     InputOK = False     'Reset status
  263.     OutputOK = False
  264.     txtInput = " Input File"
  265.     txtOutput = " Ouptut File"
  266. End Sub
  267.  
  268. Private Sub ProcessWordList()
  269.     cmdRemDupes.Enabled = False
  270.     
  271.     Dim t As Double: t = Timer
  272.  
  273.     lblDuplicates = "Duplicates:"
  274.     lblUnique = "Unique Items:"
  275.     lblStatus = "Removing duplicates...": DoEvents
  276.     
  277.     If chkAlpha.Value = vbChecked Then
  278.         Call strStableSort2(ArrBuffer(), Min, Max)
  279.         Call PrintUnique 'Using Indexed Sort -> Call IndexPrintUnique
  280.     Else
  281.         ReDim ArrIndexes(Min To Max)
  282.         Call strStableSort2Indexed(ArrBuffer(), ArrIndexes(), Min, Max)
  283.         Call IndexPrintUniqueOrig
  284.     End If
  285.  
  286.     lblDuplicates = "Duplicates: " & LineCount - Unique
  287.     lblUnique = "Unique Items: " & Unique
  288.     lblStatus = "Dupes removed in " & Round(Timer - t, 3) & " secs!"
  289.     
  290.     Call Reset
  291.     cmdRemDupes.Enabled = True
  292. End Sub
  293.  
  294. Private Sub cmdRemDupes_Click()
  295.     If InputOK = False Or OutputOK = False Then
  296.         lblStatus = "Invalid Input/Output File(s)"
  297.         MsgBox "Invalid Input/Output File(s)", vbInformation, "Error"
  298.     Else
  299.         If LineCount > 0 Then
  300.             Call ProcessWordList
  301.         Else
  302.             lblStatus = "Input file is blank"
  303.         End If
  304.     End If
  305. End Sub
  306.  
  307. Private Sub PrintUnique() 'From string
  308.     Dim FF As Integer: FF = FreeFile
  309.     
  310.     Open txtOutput For Output As FF
  311.         Print #FF, ArrBuffer(Min): Unique = 1 'First Item
  312.         
  313.         Dim i As Long
  314.         For i = Min + 1 To Max          'Start w/ 2nd item, compare to previous item
  315.             If ArrBuffer(i) <> ArrBuffer(i - 1) Then
  316.                 Print #FF, ArrBuffer(i) 'Print #FF, ArrBuffer(i); 'Don't add CRLF
  317.                 Unique = Unique + 1
  318.             End If
  319.         Next i
  320.     Close FF
  321. End Sub
  322.  
  323. Private Sub IndexPrintUniqueOrig()
  324.     Dim uIndex() As Long: ReDim uIndex(Min To Max) As Long
  325.  
  326.     uIndex(ArrIndexes(Min)) = ArrIndexes(Min): Unique = 1 'First Item
  327.     
  328.     Dim i As Long
  329.     For i = Min + 1 To Max
  330.         If ArrBuffer(ArrIndexes(i)) <> ArrBuffer(ArrIndexes(i - 1)) Then
  331.             uIndex(ArrIndexes(i)) = ArrIndexes(i)
  332.             Unique = Unique + 1
  333.         Else
  334.             uIndex(ArrIndexes(i)) = -1
  335.         End If
  336.     Next i
  337.     
  338.     Dim FF As Integer: FF = FreeFile
  339.     Open txtOutput For Output As FF
  340.         For i = Min To Max
  341.             If uIndex(i) > -1 Then Print #FF, ArrBuffer(uIndex(i))
  342.         Next i
  343.     Close FF
  344. End Sub
  345.  
  346. 'Private Sub IndexPrintUnique() 'From Index
  347. '    Dim FF As Integer: FF = FreeFile
  348. '
  349. '    Open txtOutput For Output As FF
  350. '        Print #FF, ArrBuffer(ArrIndexes(Min))       'First item
  351. '        Unique = 1
  352. '
  353. '        Dim i As Long
  354. '        For i = Min + 1 To Max          'Start w/ 2nd item, compare to previous item
  355. '
  356. '            If ArrBuffer(ArrIndexes(i)) <> ArrBuffer(ArrIndexes(i - 1)) Then
  357. '                Unique = Unique + 1
  358. '
  359. '                If i < Max Then
  360. '                    Print #FF, ArrBuffer(ArrIndexes(i))
  361. '                Else
  362. '                    Print #FF, ArrBuffer(ArrIndexes(i)); 'Don't add CRLF
  363. '                End If
  364. '            End If
  365. '
  366. '        Next i
  367. '    Close FF
  368. 'End Sub
  369.  
  370. 'Private Sub Col_RemDupes() 'Determine/Remove duplicate items with a Collection
  371. '    Dim col As Collection: Set col = New Collection
  372. '    Dim FF As Integer: FF = FreeFile
  373. '
  374. '    On Error Resume Next
  375. '
  376. '    Open txtOutput For Output As FF
  377. '
  378. '        Dim i As Long
  379. '        For i = LBound(ArrBuffer) To UBound(ArrBuffer)
  380. '            col.Add ArrBuffer(i), ArrBuffer(i)
  381. '
  382. '            If Err.Number <> 457 Then
  383. '                Print #FF, ArrBuffer(i)
  384. '            Else
  385. '                Err.Clear  '457 = Item already exists in the collection
  386. '            End If
  387. '        Next i
  388. '
  389. '    Close FF
  390. '
  391. '    Set col = Nothing
  392. 'End Sub
  393.