encode_lz77.m
上传用户:xtlvip
上传日期:2022-07-06
资源大小:1k
文件大小:5k
源码类别:

压缩解压

开发平台:

Matlab

  1. function encode_lz77(str)
  2. sliding_window_length=20;
  3. search_buffer_length=0;
  4. search_buffer='';
  5. look_buffer=str;
  6. num_match = 0;
  7. index=0;
  8. match_index_insearch_arr=[];
  9. big_match=0;
  10.         big_match_insearch_arr = [];
  11.         index_of_matched_look_arr =[];
  12. big_match_inlook_arr=[];
  13. code='';
  14. while( length(look_buffer)>0 && search_buffer_length <= sliding_window_length)
  15.     % the index of the character that we will start from it
  16.       char_index=1;
  17.     char=look_buffer(char_index);
  18.     % array of the indexes that match the char in search buffer
  19.     char_index_arr=find(search_buffer == char);
  20. v='aaaaaaa';
  21. % condition if there is a match in search buffer
  22.     if(char_index_arr >0)
  23.         [big_match_insearch_arr index_big_match chars_matched_inlook]=get_biggest_match(sliding_window_length,char_index,char_index_arr,search_buffer,look_buffer); 
  24.               num_match = length(big_match_insearch_arr);
  25.               index=index_big_match;
  26.               node=write_code(index,num_match,look_buffer);
  27.     %next character to be added to search buffer with the matched data
  28.               if(numel(look_buffer)< (length(chars_matched_inlook)+1))
  29.                   search_buffer = add_search_buffer(search_buffer,sliding_window_length,chars_matched_inlook);
  30.               else
  31.               chars_matched_inlook(length(chars_matched_inlook)+1)= look_buffer(length(chars_matched_inlook)+1);
  32.               search_buffer = add_search_buffer(search_buffer,sliding_window_length,chars_matched_inlook); 
  33.               end
  34.     else
  35.         d='eeeeeeeeee';
  36.         num_match=0;
  37.         index  = 0;
  38.         node=write_code(index,num_match,look_buffer);
  39.         search_buffer=add_search_buffer(search_buffer,sliding_window_length,char); 
  40.     end
  41.    
  42.     
  43.     n_look_buffer=modify_look_buffer(look_buffer,num_match);
  44.     search_buffer;
  45.     look_buffer = n_look_buffer;
  46.     code=[code,node];
  47.     e=length(search_buffer);
  48. end
  49.     code
  50. end
  51. function look_buffer=modify_look_buffer(look_buffer,num_match)
  52. for i=1:length(look_buffer)-num_match-1
  53.     look_buffer(i)=look_buffer(num_match+i+1);
  54. end
  55. look_buffer = look_buffer(1:length(look_buffer)-num_match-1);
  56. end
  57. function code=write_code(index,num_match,look_buffer)
  58. if (numel(look_buffer)< num_match+1 )
  59.     code=['(' num2str(index) ',' num2str(num_match) ', *)'];
  60. else
  61. code=['(' num2str(index) ',' num2str(num_match) ',' look_buffer(num_match+1) ')'];
  62. end
  63. end
  64. function search_buffer = add_search_buffer(search_buffer,sliding_window_length,new_data_arr)
  65. new_data_arr;
  66. for i=1:length(new_data_arr)
  67. if(length(search_buffer) == sliding_window_length )
  68.       % modified_search_buffer = modified_buffer(search_buffer,new_data_arr);
  69.       for j=length(search_buffer)-1 :-1:1
  70.       search_buffer(j+1) = search_buffer(j);
  71.       end
  72.   search_buffer(1)=new_data_arr(i); 
  73. else
  74.     for k=length(search_buffer):-1:1
  75.         search_buffer(k+1)=search_buffer(k);
  76.     end
  77.     search_buffer(1)=new_data_arr(i);
  78. end
  79. end
  80. end
  81. function [big_match_insearch_arr index_big_match chars_matched_inlook] = get_biggest_match(sliding_window_length,char_index,char_index_arr,search_buffer,look_buffer)
  82. z='yes';
  83. big_match=0;
  84. index_big_match=0;
  85. look_buffer_length = length(look_buffer);
  86.         big_match_insearch_arr = [];
  87.         chars_matched_inlook=[];
  88.         
  89. big_match_inlook_arr=[];
  90. % counter that hold the biggest match of symbols
  91.         big_match=0;
  92.         
  93.         % loop on the array which hold search buffer index number of match 
  94.        for i=1:length(char_index_arr)
  95.            r='bbbbbbbbbbbb';
  96.            % pointer on search buffer
  97.         p_search = char_index_arr(i);
  98.         % pointer on Look buffer
  99.         p_look = char_index;
  100.                 % arrays that will hold the sequenced indexes that have a
  101.                 % match in both buffers
  102.         match_index_insearch_arr=[];
  103.         index_of_matched_look_arr =[];
  104.         matched_chars_inlook=[];
  105.         % a loop that find the  match indexes that follow every char in the array in search & look buffer
  106.         for j=0:p_search
  107.            if(p_look+j<= length(look_buffer)& look_buffer(p_look+j) == search_buffer(p_search-j) )
  108.                % add the matched index in look buffer 
  109.                index_of_matched_look_arr(length(index_of_matched_look_arr)+1) = p_look+j;
  110.                %the actual symbols in match in look buffer
  111.                matched_chars_inlook(length(matched_chars_inlook)+1)=look_buffer(p_look+j);
  112.               
  113.               % add index number to array of the number of indexes in search buffer which has a match 
  114.               match_index_insearch_arr(length(match_index_insearch_arr)+1) = p_search-j; 
  115.            else
  116.                break;
  117.            end
  118.         end
  119.     %Hold the big match to a variables  & hold he array of matched indexes in both buffers 
  120.         num_match=length(index_of_matched_look_arr);
  121.         if(num_match >= big_match)
  122.             %start index of big match
  123.             index_big_match=p_search;
  124.             big_match=num_match;
  125.             big_match_insearch_arr = match_index_insearch_arr;
  126.         big_match_inlook_arr = index_of_matched_look_arr;
  127.         chars_matched_inlook = matched_chars_inlook;
  128.        end
  129.        end
  130.         
  131. end