Hi,

I need some advice to what would be an idiomatic solution to following problem 
in X10.

The thing I want to do is just a basic search in from a map implemented using a 
binary search tree. Two thing needed are to check if an entry with given key 
exists, and return the entry associated with a given key. I have problems 
solving the situation where user wants to check if given key is in map, and if 
it is get it. I can think of number of ways, however none I'm completely 
satisfied with...

- Write two functions, contains(key:K):Boolean and get(key:K):T , where get 
throws if entry with key isn't found. Ineffective since same search would be 
made twice, once by both functions.

- Write a function get(key:K):Pair[Boolean, T] , where pair.first would 
indicate if entry was found and if true pair.second would be the value. Would 
require contraints { T haszero } or some crude hack to always get object of 
type T .

- Return a Cell[T] so that null can be returned if nothing was found

- Return a class storing a reference to node of a tree, and methods to check is 
node==null and get data associated with the node. This approach would be close 
to C++ standard library way of returning an iterator. I like this solution the 
most. Are you planning to develop your iterators to this direction?


Also, what would be the standard way of accessing elements in containers to 
write generic algorithms for containers? Like count occurences of some element, 
check if range is sorted, compare two ranges in containers... In C++ iterators 
would be the answer, again. How about in X10?

------------------------------------------------------------------------------
_______________________________________________
X10-users mailing list
X10-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/x10-users

Reply via email to