/** * Remove a pair from the mapping. This operation removes a (key,id) pair from the mapping. * * @param key is the key whose id is to be released */ public void dropPair(long key) { int id = ht.lookup(key); if (id == 0) return; ht.remove(key); ids.swap(id); cnt--; }
/** * Create a String representation of the IdMap. * * @param s is the String in which the */ public String toString() { String s = "{ "; for (int i = firstId(); i != 0; i = nextId(i)) { s += "(" + ht.getKey(i) + "," + i + ") "; } return s + "}"; }
/** * Add a new key-id pair. * * @param key is the key for which an id is required * @param id is the requested id that the key is to be mapped to * @return the new id or 0 if the key is already mapped or the id is already in use or the * operation fails */ public int addPair(long key, int id) { if (validKey(key) || validId(id)) return 0; if (!ht.insert(key, id)) return 0; ids.swap(id); cnt++; return id; }
/** * Add a new key-id pair. * * @param key is the key for which an id is required * @return the new id or 0 if the key is already mapped or the operation fails */ public int addPair(long key) { if (validKey(key) || (ids.firstOut() == 0)) return 0; int id = ids.firstOut(); if (!ht.insert(key, id)) return 0; ids.swap(id); cnt++; return id; }
/** * Get the id for a given key. * * @param key is the key for which the id is required * @return the corresponding id or 0 if the key is not mapped or the operation fails */ public int getId(long key) { return ht.lookup(key); }
/** * Determine if a given key has been mapped to an identfier. * * @param key is the key to be checked * @return true if the key has been mapped, else false */ public boolean validKey(long key) { return (ht.lookup(key) != 0); }
/** * Get the key that was mapped to the given identifier * * @param id is the identifier whose key is to be returned * @return the key that maps to id, or 0 if there is none */ public long getKey(int id) { return (validId(id) ? ht.getKey(id) : 0); }