/// Insert a new key-value binding.
Insert a new key-value binding.
Insert a new key - value binding .
pub fn insert<R: AsRange<Item = K>>(&mut self, key: R, value: V)
	where
		K: Clone + PartialEnum + Measure,
		V: PartialEq + Clone,
	{
		let mut key = AnyRange::from(key);
		if key.is_empty() {
			return;
		}
		match self.address_of(&key, true) {
			Ok(mut addr) => {
				// let mut value = Some(value);
				let mut next_addr = self.btree.next_item_address(addr);
				loop {
					let (prev_addr, prev_next_addr) = {
						let product = key.product(self.btree.item(addr).unwrap().key()).cloned();
						let mut removed_item_value = None;
						if let Some(ProductArg::Object(item_after)) = product.after {
							let item = self.btree.item_mut(addr).unwrap();
							item.set_key(item_after);
							removed_item_value = Some(item.value().clone());
						}
						match product.before {
							Some(ProductArg::Object(item_before)) => {
								match removed_item_value {
									Some(old_value) => {
										if old_value == value {
											key.add(&item_before);
											self.insert_item(addr, key, value);
										} else {
											let (addr, _) = self.insert_item(addr, key, value);
											self.insert_item(addr, item_before, old_value);
										}
									}
									None => {
										if *self.btree.item(addr).unwrap().value() == value {
											key.add(&item_before);
											self.set_item_key(addr, next_addr, key);
										} else {
											let (_, _, old_value) =
												self.set_item(addr, next_addr, key, value);
											self.insert_item(addr, item_before, old_value);
										}
									}
								}
								break;
							}
							Some(ProductArg::Subject(_)) | None => {
								match self.btree.previous_item_address(addr) {
									Some(prev_addr)
										if self
											.btree
											.item(prev_addr)
											.unwrap()
											.key()
											.connected_to(&key) =>
									{
										// We can move one to the previous item.
										let (prev_addr, addr) = if removed_item_value.is_none() {
											self.remove_item(addr)
										} else {
											(prev_addr, Some(addr))
										};
										(prev_addr, addr)
									}
									_ => {
										// There is no previous item, we must get it done now.
										if removed_item_value.is_some() {
											self.insert_item(addr, key, value);
										} else {
											self.set_item(addr, next_addr, key, value);
										}
										break;
									}
								}
							}
						}
					};
					addr = prev_addr;
					next_addr = prev_next_addr;
				}
				// // some work to do here...
				// loop {
				// 	let item = self.btree.item(addr).unwrap();
				// 	match item.key().without(&key) {
				// 		Difference::Split(left, right) => {
				// 			let left = left.cloned();
				// 			let right = right.cloned();
				// 			if item.value() == &value {
				// 				// nothing to do.
				// 			} else {
				// 				let right_value = {
				// 					let item = self.btree.item_mut(addr).unwrap();
				// 					*item.key_mut() = right;
				// 					item.value().clone()
				// 				};
				// 				addr = self.btree.insert_at(addr, Item::new(key.into(), value));
				// 				self.btree.insert_at(addr, Item::new(left, right_value));
				// 			}
				// 			// Because we have `Some(left)`
				// 			// we know nothing on the left intersects the input range.
				// 			break // we are done
				// 		},
				// 		Difference::Before(left, _) => {
				// 			let left = left.cloned();
				// 			let same_as_prev = item.value() == &value;
				// 			let same_as_next = next_addr.is_some();
				// 			if same_as_prev {
				// 				if same_as_next {
				// 					let next_item = self.btree.item_mut(next_addr.unwrap()).unwrap();
				// 					next_item.key_mut().add(&left);
				// 					self.btree.remove_at(addr);
				// 				} else {
				// 					let item = self.btree.item_mut(addr).unwrap();
				// 					item.key_mut().add(&key);
				// 				}
				// 			} else {
				// 				if same_as_next {
				// 					// nothing to do.
				// 				} else {
				// 					let item = self.btree.item_mut(addr).unwrap();
				// 					std::mem::swap(item.value_mut(), &mut value);
				// 					*item.key_mut() = key.clone().into();
				// 					self.btree.insert_at(addr, Item::new(left, value));
				// 				}
				// 			}
				// 			// Because we have `Some(left)`
				// 			// we know nothing on the left intersects the input range.
				// 			break // we are done
				// 		},
				// 		Difference::After(right, _) => {
				// 			let right = right.cloned();
				// 			if item.value() == &value {
				// 				let item = self.btree.item_mut(addr).unwrap();
				// 				item.key_mut().add(&key);
				// 			} else {
				// 				let item = self.btree.item_mut(addr).unwrap();
				// 				*item.key_mut() = right;
				// 				addr = self.btree.insert_at(addr, Item::new(key.clone().into(), value.clone()));
				// 			}
				// 		},
				// 		Difference::Empty => {
				// 			let same_as_next = next_addr.map(|next_addr| self.btree.item(next_addr).unwrap().value() == &value).unwrap_or(false);
				// 			if same_as_next {
				// 				self.btree.remove_at(addr);
				// 			} else {
				// 				let item = self.btree.item_mut(addr).unwrap();
				// 				item.key_mut().add(&key);
				// 				item.set_value(value.clone());
				// 			}
				// 		}
				// 	}
				// 	// go to the previous item is it also intersects the input range.
				// 	match self.btree.previous_item_address(addr) {
				// 		Some(prev_addr) if self.btree.item(prev_addr).unwrap().key().connected_to(&key) => {
				// 			next_addr = Some(addr);
				// 			addr = prev_addr
				// 		},
				// 		_ => break // otherwise we're done.
				// 	}
				// }
			}
			Err(addr) => {
				// case (G)
				self.btree.insert_at(addr, Item::new(key, value));
			}
		}
	}
pub fn insert < R : AsRange < Item = K > > ( & mut self , key : R , value : V ) where K : Clone + PartialEnum + Measure , V : PartialEq + Clone , { let mut key = AnyRange :: from ( key ) ; if key . is_empty ( ) { return ; } match self . address_of ( & key , true ) { Ok ( mut addr ) => { // let mut value = Some(value); let mut next_addr = self . btree . next_item_address ( addr ) ; loop { let ( prev_addr , prev_next_addr ) = { let product = key . product ( self . btree . item ( addr ) . unwrap ( ) . key ( ) ) . cloned ( ) ; let mut removed_item_value = None ; if let Some ( ProductArg :: Object ( item_after ) ) = product . after { let item = self . btree . item_mut ( addr ) . unwrap ( ) ; item . set_key ( item_after ) ; removed_item_value = Some ( item . value ( ) . clone ( ) ) ; } match product . before { Some ( ProductArg :: Object ( item_before ) ) => { match removed_item_value { Some ( old_value ) => { if old_value == value { key . add ( & item_before ) ; self . insert_item ( addr , key , value ) ; } else { let ( addr , _ ) = self . insert_item ( addr , key , value ) ; self . insert_item ( addr , item_before , old_value ) ; } } None => { if * self . btree . item ( addr ) . unwrap ( ) . value ( ) == value { key . add ( & item_before ) ; self . set_item_key ( addr , next_addr , key ) ; } else { let ( _ , _ , old_value ) = self . set_item ( addr , next_addr , key , value ) ; self . insert_item ( addr , item_before , old_value ) ; } } } break ; } Some ( ProductArg :: Subject ( _ ) ) | None => { match self . btree . previous_item_address ( addr ) { Some ( prev_addr ) if self . btree . item ( prev_addr ) . unwrap ( ) . key ( ) . connected_to ( & key ) => { // We can move one to the previous item. let ( prev_addr , addr ) = if removed_item_value . is_none ( ) { self . remove_item ( addr ) } else { ( prev_addr , Some ( addr ) ) } ; ( prev_addr , addr ) } _ => { // There is no previous item, we must get it done now. if removed_item_value . is_some ( ) { self . insert_item ( addr , key , value ) ; } else { self . set_item ( addr , next_addr , key , value ) ; } break ; } } } } } ; addr = prev_addr ; next_addr = prev_next_addr ; } // // some work to do here... // loop { // 	let item = self.btree.item(addr).unwrap(); // 	match item.key().without(&key) { // 		Difference::Split(left, right) => { // 			let left = left.cloned(); // 			let right = right.cloned(); // 			if item.value() == &value { // 				// nothing to do. // 			} else { // 				let right_value = { // 					let item = self.btree.item_mut(addr).unwrap(); // 					*item.key_mut() = right; // 					item.value().clone() // 				}; // 				addr = self.btree.insert_at(addr, Item::new(key.into(), value)); // 				self.btree.insert_at(addr, Item::new(left, right_value)); // 			} // 			// Because we have `Some(left)` // 			// we know nothing on the left intersects the input range. // 			break // we are done // 		}, // 		Difference::Before(left, _) => { // 			let left = left.cloned(); // 			let same_as_prev = item.value() == &value; // 			let same_as_next = next_addr.is_some(); // 			if same_as_prev { // 				if same_as_next { // 					let next_item = self.btree.item_mut(next_addr.unwrap()).unwrap(); // 					next_item.key_mut().add(&left); // 					self.btree.remove_at(addr); // 				} else { // 					let item = self.btree.item_mut(addr).unwrap(); // 					item.key_mut().add(&key); // 				} // 			} else { // 				if same_as_next { // 					// nothing to do. // 				} else { // 					let item = self.btree.item_mut(addr).unwrap(); // 					std::mem::swap(item.value_mut(), &mut value); // 					*item.key_mut() = key.clone().into(); // 					self.btree.insert_at(addr, Item::new(left, value)); // 				} // 			} // 			// Because we have `Some(left)` // 			// we know nothing on the left intersects the input range. // 			break // we are done // 		}, // 		Difference::After(right, _) => { // 			let right = right.cloned(); // 			if item.value() == &value { // 				let item = self.btree.item_mut(addr).unwrap(); // 				item.key_mut().add(&key); // 			} else { // 				let item = self.btree.item_mut(addr).unwrap(); // 				*item.key_mut() = right; // 				addr = self.btree.insert_at(addr, Item::new(key.clone().into(), value.clone())); // 			} // 		}, // 		Difference::Empty => { // 			let same_as_next = next_addr.map(|next_addr| self.btree.item(next_addr).unwrap().value() == &value).unwrap_or(false); // 			if same_as_next { // 				self.btree.remove_at(addr); // 			} else { // 				let item = self.btree.item_mut(addr).unwrap(); // 				item.key_mut().add(&key); // 				item.set_value(value.clone()); // 			} // 		} // 	} // 	// go to the previous item is it also intersects the input range. // 	match self.btree.previous_item_address(addr) { // 		Some(prev_addr) if self.btree.item(prev_addr).unwrap().key().connected_to(&key) => { // 			next_addr = Some(addr); // 			addr = prev_addr // 		}, // 		_ => break // otherwise we're done. // 	} // } } Err ( addr ) => { // case (G) self . btree . insert_at ( addr , Item :: new ( key , value ) ) ; } } }
Insert a new key-value binding.
Insert a new key - value binding .
&mut self
key
R
value
V