2017-10-06 9 views
2

파이썬에서 목록이 있으면 색인을 찾을 수 있습니다. 이렇게하면 사물을 추가 할 때 ID를 계속 실행할 수 있습니다.Chapel에서 사전 형 데이터를 찾기 위해 양방향 검색을 수행하려면 어떻게해야합니까?

> things = [] 
> things.append("spinach") 
> things.append("carrots") 
> things.index("carrots") 
1 

그래서 야채 (또는 덩이 식물)를주세요. ID를 찾을 수 있습니다. 신분증이 주어지면 나는 채소 (또는 결절)를 발견 할 수 있습니다.

알 수없는 개체 수와 이름 또는 ID에서 참조 할 수있는 채플의 동일한 패턴은 무엇입니까?

+0

'things '가 커질 경우 사전을 사용하여 역 매핑을 수행해야합니다. – kindall

답변

2

당신은 1D 직사각형 배열 push_backfind를 사용할 수 있습니다 @kindall이 사전은 아마 더 나은 선택입니다 언급 있도록 find는 선형 검색을 수행

var A : [1..0] string; 
A.push_back("spinach"); 
A.push_back("carrots"); 
const (found, idx) = A.find("carrots"); 
if found then writeln("Found at: ", idx); 
// Found at: 2 

하는 것으로. 인덱스는 밀도 범위에없는 경우

var thingsDom : domain(string); 
var things : [thingsDom] int; 
var idxToThing : [1..0] string; 
// ... 
// add a thing 
idxToThing.push_back(something); 
const newIdx = idxToThing.domain.last; 
thingsDom.add(something); 
things[something] = newIdx; 

assert(idxToThing[things[something]] == something); 

두 연관 배열이 더 나은 것 : 채플에서, 그 연관 도메인/배열을 의미한다.