@@ -411,6 +411,66 @@ defmodule BiMap do
411411 def put ( bimap , kv )
412412 def put ( bimap , { key , value } ) , do: put ( bimap , key , value )
413413
414+ @ doc """
415+ Inserts `{key, value}` pair into `bimap` if `key` is not already in `bimap`.
416+
417+ If `key` already exists in `bimap`, `bimap` is returned unchanged.
418+
419+ If `key` does not exist and `value` is already in `bimap`, any overlapping bindings are
420+ deleted.
421+
422+ ## Examples
423+
424+ iex> bimap = BiMap.new
425+ #BiMap<[]>
426+ iex> bimap = BiMap.put_new_key(bimap, :a, 0)
427+ #BiMap<[a: 0]>
428+ iex> bimap = BiMap.put_new_key(bimap, :a, 1)
429+ #BiMap<[a: 0]>
430+ iex> BiMap.put_new_key(bimap, :b, 1)
431+ #BiMap<[a: 0, b: 1]>
432+ iex> BiMap.put_new_key(bimap, :c, 1)
433+ #BiMap<[a: 0, c: 1]>
434+ """
435+ @ spec put_new_key ( t , k , v ) :: t
436+ def put_new_key ( % BiMap { } = bimap , key , value ) do
437+ if BiMap . has_key? ( bimap , key ) do
438+ bimap
439+ else
440+ put ( bimap , key , value )
441+ end
442+ end
443+
444+ @ doc """
445+ Inserts `{key, value}` pair into `bimap` if `value` is not already in `bimap`.
446+
447+ If `value` already exists in `bimap`, `bimap` is returned unchanged.
448+
449+ If `value` does not exist and `key` is already in `bimap`, any overlapping bindings are
450+ deleted.
451+
452+ ## Examples
453+
454+ iex> bimap = BiMap.new
455+ #BiMap<[]>
456+ iex> bimap = BiMap.put_new_value(bimap, :a, 0)
457+ #BiMap<[a: 0]>
458+ iex> bimap = BiMap.put_new_value(bimap, :a, 1)
459+ #BiMap<[a: 1]>
460+ iex> BiMap.put_new_value(bimap, :b, 1)
461+ #BiMap<[a: 1]>
462+ iex> BiMap.put_new_value(bimap, :c, 2)
463+ #BiMap<[a: 1, c: 2]>
464+ """
465+ @ spec put_new_value ( t , k , v ) :: t
466+ def put_new_value ( % BiMap { } = bimap , key , value ) do
467+ if BiMap . has_value? ( bimap , value ) do
468+ bimap
469+ else
470+ put ( bimap , key , value )
471+ end
472+ end
473+
414474 @ doc """
415475 Deletes `{key, value}` pair from `bimap`.
416476
0 commit comments