📦 collections - Veri Yapıları

Vec, HashMap, HashSet, LinkedList, BTreeMap, BinaryHeap

89
function
1,126
lines
34 KB
Boyut

🚀 Quick Start

import collections

// Vec (dinamik dizi)
let v = collections.vec_new()
collections.vec_push(v, 10)
collections.vec_push(v, 20)
let eleman = collections.vec_get(v, 0)

// HashMap
let map = collections.hashmap_new()
collections.hashmap_insert(map, "name", "BERK")
collections.hashmap_insert(map, "version", "1.0")
let isim = collections.hashmap_get(map, "name")

// HashSet
let set = collections.hashset_new()
collections.hashset_insert(set, "apple")
collections.hashset_insert(set, "banana")
let var_mı = collections.hashset_contains(set, "apple")

// LinkedList
let list = collections.linkedlist_new()
collections.linkedlist_push_back(list, 1)
collections.linkedlist_push_front(list, 0)

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

📚 Veri Yapıları

💡 Exampleler

Word Frequency Counter

import collections, string, io

function word_frequency(text: str) -> HashMap do
    let freq = collections.hashmap_new()
    let kelimeler = string.böl(text, " ")
    
    each kelime in kelimeler for do
        let küçük = string.küçük_harf(kelime)
        
        if collections.hashmap_contains(freq, küçük)  do
            let count = collections.hashmap_get(freq, küçük)
            collections.hashmap_insert(freq, küçük, count + 1)
        end else do
            collections.hashmap_insert(freq, küçük, 1)
        end
    end
    
    return freq
end

let metin = io.dosya_oku("document.txt")
let frekans = word_frequency(metin)

// En çok geçen 10 kelimeyi göster
let kelimeler = collections.hashmap_keys(frekans)
collections.vec_sort_by(kelimeler, function(a, b) do
    return collections.hashmap_get(frekans, b) - collections.hashmap_get(frekans, a)
end)

each i in aralık(0, 10) for do
    let k = collections.vec_get(kelimeler, i)
    yazdır(k + ": " + collections.hashmap_get(frekans, k).yazıya())
end

LRU Cache Implementation

import collections

sınıf LRUCache do
    let kapasite: tamsayı
    let cache: HashMap
    let sıra: LinkedList
    
    function yeni(kap: tamsayı) do
        self.kapasite = kap
        self.cache = collections.hashmap_new()
        self.sıra = collections.linkedlist_new()
    end
    
    function get(anahtar: str) -> herhangi do
        if collections.hashmap_contains(self.cache, anahtar) else do
            return yok
        end
        
        // Sıraya taşı (LRU güncelle)
        collections.linkedlist_remove(self.sıra, anahtar)
        collections.linkedlist_push_back(self.sıra, anahtar)
        
        return collections.hashmap_get(self.cache, anahtar)
    end
    
    function put(anahtar: str, değer: herhangi) do
        if collections.hashmap_contains(self.cache, anahtar)  do
            collections.linkedlist_remove(self.sıra, anahtar)
        end
        
        collections.hashmap_insert(self.cache, anahtar, değer)
        collections.linkedlist_push_back(self.sıra, anahtar)
        
        if collections.hashmap_size(self.cache) > self.kapasite  do
            // En eski elemanı çıkar
            let eski = collections.linkedlist_pop_front(self.sıra)
            collections.hashmap_remove(self.cache, eski)
        end
    end
end

let 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

← All Modules