encode_lz77.m
上传用户:xtlvip
上传日期:2022-07-06
资源大小:1k
文件大小:5k
- function encode_lz77(str)
- sliding_window_length=20;
- search_buffer_length=0;
- search_buffer='';
- look_buffer=str;
- num_match = 0;
- index=0;
- match_index_insearch_arr=[];
- big_match=0;
- big_match_insearch_arr = [];
- index_of_matched_look_arr =[];
- big_match_inlook_arr=[];
- code='';
- while( length(look_buffer)>0 && search_buffer_length <= sliding_window_length)
- % the index of the character that we will start from it
- char_index=1;
- char=look_buffer(char_index);
- % array of the indexes that match the char in search buffer
- char_index_arr=find(search_buffer == char);
- v='aaaaaaa';
- % condition if there is a match in search buffer
- if(char_index_arr >0)
- [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);
- num_match = length(big_match_insearch_arr);
- index=index_big_match;
- node=write_code(index,num_match,look_buffer);
- %next character to be added to search buffer with the matched data
- if(numel(look_buffer)< (length(chars_matched_inlook)+1))
- search_buffer = add_search_buffer(search_buffer,sliding_window_length,chars_matched_inlook);
- else
- chars_matched_inlook(length(chars_matched_inlook)+1)= look_buffer(length(chars_matched_inlook)+1);
- search_buffer = add_search_buffer(search_buffer,sliding_window_length,chars_matched_inlook);
- end
- else
- d='eeeeeeeeee';
- num_match=0;
- index = 0;
- node=write_code(index,num_match,look_buffer);
- search_buffer=add_search_buffer(search_buffer,sliding_window_length,char);
- end
-
-
- n_look_buffer=modify_look_buffer(look_buffer,num_match);
- search_buffer;
- look_buffer = n_look_buffer;
- code=[code,node];
- e=length(search_buffer);
- end
- code
- end
- function look_buffer=modify_look_buffer(look_buffer,num_match)
- for i=1:length(look_buffer)-num_match-1
- look_buffer(i)=look_buffer(num_match+i+1);
- end
- look_buffer = look_buffer(1:length(look_buffer)-num_match-1);
- end
- function code=write_code(index,num_match,look_buffer)
- if (numel(look_buffer)< num_match+1 )
- code=['(' num2str(index) ',' num2str(num_match) ', *)'];
- else
- code=['(' num2str(index) ',' num2str(num_match) ',' look_buffer(num_match+1) ')'];
- end
- end
- function search_buffer = add_search_buffer(search_buffer,sliding_window_length,new_data_arr)
- new_data_arr;
- for i=1:length(new_data_arr)
- if(length(search_buffer) == sliding_window_length )
- % modified_search_buffer = modified_buffer(search_buffer,new_data_arr);
- for j=length(search_buffer)-1 :-1:1
- search_buffer(j+1) = search_buffer(j);
- end
- search_buffer(1)=new_data_arr(i);
- else
- for k=length(search_buffer):-1:1
- search_buffer(k+1)=search_buffer(k);
- end
- search_buffer(1)=new_data_arr(i);
- end
- end
- end
- 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)
- z='yes';
- big_match=0;
- index_big_match=0;
- look_buffer_length = length(look_buffer);
- big_match_insearch_arr = [];
- chars_matched_inlook=[];
-
- big_match_inlook_arr=[];
- % counter that hold the biggest match of symbols
- big_match=0;
-
- % loop on the array which hold search buffer index number of match
- for i=1:length(char_index_arr)
- r='bbbbbbbbbbbb';
- % pointer on search buffer
- p_search = char_index_arr(i);
- % pointer on Look buffer
- p_look = char_index;
- % arrays that will hold the sequenced indexes that have a
- % match in both buffers
- match_index_insearch_arr=[];
- index_of_matched_look_arr =[];
- matched_chars_inlook=[];
- % a loop that find the match indexes that follow every char in the array in search & look buffer
- for j=0:p_search
- if(p_look+j<= length(look_buffer)& look_buffer(p_look+j) == search_buffer(p_search-j) )
- % add the matched index in look buffer
- index_of_matched_look_arr(length(index_of_matched_look_arr)+1) = p_look+j;
- %the actual symbols in match in look buffer
- matched_chars_inlook(length(matched_chars_inlook)+1)=look_buffer(p_look+j);
-
- % add index number to array of the number of indexes in search buffer which has a match
- match_index_insearch_arr(length(match_index_insearch_arr)+1) = p_search-j;
- else
- break;
- end
- end
- %Hold the big match to a variables & hold he array of matched indexes in both buffers
- num_match=length(index_of_matched_look_arr);
- if(num_match >= big_match)
- %start index of big match
- index_big_match=p_search;
- big_match=num_match;
- big_match_insearch_arr = match_index_insearch_arr;
- big_match_inlook_arr = index_of_matched_look_arr;
- chars_matched_inlook = matched_chars_inlook;
- end
- end
-
- end