2009-08-13 1 views
15

클로저에서 카운트의 시간 복잡도는 얼마입니까?클로저에서 카운트 기능의 시간 복잡도는 얼마입니까?

public static int count(Object o){ 
     if(o == null) 
       return 0; 
     else if(o instanceof Counted) 
       return ((Counted) o).count(); 
     else if(o instanceof IPersistentCollection) { 
       ISeq s = seq(o); 
       o = null; 
       int i = 0; 
       for(; s != null; s = s.next()) { 
         if(s instanceof Counted) 
           return i + s.count(); 
         i++; 
       } 
       return i; 
     } 
     else if(o instanceof String) 
       return ((String) o).length(); 
     else if(o instanceof Collection) 
       return ((Collection) o).size(); 
     else if(o instanceof Map) 
       return ((Map) o).size(); 
     else if(o.getClass().isArray()) 
       return Array.getLength(o); 

     throw new UnsupportedOperationException("count not supported on this type: " + o.getClass().getSimpleName()); 
} 

그래서 O의 (1) 카운트 된 구현합니다 배열, 문자열, 컬렉션,지도와 아무것도 :

답변

21

여기에 구현입니다. IPersistentCollection을 구현하지만 Counted는 구현하지 않는 경우 O (n)입니다.