Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if(this.storeSize <= ARRAY_LENGTH_LIMIT) 语句 判断了两次,代码有冗余。 #1000

Open
geeeeeeeek opened this issue Mar 10, 2023 · 2 comments

Comments

@geeeeeeeek
Copy link

	private DictSegment lookforSegment(Character keyChar ,  int create){
		
		DictSegment ds = null;

		if(this.storeSize <= ARRAY_LENGTH_LIMIT){
			//获取数组容器,如果数组未创建则创建数组
			DictSegment[] segmentArray = getChildrenArray();			
			//搜寻数组
			DictSegment keySegment = new DictSegment(keyChar);
			int position = Arrays.binarySearch(segmentArray, 0 , this.storeSize, keySegment);
			if(position >= 0){
				ds = segmentArray[position];
			}
		
			//遍历数组后没有找到对应的segment
			if(ds == null && create == 1){
				ds = keySegment;
				if(this.storeSize < ARRAY_LENGTH_LIMIT){
					//数组容量未满,使用数组存储
					segmentArray[this.storeSize] = ds;
					//segment数目+1
					this.storeSize++;
					Arrays.sort(segmentArray , 0 , this.storeSize);
					
				}else{
					//数组容量已满,切换Map存储
					//获取Map容器,如果Map未创建,则创建Map
					Map<Character , DictSegment> segmentMap = getChildrenMap();
					//将数组中的segment迁移到Map中
					migrate(segmentArray ,  segmentMap);
					//存储新的segment
					segmentMap.put(keyChar, ds);
					//segment数目+1 ,  必须在释放数组前执行storeSize++ , 确保极端情况下,不会取到空的数组
					this.storeSize++;
					//释放当前的数组引用
					this.childrenArray = null;
				}

			}			
			
		}else{
			//获取Map容器,如果Map未创建,则创建Map
			Map<Character , DictSegment> segmentMap = getChildrenMap();
			//搜索Map
			ds = (DictSegment)segmentMap.get(keyChar);
			if(ds == null && create == 1){
				//构造新的segment
				ds = new DictSegment(keyChar);
				segmentMap.put(keyChar , ds);
				//当前节点存储segment数目+1
				this.storeSize ++;
			}
		}

		return ds;
	}

if(this.storeSize <= ARRAY_LENGTH_LIMIT) 语句 判断了两次,代码有冗余。

@SystemXPlus
Copy link

SystemXPlus commented Mar 10, 2023 via email

@geeeeeeeek
Copy link
Author

两个if语句的判断条件不一样,外层if判断的是 <= , 内层if判断的是 <

------------------ 原始邮件 ------------------ 发件人: "medcl/elasticsearch-analysis-ik" @.>; 发送时间: 2023年3月10日(星期五) 下午4:16 @.>; @.>; 主题: [medcl/elasticsearch-analysis-ik] if(this.storeSize <= ARRAY_LENGTH_LIMIT) 语句 判断了两次,代码有冗余。 (Issue #1000) private DictSegment lookforSegment(Character keyChar , int create){ DictSegment ds = null; if(this.storeSize <= ARRAY_LENGTH_LIMIT){ //获取数组容器,如果数组未创建则创建数组 DictSegment[] segmentArray = getChildrenArray(); //搜寻数组 DictSegment keySegment = new DictSegment(keyChar); int position = Arrays.binarySearch(segmentArray, 0 , this.storeSize, keySegment); if(position >= 0){ ds = segmentArray[position]; } //遍历数组后没有找到对应的segment if(ds == null && create == 1){ ds = keySegment; if(this.storeSize < ARRAY_LENGTH_LIMIT){ //数组容量未满,使用数组存储 segmentArray[this.storeSize] = ds; //segment数目+1 this.storeSize++; Arrays.sort(segmentArray , 0 , this.storeSize); }else{ //数组容量已满,切换Map存储 //获取Map容器,如果Map未创建,则创建Map Map<Character , DictSegment> segmentMap = getChildrenMap(); //将数组中的segment迁移到Map中 migrate(segmentArray , segmentMap); //存储新的segment segmentMap.put(keyChar, ds); //segment数目+1 , 必须在释放数组前执行storeSize++ , 确保极端情况下,不会取到空的数组 this.storeSize++; //释放当前的数组引用 this.childrenArray = null; } } }else{ //获取Map容器,如果Map未创建,则创建Map Map<Character , DictSegment> segmentMap = getChildrenMap(); //搜索Map ds = (DictSegment)segmentMap.get(keyChar); if(ds == null && create == 1){ //构造新的segment ds = new DictSegment(keyChar); segmentMap.put(keyChar , ds); //当前节点存储segment数目+1 this.storeSize ++; } } return ds; } if(this.storeSize <= ARRAY_LENGTH_LIMIT) 语句 判断了两次,代码有冗余。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.>

oh, Yeah, Great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants