Skip to content

OrderedIndex: support multiple-column ordered index #126

@Kybxd

Description

@Kybxd

C++

To use struct as std::map keys in cpp, we should implement operator<:

struct Person {
    std::string name;
    int age;
    double height;

    bool operator<(const Person& other) const {
        if (name != other.name) return name < other.name;
        if (age != other.age) return age < other.age;
        return height < other.height;
    }
};

Go

To use struct as treemap keys in go, we should implement interface Ordered:

type Ordered[T any] interface {
	Less(another *T) bool
}
var _ Ordered[Person] = (*Person)(nil)

type Person struct {
	Name   string
	Age    int
	Height float64
}

func (p *Person) Less(another *Person) bool {
	if p.Name != another.Name {
		return p.Name < another.Name
	}
	if p.Age != another.Age {
		return p.Age < another.Age
	}
	return p.Height < another.Height
}

We should also create treemap.CustomKeyTreemap struct which accepts treemap.Ordered as key:

type CustomKeyTreemap[K Ordered[K], V any] struct {
    ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions