Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: find .tool-versions and legacy files until reach root directory and keep .tool-versions order. #289

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add SortedMap struct
aooohan committed May 23, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 06cda354b52b172796d4c462ff061c01ba5f0368
82 changes: 82 additions & 0 deletions internal/util/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2024 Han Li and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package util

//type Map[K comparable, V any] interface {
// Get(k K) (V, bool)
// Set(k K, v V) bool
// Remove(k K) V
// Contains(k K) bool
// Len() int
// ForEach(f func(k K, v V) error) error
//}

type SortedMap[K comparable, V any] struct {
keys []K
vals map[K]V
}

func (s *SortedMap[K, V]) Get(k K) (V, bool) {
v, ok := s.vals[k]
return v, ok
}

func (s *SortedMap[K, V]) Set(k K, v V) bool {
_, exists := s.vals[k]
if !exists {
s.keys = append(s.keys, k)
}
s.vals[k] = v
return !exists
}

func (s *SortedMap[K, V]) Remove(k K) V {
v := s.vals[k]
delete(s.vals, k)
for i, key := range s.keys {
if key == k {
s.keys = append(s.keys[:i], s.keys[i+1:]...)
break
}
}
return v
}

func (s *SortedMap[K, V]) Contains(k K) bool {
_, exists := s.vals[k]
return exists
}

func (s *SortedMap[K, V]) Len() int {
return len(s.keys)
}

func (s *SortedMap[K, V]) ForEach(f func(k K, v V) error) error {
for _, k := range s.keys {
if err := f(k, s.vals[k]); err != nil {
return err
}
}
return nil
}

func NewSortedMap[K comparable, V any]() *SortedMap[K, V] {
return &SortedMap[K, V]{
keys: []K{},
vals: make(map[K]V),
}
}
95 changes: 95 additions & 0 deletions internal/util/map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2024 Han Li and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package util

import (
"fmt"
"testing"
)

func TestSortedMap(t *testing.T) {
sm := NewSortedMap[int, string]()

// Test Set method
sm.Set(1, "one")

// Test Get method
if _, ok := sm.Get(1); !ok {
t.Errorf("Expected true, got false")
}

// Test Contains method
if !sm.Contains(1) {
t.Errorf("Expected true, got false")
}

// Test Len method
if sm.Len() != 1 {
t.Errorf("Expected 1, got %d", sm.Len())
}

// Test ForEach method
err := sm.ForEach(func(k int, v string) error {
if k != 1 || v != "one" {
return fmt.Errorf("Expected key: 1, value: 'one', got key: %d, value: %s", k, v)
}
return nil
})
if err != nil {
t.Errorf(err.Error())
}

// Test Remove method
val := sm.Remove(1)
if val != "one" {
t.Errorf("Expected 'one', got %s", val)
}
if sm.Contains(1) {
t.Errorf("Expected false, got true")
}
if sm.Len() != 0 {
t.Errorf("Expected 0, got %d", sm.Len())
}
}

func TestSortedMap_ForEach(t *testing.T) {
sm := NewSortedMap[int, string]()
sm.Set(1, "one")
sm.Set(2, "two")
sm.Set(3, "three")

var keys []int
var values []string
_ = sm.ForEach(func(k int, v string) error {
keys = append(keys, k)
values = append(values, v)
return nil
})

if len(keys) != 3 {
t.Errorf("Expected 3, got %d", len(keys))
}
if len(values) != 3 {
t.Errorf("Expected 3, got %d", len(values))
}
if keys[0] != 1 || keys[1] != 2 || keys[2] != 3 {
t.Errorf("Expected [1 2 3], got %v", keys)
}
if values[0] != "one" || values[1] != "two" || values[2] != "three" {
t.Errorf("Expected ['one' 'two' 'three'], got %v", values)
}
}