java - 3 dimensional ConcurrentSkipListMap map -
i have written 3 dimensional concurrentskiplistmap, not able figure out way iterate on it. how define iterator same.
import java.util.concurrent.concurrentskiplistmap; /** * helper implementation handle 3 dimensional sorted maps */ public class mycustomindex { private concurrentskiplistmap<byte[], concurrentskiplistmap<byte[], concurrentskiplistmap<byte[], byte[]>>> table; public mycustomindex() { this.table = new concurrentskiplistmap<byte[], concurrentskiplistmap<byte[], concurrentskiplistmap<byte[], byte[]>>>(new customcomparator); } /** * * @param k * @param f * @param q */ public void put(byte[] k, byte[] f, byte[] q) { concurrentskiplistmap<byte[], byte[]> qtodummyvaluemap; concurrentskiplistmap<byte[], concurrentskiplistmap<byte[], byte[]>> ftoqmap; if( table.containsk(k)) { ftoqmap = table.get(k); if ( ftoqmap.containsk(f)) { qtodummyvaluemap = ftoqmap.get(f); } else { qtodummyvaluemap = new concurrentskiplistmap<byte[], byte[]>(new customcomparator); } } else { qtodummyvaluemap = new concurrentskiplistmap<byte[], byte[]>(new customcomparator); ftoqmap = new concurrentskiplistmap<byte[], concurrentskiplistmap<byte[], byte[]>>(new customcomparator); } qtodummyvaluemap.put(q, new byte[0]); ftoqmap.put(f, qtodummyvaluemap); table.put(k, ftoqmap); } public concurrentskiplistmap<byte[], concurrentskiplistmap<byte[], concurrentskiplistmap<byte[], byte[]>>> gettable() { return table; } public void settable( concurrentskiplistmap<byte[], concurrentskiplistmap<byte[], concurrentskiplistmap<byte[], byte[]>>> table) { this.table = table; } }
here's nested iterator take iterator<iterator<t>> , turn iterator<t>. there static methods allow grow iterator<v> objects out of map<k,v>s along higher-order stuff (iterator<iterator<v>> , map<k,map<k,v> etc.).
the idea iterating on higher levels of iterator such iterator<iterator<iterator<t>>> wrap 1 of these inside other can see in three-way test , mapmapmap test.
public class nestediterator<t> implements iterator<t> { // outer iterator. goes null when exhausted. iterator<iterator<t>> i2 = null; // inner iterator. goes null when exhausted. iterator<t> i1 = null; // next value. t next = null; // takes depth-2 iterator. public nestediterator(iterator<iterator<t>> i2) { this.i2 = i2; // prime pump. if (i2 != null && i2.hasnext()) { i1 = i2.next(); } } @override public boolean hasnext() { // there 1 waiting? if (next == null) { // no! // i1 go null if exhausted. if (i1 == null) { // i1 exhausted! new 1 i2. if (i2 != null && i2.hasnext()) { /// next. i1 = i2.next(); // set i2 null if exhausted. if (!i2.hasnext()) { // exhausted. i2 = null; } } else { // exhausted. i2 = null; } } // null i1 mean over! if (i1 != null) { if (i1.hasnext()) { // next. next = i1.next(); // set i1 null if exhausted. if (!i1.hasnext()) { // exhausted. i1 = null; } } else { // exhausted. i1 = null; } } } return next != null; } @override public t next() { t n = next; next = null; return n; } @override public void remove() { throw new unsupportedoperationexception("not supported."); } // iterating across maps of maps of maps. static <k1, k2, k3, v> iterator<iterator<iterator<v>>> iiiv(map<k1, map<k2, map<k3, v>>> i) { final iterator<map<k2, map<k3, v>>> iv = iv(i); return new iterator<iterator<iterator<v>>>() { @override public boolean hasnext() { return iv.hasnext(); } @override public iterator<iterator<v>> next() { return iiv(iv.next()); } @override public void remove() { iv.remove(); } }; } // iterating across maps of maps. static <k1, k2, v> iterator<iterator<v>> iiv(map<k1, map<k2, v>> i) { final iterator<map<k2, v>> iv = iv(i); return new iterator<iterator<v>>() { @override public boolean hasnext() { return iv.hasnext(); } @override public iterator<v> next() { return iv(iv.next()); } @override public void remove() { iv.remove(); } }; } // iterating across map values. static <k, v> iterator<v> iv(final map<k, v> map) { return iv(map.entryset().iterator()); } // iterating across map.entry iterators. static <k, v> iterator<v> iv(final iterator<map.entry<k, v>> i) { return new iterator<v>() { @override public boolean hasnext() { return i.hasnext(); } @override public v next() { return i.next().getvalue(); } @override public void remove() { i.remove(); } }; } // **** testing **** enum { i1, i2, i3; }; public static void main(string[] args) { // 2 way test. testtwoway(); system.out.flush(); system.err.flush(); // 3 way test. testthreeway(); system.out.flush(); system.err.flush(); // mapmap test testmapmap(); system.out.flush(); system.err.flush(); // mapmapmap test testmapmapmap(); system.out.flush(); system.err.flush(); } private static void testmapmap() { map<string,string> m = new treemap<> (); m.put("m-1", "v-1"); m.put("m-2", "v-2"); map<string,map<string,string>> mm = new treemap<> (); mm.put("mm-1", m); mm.put("mm-2", m); system.out.println("mapmap"); iterator<iterator<string>> iiv = iiv(mm); (iterator<string> = new nestediterator<>(iiv); i.hasnext();) { system.out.print(i.next() + ","); } system.out.println(); } private static void testmapmapmap() { map<string,string> m = new treemap<> (); m.put("m-1", "v-1"); m.put("m-2", "v-2"); m.put("m-3", "v-3"); map<string,map<string,string>> mm = new treemap<> (); mm.put("mm-1", m); mm.put("mm-2", m); map<string,map<string,map<string,string>>> mmm = new treemap<> (); mmm.put("mmm-1", mm); mmm.put("mmm-2", mm); system.out.println("mapmapmap"); iterator<iterator<iterator<string>>> iiiv = iiiv(mmm); (iterator<string> = new nestediterator<>(new nestediterator<>(iiiv)); i.hasnext();) { system.out.print(i.next() + ","); } system.out.println(); } private static void testthreeway() { // 3 way test. system.out.println("three way"); list<iterator<i>> lii1 = arrays.aslist( enumset.allof(i.class).iterator(), enumset.allof(i.class).iterator(), enumset.allof(i.class).iterator(), enumset.allof(i.class).iterator()); list<iterator<i>> lii2 = arrays.aslist( enumset.allof(i.class).iterator(), enumset.allof(i.class).iterator(), enumset.allof(i.class).iterator(), enumset.allof(i.class).iterator()); list<iterator<i>> lii3 = arrays.aslist( enumset.allof(i.class).iterator(), enumset.allof(i.class).iterator(), enumset.allof(i.class).iterator(), enumset.allof(i.class).iterator()); iterator<iterator<iterator<i>>> liii = arrays.aslist( lii1.iterator(), lii2.iterator(), lii3.iterator()).iterator(); // grow 3-nest. // unroll it. (iterator<i> ii = new nestediterator<>(new nestediterator<>(liii)); ii.hasnext();) { = ii.next(); system.out.print(it + ","); } system.out.println(); } private static void testtwoway() { system.out.println("two way"); list<iterator<i>> lii = arrays.aslist( enumset.allof(i.class).iterator(), enumset.allof(i.class).iterator(), enumset.allof(i.class).iterator()); (iterator<i> ii = new nestediterator<>(lii.iterator()); ii.hasnext();) { = ii.next(); system.out.print(it + ","); } system.out.println(); } } your code can this. note have not tested @ , have made use of map instead of concurrentskiplistmap whenever possible , using <> stuff java 7 out lot.
public class mycustomindex implements iterable<byte[]> { private map<byte[], map<byte[], map<byte[], byte[]>>> table; public mycustomindex() { this.table = new concurrentskiplistmap<>(); } /** * @param k * @param f * @param q */ public void put(byte[] k, byte[] f, byte[] q) { map<byte[], byte[]> qtodummyvaluemap; map<byte[], map<byte[], byte[]>> ftoqmap; if (table.containskey(k)) { ftoqmap = table.get(k); if (ftoqmap.containskey(f)) { qtodummyvaluemap = ftoqmap.get(f); } else { qtodummyvaluemap = new concurrentskiplistmap<>(); } } else { qtodummyvaluemap = new concurrentskiplistmap<>(); ftoqmap = new concurrentskiplistmap<>(); } qtodummyvaluemap.put(q, new byte[0]); ftoqmap.put(f, qtodummyvaluemap); table.put(k, ftoqmap); } public map<byte[], map<byte[], map<byte[], byte[]>>> gettable() { return table; } public iterator<byte[]> iterator () { // **** have been aiming @ along **** return new nestediterator(new nestediterator<>(nestediterator.iiiv(table))); } }
Comments
Post a Comment