Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

📦 collections - Veri Yapıları

Vec, HashMap, HashSet, LinkedList, BTreeMap, BinaryHeap

89
Fonksiyon
1,126
Satır
34 KB
Boyut

🚀 Hızlı Başlangıç

içe_aktar collections

// Vec (dinamik dizi)
değişken v = collections.vec_new()
collections.vec_push(v, 10)
collections.vec_push(v, 20)
değişken eleman = collections.vec_get(v, 0)

// HashMap
değişken map = collections.hashmap_new()
collections.hashmap_insert(map, "name", "BERK")
collections.hashmap_insert(map, "version", "1.0")
değişken isim = collections.hashmap_get(map, "name")

// HashSet
değişken set = collections.hashset_new()
collections.hashset_insert(set, "apple")
collections.hashset_insert(set, "banana")
değişken var_mı = collections.hashset_contains(set, "apple")

// LinkedList
değişken list = collections.linkedlist_new()
collections.linkedlist_push_back(list, 1)
collections.linkedlist_push_front(list, 0)

// BinaryHeap (öncelik kuyruğu)
değişken heap = collections.heap_new()
collections.heap_push(heap, 5)
collections.heap_push(heap, 3)
değişken en_büyük = collections.heap_pop(heap)  // 5

📚 Veri Yapıları

  • Vec: Dinamik dizi, O(1) erişim, O(1) amortized push
  • HashMap: Hash tablosu, O(1) average insert/get/delete
  • HashSet: Unique elemanlar, O(1) average contains
  • LinkedList: Çift bağlı liste, O(1) push/pop başta/sonda
  • BTreeMap: Sıralı map, O(log n) operations
  • BinaryHeap: Max-heap, O(log n) push/pop, O(1) peek

💡 Örnekler

Word Frequency Counter

içe_aktar collections, string, io

fonksiyon word_frequency(text: yazı) -> HashMap yap
    değişken freq = collections.hashmap_new()
    değişken kelimeler = string.böl(text, " ")
    
    her kelime içinde kelimeler için yap
        değişken küçük = string.küçük_harf(kelime)
        
        eğer collections.hashmap_contains(freq, küçük) ise yap
            değişken count = collections.hashmap_get(freq, küçük)
            collections.hashmap_insert(freq, küçük, count + 1)
        son değilse yap
            collections.hashmap_insert(freq, küçük, 1)
        son
    son
    
    dön freq
son

değişken metin = io.dosya_oku("document.txt")
değişken frekans = word_frequency(metin)

// En çok geçen 10 kelimeyi göster
değişken kelimeler = collections.hashmap_keys(frekans)
collections.vec_sort_by(kelimeler, fonksiyon(a, b) yap
    dön collections.hashmap_get(frekans, b) - collections.hashmap_get(frekans, a)
son)

her i içinde aralık(0, 10) için yap
    değişken k = collections.vec_get(kelimeler, i)
    yazdır(k + ": " + collections.hashmap_get(frekans, k).yazıya())
son

LRU Cache Implementation

içe_aktar collections

sınıf LRUCache yap
    değişken kapasite: tamsayı
    değişken cache: HashMap
    değişken sıra: LinkedList
    
    fonksiyon yeni(kap: tamsayı) yap
        self.kapasite = kap
        self.cache = collections.hashmap_new()
        self.sıra = collections.linkedlist_new()
    son
    
    fonksiyon get(anahtar: yazı) -> herhangi yap
        eğer collections.hashmap_contains(self.cache, anahtar) değilse yap
            dön yok
        son
        
        // Sıraya taşı (LRU güncelle)
        collections.linkedlist_remove(self.sıra, anahtar)
        collections.linkedlist_push_back(self.sıra, anahtar)
        
        dön collections.hashmap_get(self.cache, anahtar)
    son
    
    fonksiyon put(anahtar: yazı, değer: herhangi) yap
        eğer collections.hashmap_contains(self.cache, anahtar) ise yap
            collections.linkedlist_remove(self.sıra, anahtar)
        son
        
        collections.hashmap_insert(self.cache, anahtar, değer)
        collections.linkedlist_push_back(self.sıra, anahtar)
        
        eğer collections.hashmap_size(self.cache) > self.kapasite ise yap
            // En eski elemanı çıkar
            değişken eski = collections.linkedlist_pop_front(self.sıra)
            collections.hashmap_remove(self.cache, eski)
        son
    son
son

değişken cache = LRUCache.yeni(3)
cache.put("a", 1)
cache.put("b", 2)
cache.put("c", 3)
yazdır(cache.get("a"))  // 1
cache.put("d", 4)  // "b" çıkarılır
yazdır(cache.get("b"))  // yok

← Tüm Modüller