Advertisement
ahmedtsadek

lz77

May 26th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package AppPack;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. /**
  16. *
  17. * @author Sara
  18. */
  19. public class LZ77 {
  20. //Compress method that take string data to work on, and returns a list of tags as a result of Compressing this data
  21. public List<LZ77Tupel> compress( String text )
  22. {
  23. //Create a list of typy "LZ77Tupel"
  24. List<LZ77Tupel> list = new ArrayList<LZ77Tupel>();
  25. //search variable to contain the history characters that occurs for the first time
  26. String search = "";
  27. // input variable now = the content of the text variable.
  28. //Remember that text variable contain the data to be compressed
  29. String input = text;
  30. // next variable works as a pointer that points to the next character in the buffer
  31. String next;
  32. int indexGood = 0;
  33. int indexFound;
  34. int searchLen;
  35. // while there is a data in the look ahead buffer keep compressing
  36. while ( input.length() > 0 )
  37. {
  38. searchLen = 1;
  39. // get the first character in the buffer using substring function
  40. next = input.substring( 0, searchLen );
  41. // check if the character is in your search history or not
  42. // in other words check if this is the first time that character happens or not
  43. if ( search.indexOf( next ) == -1 )
  44. {
  45. // Insert the tag values in the list
  46. list.add( new LZ77Tupel(0,0, next ) );
  47. System.out.println("tag "+"<0,"+"0,"+next+">");
  48. // add characters in your search history
  49. search += next;
  50. System.out.println("This is search history: "+search);
  51. // Remove the current character from you input data
  52. //Because you have finished using it
  53. input = input.substring( searchLen );
  54. // Continue to go back at the start of the while loop
  55. //dont continue in the do-while loop
  56. continue;
  57. }
  58. // When the last if condition is wrong
  59. //This means that the character you points to now is exist in your search history and used before
  60. //Go Inside Do-While loop
  61. do
  62. {
  63. next = input.substring( 0, searchLen );
  64. System.out.println("The current character now is: "+next);
  65. // Return the index of the current character from the search history
  66. indexFound = search.indexOf( next );
  67. System.out.println("the Current character"+next +"Was fount at position "+indexFound+"in the search history");
  68. if ( indexFound != -1 )
  69. indexGood = indexFound;
  70. // increase the search length by 1;
  71. searchLen++;
  72. System.out.println("search length now "+searchLen);
  73. }
  74. while ( indexFound != -1 );
  75. // Add the new characters in the search history
  76. search += next.substring( 0, next.length() );
  77. System.out.println("This is search history: "+search);
  78. // Remove the used characters from the input data.
  79. input = input.substring( next.length() );
  80. list.add( new LZ77Tupel( indexGood, next.length() - 1, next.substring( next.length() - 1 ) ) );
  81. System.out.println("tag < "+indexGood+","+(next.length()- 1)+","+next.substring(( next.length()- 1))+">");
  82. }
  83. // Return the List of tags
  84. return list;
  85.  
  86. }
  87.  
  88. public static void main(String[] args) throws FileNotFoundException, IOException {
  89. LZ77 lz77 = new LZ77();
  90.  
  91. List<LZ77Tupel> list1 = lz77.compress( "abcaaabacaaaa" );
  92.  
  93. System.out.println( "Compressed1: " + list1.size() + " tupels." );
  94.  
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement