У нас вы можете посмотреть бесплатно Advance Practical PHP Big O Notation Collision - video 115V или скачать в максимальном доступном качестве, видео которое было загружено на ютуб. Для загрузки выберите вариант из формы ниже:
Если кнопки скачивания не
загрузились
НАЖМИТЕ ЗДЕСЬ или обновите страницу
Если возникают проблемы со скачиванием видео, пожалуйста напишите в поддержку по адресу внизу
страницы.
Спасибо за использование сервиса ClipSaver.ru
That is an excellent breakdown of how PHP handles the "under-the-hood" mechanics of hash maps. Since we are talking about Collisions and Chaining, a quick visual helps clarify why that O(1) to O(n) slowdown happens. Remember: Here are three high-level concepts that explain why it works so well: * The Power of the Prime: PHP's internal array sizes are often powers of two, but the hashing algorithm (DJB2) is designed to spread keys as evenly as possible to minimize those "linked list" chains. * The Load Factor: PHP automatically resizes its internal memory structure when the number of elements gets too high. By doubling the size of the array and "re-hashing" everything, it keeps the chains short and the speed close to O(1). * Hash Flooding (Security): Historically, attackers could exploit collisions by sending thousands of keys that result in the same hash, essentially "breaking" a server's performance. Modern PHP uses Hash DoS protection (randomized seeds) to prevent this. Building a hash map from scratch is the best way to demystify how keys turn into memory addresses. In this example, we’ll use a simple modulo operation for our hashing logic and implement Chaining to handle collisions. Why we can't just copy the data. You might wonder: "Why can't we just move the old buckets into the new array?" Because the index is calculated using $hash % $size. If your original size was 10, a key might land at index 3. If you double the size to 20, that same key's new position is $hash % 20, which might be 13. If you don't re-calculate, your get() function will look in the wrong place and never find the data. Performance Trade-off Rehashing is an "expensive" operation because it touches every single item in the map (O(n)). However, because it happens so infrequently, the "amortized" (average) cost of adding an item remains O(1). 2. Verifying the "Hidden" Mechanics To actually see if your rehashing and collision logic is working, you can add this temporary method to your class to peek at the internal structure: Why this works now: The get() function: It calculates the hash for the key you're asking for, goes to that specific "bucket," and loops through the small list inside to find your match. The &$pair in s e t (): In your original code, $pair['value'] = $value was only changing a temporary copy. Adding that little & symbol tells PHP to change the actual data in the bucket. Key Observations for your Results: Update vs. Insert: If you call $myMap s e t("name", "Da Vinci"), your code should find the existing "name" key in the bucket and update it rather than creating a second "name" entry. Collision Handling: If two different keys (like "A" and "F") end up in the same bucket index, you'll see two pairs listed in that same bucket—this confirms your Chaining logic is working. Rehash Success: After a rehash, the items should be spread out across more buckets, reducing the number of collisions. Why this works now: The get() function: It calculates the hash for the key you're asking for, goes to that specific "bucket," and loops through the small list inside to find your match. The $pair['value'] = $value was only changing a temporary copy. Adding that little & symbol tells PHP to change the actual data in the bucket.